예제 #1
0
        private async Task <OperationResult <string> > CallHttp(ArgumentProvider argument)
        {
            var url     = argument.Format(Settings.Url);
            var call    = HttpProvider.CallUrl(url);
            var headers = new List <KeyValuePair <string, string> >();

            if (Settings.Headers?.Any() == true)
            {
                foreach (var kvp in Settings.Headers)
                {
                    var headerValue = argument.Format(kvp.Value);
                    headers.Add(new KeyValuePair <string, string>(kvp.Key, headerValue));
                    call.WithHeader(kvp.Key, headerValue);
                }
            }
            call.AcceptStatusCodes(Settings.SuccessStatusCodes.Select(x => (HttpStatusCode)x).ToArray());
            string body = string.Empty;

            if (!string.IsNullOrEmpty(Settings.Body))
            {
                body = argument.Format(Settings.Body);
                call.WithJsonBody(body.ToJsonObject());
            }
            if (Settings.FormData?.Any() == true)
            {
                var formData = new List <KeyValuePair <string, string> >();
                foreach (var kvp in Settings.FormData)
                {
                    var formValue = argument.Format(kvp.Value);
                    formData.Add(new KeyValuePair <string, string>(kvp.Key, formValue));
                    call.WithHttpParameter(kvp.Key, formValue);
                }
                body = JsonConvert.SerializeObject(formData);
            }
            var executionData = new
            {
                Settings.Method,
                url,
                headers,
                body
            };

            argument.PutPrivate(nameof(executionData), JsonConvert.SerializeObject(executionData));

            if ("get".Equals(Settings.Method, StringComparison.CurrentCultureIgnoreCase))
            {
                return(await call.GetAsync());
            }
            else if ("post".Equals(Settings.Method, StringComparison.CurrentCultureIgnoreCase))
            {
                return(await call.PostAsync <string>());
            }
            else if ("put".Equals(Settings.Method, StringComparison.CurrentCultureIgnoreCase))
            {
                return(await call.PutAsync <string>());
            }
            else if ("delete".Equals(Settings.Method, StringComparison.CurrentCultureIgnoreCase))
            {
                return(await call.DeleteAsync <string>());
            }

            return(new OperationResult <string>
            {
                Success = false,
                Code = Messages.UnsupportedHttpMethod.Code,
                Message = Messages.UnsupportedHttpMethod.Message
            });
        }