|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using ManagedCode.Communication; |
| 6 | +using ManagedCode.Communication.Extensions; |
| 7 | +using Polly; |
| 8 | + |
| 9 | +namespace ManagedCode.Communication.Extensions.Http; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// Helpers that execute <see cref="HttpClient"/> requests and transform the responses into |
| 13 | +/// <see cref="ManagedCode.Communication.Result"/> instances. |
| 14 | +/// </summary> |
| 15 | +public static class ResultHttpClientExtensions |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Sends a request built by <paramref name="requestFactory"/> and converts the HTTP response into a |
| 19 | + /// <see cref="Result{T}"/>. When a <paramref name="pipeline"/> is provided the request is executed through it, |
| 20 | + /// enabling Polly resilience strategies such as retries or circuit breakers. |
| 21 | + /// </summary> |
| 22 | + /// <typeparam name="T">The JSON payload type that the endpoint returns in case of success.</typeparam> |
| 23 | + /// <param name="client">The <see cref="HttpClient"/> used to send the request.</param> |
| 24 | + /// <param name="requestFactory">Factory that creates a fresh <see cref="HttpRequestMessage"/> for each attempt.</param> |
| 25 | + /// <param name="pipeline">Optional Polly resilience pipeline that wraps the HTTP invocation.</param> |
| 26 | + /// <param name="cancellationToken">Token that cancels the request execution.</param> |
| 27 | + public static Task<Result<T>> SendForResultAsync<T>( |
| 28 | + this HttpClient client, |
| 29 | + Func<HttpRequestMessage> requestFactory, |
| 30 | + ResiliencePipeline<HttpResponseMessage>? pipeline = null, |
| 31 | + CancellationToken cancellationToken = default) |
| 32 | + { |
| 33 | + ArgumentNullException.ThrowIfNull(client); |
| 34 | + ArgumentNullException.ThrowIfNull(requestFactory); |
| 35 | + |
| 36 | + return SendCoreAsync( |
| 37 | + client, |
| 38 | + requestFactory, |
| 39 | + static response => response.FromJsonToResult<T>(), |
| 40 | + pipeline, |
| 41 | + cancellationToken); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Sends a request built by <paramref name="requestFactory"/> and converts the HTTP response into a |
| 46 | + /// <see cref="Result"/> without a payload. When a <paramref name="pipeline"/> is provided the request is executed |
| 47 | + /// through it, enabling Polly resilience strategies such as retries or circuit breakers. |
| 48 | + /// </summary> |
| 49 | + /// <param name="client">The <see cref="HttpClient"/> used to send the request.</param> |
| 50 | + /// <param name="requestFactory">Factory that creates a fresh <see cref="HttpRequestMessage"/> for each attempt.</param> |
| 51 | + /// <param name="pipeline">Optional Polly resilience pipeline that wraps the HTTP invocation.</param> |
| 52 | + /// <param name="cancellationToken">Token that cancels the request execution.</param> |
| 53 | + public static Task<Result> SendForResultAsync( |
| 54 | + this HttpClient client, |
| 55 | + Func<HttpRequestMessage> requestFactory, |
| 56 | + ResiliencePipeline<HttpResponseMessage>? pipeline = null, |
| 57 | + CancellationToken cancellationToken = default) |
| 58 | + { |
| 59 | + ArgumentNullException.ThrowIfNull(client); |
| 60 | + ArgumentNullException.ThrowIfNull(requestFactory); |
| 61 | + |
| 62 | + return SendCoreAsync( |
| 63 | + client, |
| 64 | + requestFactory, |
| 65 | + static response => response.FromRequestToResult(), |
| 66 | + pipeline, |
| 67 | + cancellationToken); |
| 68 | + } |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Performs a GET request for <paramref name="requestUri"/> and converts the response into a |
| 72 | + /// <see cref="Result{T}"/>. The optional <paramref name="pipeline"/> allows attaching Polly retry or circuit |
| 73 | + /// breaker strategies. |
| 74 | + /// </summary> |
| 75 | + /// <typeparam name="T">The JSON payload type that the endpoint returns in case of success.</typeparam> |
| 76 | + /// <param name="client">The <see cref="HttpClient"/> used to send the request.</param> |
| 77 | + /// <param name="requestUri">The request URI.</param> |
| 78 | + /// <param name="pipeline">Optional Polly resilience pipeline that wraps the HTTP invocation.</param> |
| 79 | + /// <param name="cancellationToken">Token that cancels the request execution.</param> |
| 80 | + public static Task<Result<T>> GetAsResultAsync<T>( |
| 81 | + this HttpClient client, |
| 82 | + string requestUri, |
| 83 | + ResiliencePipeline<HttpResponseMessage>? pipeline = null, |
| 84 | + CancellationToken cancellationToken = default) |
| 85 | + { |
| 86 | + ArgumentNullException.ThrowIfNull(client); |
| 87 | + ArgumentException.ThrowIfNullOrEmpty(requestUri); |
| 88 | + |
| 89 | + return client.SendForResultAsync<T>( |
| 90 | + () => new HttpRequestMessage(HttpMethod.Get, requestUri), |
| 91 | + pipeline, |
| 92 | + cancellationToken); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Performs a GET request for <paramref name="requestUri"/> and converts the response into a non generic |
| 97 | + /// <see cref="Result"/>. |
| 98 | + /// </summary> |
| 99 | + /// <param name="client">The <see cref="HttpClient"/> used to send the request.</param> |
| 100 | + /// <param name="requestUri">The request URI.</param> |
| 101 | + /// <param name="pipeline">Optional Polly resilience pipeline that wraps the HTTP invocation.</param> |
| 102 | + /// <param name="cancellationToken">Token that cancels the request execution.</param> |
| 103 | + public static Task<Result> GetAsResultAsync( |
| 104 | + this HttpClient client, |
| 105 | + string requestUri, |
| 106 | + ResiliencePipeline<HttpResponseMessage>? pipeline = null, |
| 107 | + CancellationToken cancellationToken = default) |
| 108 | + { |
| 109 | + ArgumentNullException.ThrowIfNull(client); |
| 110 | + ArgumentException.ThrowIfNullOrEmpty(requestUri); |
| 111 | + |
| 112 | + return client.SendForResultAsync( |
| 113 | + () => new HttpRequestMessage(HttpMethod.Get, requestUri), |
| 114 | + pipeline, |
| 115 | + cancellationToken); |
| 116 | + } |
| 117 | + |
| 118 | + private static async Task<TResponse> SendCoreAsync<TResponse>( |
| 119 | + HttpClient client, |
| 120 | + Func<HttpRequestMessage> requestFactory, |
| 121 | + Func<HttpResponseMessage, Task<TResponse>> convert, |
| 122 | + ResiliencePipeline<HttpResponseMessage>? pipeline, |
| 123 | + CancellationToken cancellationToken) |
| 124 | + { |
| 125 | + if (pipeline is null) |
| 126 | + { |
| 127 | + using var request = requestFactory(); |
| 128 | + using var directResponse = await client.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| 129 | + return await convert(directResponse).ConfigureAwait(false); |
| 130 | + } |
| 131 | + |
| 132 | + var httpResponse = await pipeline.ExecuteAsync( |
| 133 | + async cancellationToken => |
| 134 | + { |
| 135 | + using var request = requestFactory(); |
| 136 | + return await client.SendAsync(request, cancellationToken).ConfigureAwait(false); |
| 137 | + }, |
| 138 | + cancellationToken).ConfigureAwait(false); |
| 139 | + |
| 140 | + using (httpResponse) |
| 141 | + { |
| 142 | + return await convert(httpResponse).ConfigureAwait(false); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments