/// <summary> /// Get request hash. /// </summary> /// <param name="response">Response to get hash from.</param> /// <returns>Returns hash string for the request.</returns> public static string?GetRequestHash(this FluentHttpResponse response) { if (response.Items.TryGetValue(HashKey, out var value)) { return((string)value); } return(null); }
public void Push(string key, FluentHttpResponse response) { if (_contexts.TryGetValue(key, out var context)) { context.Response = response; return; } _contexts.Add(key, new FluentlyExecutionContext { Response = response }); }
/// <summary> /// Send request and returns HTTP Response and also read content with the type specified (when success). /// </summary> /// <typeparam name="T">Type to return.</typeparam> /// <returns>Return response with data typed.</returns> public async Task <FluentHttpResponse <T> > ReturnAsResponse <T>() { var response = await ReturnAsResponse().ConfigureAwait(false); var genericResponse = new FluentHttpResponse <T>(response); if (genericResponse.IsSuccessStatusCode) { genericResponse.Data = await genericResponse.Content.ReadAsAsync <T>(_fluentHttpClient.Formatters, _cancellationToken) .ConfigureAwait(false); } return(genericResponse); }
/// <summary> /// Clone response. /// </summary> /// <param name="response">Response to clone.</param> public static async Task <FluentHttpResponse> Clone(this FluentHttpResponse response) { var contentString = await response.Content.ReadAsStringAsync(); var contentType = response.Content.Headers.ContentType; var encoding = string.IsNullOrEmpty(contentType.CharSet) ? Encoding.UTF8 : Encoding.GetEncoding(contentType.CharSet); var cloned = new FluentHttpResponse(new HttpResponseMessage(response.StatusCode) { Content = new StringContent(contentString, encoding, contentType.MediaType), ReasonPhrase = response.ReasonPhrase, Version = response.Message.Version, RequestMessage = response.Message.RequestMessage, }, response.Items); cloned.Headers.CopyFrom(response.Headers); return(cloned); }
/// <summary> /// Get time taken for the response. This is generally set via <see cref="TimerHttpMiddleware"/>. /// </summary> /// <param name="response">Response to get time from.</param> /// <returns>Returns timespan for the time taken.</returns> public static TimeSpan GetTimeTaken(this FluentHttpResponse response) => (TimeSpan)response.Items[TimeTakenKey];
/// <summary> /// Set time taken. /// </summary> /// <param name="response">Response instance.</param> /// <param name="value">Timespan value.</param> public static FluentHttpResponse SetTimeTaken(this FluentHttpResponse response, TimeSpan value) { response.Items.Add(TimeTakenKey, value); return(response); }
/// <summary> /// Initializes a new <see cref="FluentHttpResponse"/>. /// </summary> /// <param name="response"></param> public FluentHttpResponse(FluentHttpResponse response) : base(response.Message) { Items = response.Items; }
/// <summary> /// Read FluentHttpResponse as Stream /// </summary> /// <typeparam name="T">Response content type</typeparam> /// <param name="response">T</param> /// <returns></returns> public static async Task <T> As <T>(this FluentHttpResponse response) { var streamContent = await response.Message.Content.ReadAsStreamAsync().ConfigureAwait(false); return(DeserializeJsonFromStream <T>(streamContent)); }
public void Push(string key, FluentHttpResponse response) { var context = _contexts.GetOrAdd(key, _ => new FluentlyExecutionContext()); context.Response = response; }