Exemple #1
0
        private async Task <YawatResult> RequestAsync(HttpMethod verb, string endPoint, IYawatRequestData requestData,
                                                      YawatOptions options = null)
        {
            var jsonStr = requestData?.Serialize();

            return(await this.RequestAsync(verb, endPoint, jsonStr, options));
        }
Exemple #2
0
        private string PreprocessRequest(string endPoint, YawatOptions options)
        {
            var url = options.GetUrl(endPoint);

            options.Authenticator?.UpdateHttpClient(this.httpClient);

            this.BeforeRequestAsync?.Invoke(url, this.httpClient);

            return(url);
        }
Exemple #3
0
        public void RunBeforeAnyTests()
        {
            Options = new Yawat.YawatOptions(typeof(JsonResponseData))
            {
                Host     = YawatSettings.Get("Host", "localhost"),
                Protocol = YawatSettings.Get("Protocol", "https"),
                Port     = YawatSettings.Get("Port", "44328")
            };

            HttpClientWithOptions = new YawatHttpClient(Options);
        }
Exemple #4
0
        private YawatResult Request(HttpMethod verb, string endPoint, string jsonString = "",
                                    YawatOptions options = null)
        {
            YawatResult json = null;

            Task.Run(async() =>
            {
                json = await this.RequestAsync(verb, endPoint, jsonString, options);
            }).Wait();

            return(json);
        }
Exemple #5
0
        public YawatOptions OverwrittenBy(YawatOptions options)
        {
            var newOption = new YawatOptions(null)
            {
                MediaType      = options.MediaType ?? this.MediaType,
                ResponseType   = options.ResponseType ?? this.ResponseType,
                Host           = options.Host ?? this.Host,
                Protocol       = options.Protocol ?? this.Protocol,
                Port           = options.Port ?? this.Port,
                Authenticator  = options.Authenticator ?? this.Authenticator,
                IncludeHeaders = this.IncludeHeaders || options.IncludeHeaders
            };

            return(newOption);
        }
Exemple #6
0
        private async Task <YawatResult> RequestAsync(HttpMethod verb, string endPoint, string jsonString = "", YawatOptions options = null)
        {
            var useOptions = this.yawatOptions;

            if (options != null)
            {
                useOptions = this.yawatOptions.OverwrittenBy(options);
            }

            var requestUri = this.PreprocessRequest(endPoint, useOptions);

            var request = new HttpRequestMessage(verb, requestUri);

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(useOptions.MediaType));

            if (verb == HttpMethod.Post || verb == HttpMethod.Put)
            {
                request.Content = new StringContent(jsonString, Encoding.UTF8, useOptions.MediaType);
            }

            HttpResponseMessage response;

            if (useOptions.MockHttpClient != null && useOptions.MockHttpClient.EndpointHandlersDictionary.ContainsKey(endPoint))
            {
                var endpointHandler = useOptions.MockHttpClient.EndpointHandlersDictionary[endPoint];
                response = new HttpResponseMessage(endpointHandler.StatusCode)
                {
                    Content = new StringContent(endpointHandler.Data, System.Text.Encoding.UTF8,
                                                useOptions.MediaType)
                };
            }
            else
            {
                try
                {
                    response = await this.httpClient.SendAsync(request);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    response = new HttpResponseMessage
                    {
                        StatusCode = HttpStatusCode.BadRequest
                    };
                }
            }

            return(await this.PostProcessResponse(response, useOptions));
        }
Exemple #7
0
 public async Task <YawatResult> TraceAsync(string endPoint, YawatOptions options = null) => await this.RequestAsync(HttpMethod.Put, endPoint, string.Empty, options);
Exemple #8
0
 public async Task <YawatResult> PutAsync(string endPoint, string jsonString, YawatOptions options = null) => await this.RequestAsync(HttpMethod.Put, endPoint, jsonString, options);
Exemple #9
0
 public async Task <YawatResult> PostAsync(string endPoint, IYawatRequestData requestData, YawatOptions options = null) => await this.RequestAsync(HttpMethod.Post, endPoint, requestData, options);
Exemple #10
0
 public YawatResult Trace(string endPoint, YawatOptions options = null) => this.Request(HttpMethod.Put, endPoint, string.Empty, options);
Exemple #11
0
 public YawatResult Put(string endPoint, string jsonString, YawatOptions options = null) => this.Request(HttpMethod.Put, endPoint, jsonString, options);
Exemple #12
0
 public YawatHttpClient(YawatOptions options)
 {
     this.yawatOptions = options;
     this.httpClient   = new System.Net.Http.HttpClient();
 }
Exemple #13
0
        private async Task <YawatResult> PostProcessResponse(HttpResponseMessage response, YawatOptions options)
        {
            var stringData = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var responseData = Activator.CreateInstance(options.ResponseType, stringData) as IYawatResponseData;

            var result = new YawatResult(responseData)
            {
                StatusCode = response.StatusCode
            };

            if (options.IncludeHeaders)
            {
                foreach (var header in response.Headers)
                {
                    result.Headers.Add(header.Key, header.Value);
                }

                foreach (var header in response.Content.Headers)
                {
                    result.Headers.Add(header.Key, header.Value);
                }
            }

            return(result);
        }