| | | 1 | | namespace ArturRios.Util.Http; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// A lightweight HTTP gateway that wraps <see cref="HttpClient"/> and provides typed helpers |
| | | 5 | | /// for common HTTP verbs, returning <see cref="HttpOutput{TBody}"/> with deserialized content. |
| | | 6 | | /// </summary> |
| | 5 | 7 | | public class HttpGateway(HttpClient client) |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Gets the underlying <see cref="HttpClient"/> used to perform HTTP requests. |
| | | 11 | | /// </summary> |
| | 10 | 12 | | public HttpClient Client { get; } = client; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Sends a GET request to the specified route and deserializes the response body. |
| | | 16 | | /// </summary> |
| | | 17 | | /// <typeparam name="TBody">The expected response body type.</typeparam> |
| | | 18 | | /// <param name="route">The relative or absolute route for the request.</param> |
| | | 19 | | /// <returns>An <see cref="HttpOutput{TBody}"/> containing status, headers, and the deserialized body.</returns> |
| | | 20 | | public async Task<HttpOutput<TBody?>> GetAsync<TBody>(string route) |
| | 1 | 21 | | { |
| | 1 | 22 | | var response = await Client.GetAsync(route); |
| | | 23 | | |
| | 1 | 24 | | return await ResolveResponseAsync<TBody?>(response); |
| | 1 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Sends a PATCH request with an optional payload, serialized as JSON. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <typeparam name="TBody">The expected response body type.</typeparam> |
| | | 31 | | /// <param name="route">The route to send the request to.</param> |
| | | 32 | | /// <param name="payloadObject">An optional object to be serialized as JSON for the request body.</param> |
| | | 33 | | /// <returns>An <see cref="HttpOutput{TBody}"/> with the response information and body.</returns> |
| | | 34 | | public async Task<HttpOutput<TBody?>> PatchAsync<TBody>(string route, object? payloadObject = null) |
| | 1 | 35 | | { |
| | 1 | 36 | | var payload = payloadObject?.ToJsonStringContent(); |
| | | 37 | | |
| | 1 | 38 | | var response = await Client.PatchAsync(route, payload); |
| | | 39 | | |
| | 1 | 40 | | return await ResolveResponseAsync<TBody?>(response); |
| | 1 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Sends a POST request with an optional payload, serialized as JSON. |
| | | 45 | | /// </summary> |
| | | 46 | | /// <typeparam name="TBody">The expected response body type.</typeparam> |
| | | 47 | | /// <param name="route">The route to send the request to.</param> |
| | | 48 | | /// <param name="payloadObject">An optional object to be serialized as JSON for the request body.</param> |
| | | 49 | | /// <returns>An <see cref="HttpOutput{TBody}"/> with the response information and body.</returns> |
| | | 50 | | public async Task<HttpOutput<TBody?>> PostAsync<TBody>(string route, object? payloadObject = null) |
| | 1 | 51 | | { |
| | 1 | 52 | | var payload = payloadObject?.ToJsonStringContent(); |
| | | 53 | | |
| | 1 | 54 | | var response = await Client.PostAsync(route, payload); |
| | | 55 | | |
| | 1 | 56 | | return await ResolveResponseAsync<TBody?>(response); |
| | 1 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Sends a PUT request with an optional payload, serialized as JSON. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <typeparam name="TBody">The expected response body type.</typeparam> |
| | | 63 | | /// <param name="route">The route to send the request to.</param> |
| | | 64 | | /// <param name="payloadObject">An optional object to be serialized as JSON for the request body.</param> |
| | | 65 | | /// <returns>An <see cref="HttpOutput{TBody}"/> with the response information and body.</returns> |
| | | 66 | | public async Task<HttpOutput<TBody?>> PutAsync<TBody>(string route, object? payloadObject = null) |
| | 1 | 67 | | { |
| | 1 | 68 | | var payload = payloadObject?.ToJsonStringContent(); |
| | | 69 | | |
| | 1 | 70 | | var response = await Client.PutAsync(route, payload); |
| | | 71 | | |
| | 1 | 72 | | return await ResolveResponseAsync<TBody?>(response); |
| | 1 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Sends a DELETE request to the specified route and deserializes the response body. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <typeparam name="TBody">The expected response body type.</typeparam> |
| | | 79 | | /// <param name="route">The route to send the request to.</param> |
| | | 80 | | /// <returns>An <see cref="HttpOutput{TBody}"/> with the response information and body.</returns> |
| | | 81 | | public async Task<HttpOutput<TBody?>> DeleteAsync<TBody>(string route) |
| | 1 | 82 | | { |
| | 1 | 83 | | var response = await Client.DeleteAsync(route); |
| | | 84 | | |
| | 1 | 85 | | return await ResolveResponseAsync<TBody?>(response); |
| | 1 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Resolves the HTTP response into an <see cref="HttpOutput{TBody}"/> and reads the content. |
| | | 90 | | /// </summary> |
| | | 91 | | /// <typeparam name="TBody">The target type to deserialize the response content to.</typeparam> |
| | | 92 | | /// <param name="response">The <see cref="HttpResponseMessage"/> to process.</param> |
| | | 93 | | /// <returns>An <see cref="HttpOutput{TBody}"/> with populated status, headers, and body.</returns> |
| | | 94 | | private static async Task<HttpOutput<TBody?>> ResolveResponseAsync<TBody>(HttpResponseMessage response) |
| | 5 | 95 | | { |
| | 5 | 96 | | var output = new HttpOutput<TBody?>(response); |
| | | 97 | | |
| | 5 | 98 | | await output.ReadContent(); |
| | | 99 | | |
| | 5 | 100 | | return output; |
| | 5 | 101 | | } |
| | | 102 | | } |