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)); }
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); }
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); }
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); }
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); }
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)); }
public async Task <YawatResult> TraceAsync(string endPoint, YawatOptions options = null) => await this.RequestAsync(HttpMethod.Put, endPoint, string.Empty, options);
public async Task <YawatResult> PutAsync(string endPoint, string jsonString, YawatOptions options = null) => await this.RequestAsync(HttpMethod.Put, endPoint, jsonString, options);
public async Task <YawatResult> PostAsync(string endPoint, IYawatRequestData requestData, YawatOptions options = null) => await this.RequestAsync(HttpMethod.Post, endPoint, requestData, options);
public YawatResult Trace(string endPoint, YawatOptions options = null) => this.Request(HttpMethod.Put, endPoint, string.Empty, options);
public YawatResult Put(string endPoint, string jsonString, YawatOptions options = null) => this.Request(HttpMethod.Put, endPoint, jsonString, options);
public YawatHttpClient(YawatOptions options) { this.yawatOptions = options; this.httpClient = new System.Net.Http.HttpClient(); }
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); }