public override async Task ExecuteActionAsync()

in Source/Actions/Microsoft.Deployment.Actions.AzureCustom/Reddit/RetrieveSocialGistApiKey.cs [34:141]


        public override async Task<ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var errors = new List<string>();
            var firstName = request.DataStore.GetValue(SocialgistRegistrationFirstName);
            if (string.IsNullOrWhiteSpace(firstName))
            {
                errors.Add(FormatRequiredParameterMessage("First Name"));
            }

            var lastName = request.DataStore.GetValue(SocialgistRegistrationLastName);
            if (string.IsNullOrWhiteSpace(lastName))
            {
                errors.Add(FormatRequiredParameterMessage("Last Name"));
            }

            var email = request.DataStore.GetValue(SocialgistRegistrationEmailAddress);
            if (string.IsNullOrWhiteSpace(email))
            {
                errors.Add(FormatRequiredParameterMessage("Email Address"));
            }

            var jobTitle = request.DataStore.GetValue(SocialgistRegistrationJobTitle);
            if (string.IsNullOrWhiteSpace(jobTitle))
            {
                errors.Add(FormatRequiredParameterMessage("Job Title"));
            }

            var companyName = request.DataStore.GetValue(SocialgistRegistrationCompanyName);
            if (string.IsNullOrWhiteSpace(companyName))
            {
                errors.Add(FormatRequiredParameterMessage("Company Name"));
            }

            var descriptionOfUse = request.DataStore.GetValue(SocialgistRegistrationDescriptionOfUse);
            if (string.IsNullOrWhiteSpace(descriptionOfUse))
            {
                errors.Add(FormatRequiredParameterMessage("Description of Use"));
            }

            if (!bool.TryParse(request.DataStore.GetValue(SocialgistRegistrationAcceptCorrespondenceTerms), out var acceptCorrespondenceTerms))
            {
                errors.Add(FormatRequiredParameterMessage("Acceptance of Correspondence Terms"));
            }

            if (errors.Count != 0)
            {
                var errorMessage = string.Join("<br/>", errors);
                return new ActionResponse(
                    ActionStatus.Failure,
                    null,
                    null,
                    DefaultErrorCodes.DefaultErrorCode,
                    errorMessage
                );
            }

            var descriptionPayload = DescriptionFromFields(
                firstName,
                lastName,
                email,
                jobTitle,
                companyName,
                acceptCorrespondenceTerms,
                descriptionOfUse
            );

            try
            {
                var socialGistApiKey = await RetrieveKey(
                    Constants.SocialGistProvisionKeyUrl,
                    Constants.SocialGistProvisionKeyUserName,
                    Constants.SocialGistProvisionKeyPassphrase,
                    descriptionPayload
                );
                if (socialGistApiKey == null)
                {
                    // the only way this can happen is in KeyFromJson - either the json format has changed, or we did not get json back, or something about it confused json.net's parser.
                    var exception =
                        new Exception(
                            "An error occurred contacting Socialgist for your Reddit API key.  The response from Socialgist was unexpected and parsing failed.");
                    return new ActionResponse(
                        ActionStatus.Failure,
                        null,
                        exception,
                        DefaultErrorCodes.DefaultErrorCode,
                        "An error occurred contacting Socialgist for your Reddit API key"
                    );
                }
                // currently returns no value, which we then use and place into the AzureFunction AppSetting step.  Once you populate this with the real value from SocialGist, you shouldn't have to change
                // the init.json
                request.DataStore.AddToDataStore(
                    SocialGistApiKeyParameter,
                    socialGistApiKey,
                    DataStoreType.Private
                );
                return new ActionResponse(ActionStatus.Success);
            }
            catch (Exception e)
            {
                return new ActionResponse(
                    ActionStatus.Failure,
                    null,
                    e,
                    DefaultErrorCodes.DefaultErrorCode,
                    "An error occurred contacting Socialgist for your Reddit API key"
                );
            }
        }