< Summary

Information
Class: ArturRios.Util.Http.HttpGateway
Assembly: ArturRios.Util
File(s): D:\Repositories\dotnet-util\src\Http\HttpGateway.cs
Line coverage
100%
Covered lines: 30
Uncovered lines: 0
Coverable lines: 30
Total lines: 102
Line coverage: 100%
Branch coverage
50%
Covered branches: 3
Total branches: 6
Branch coverage: 50%
Method coverage
100%
Covered methods: 8
Fully covered methods: 2
Total methods: 8
Method coverage: 100%
Full method coverage: 25%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Client()100%11100%
GetAsync()100%11100%
PatchAsync()50%22100%
PostAsync()50%22100%
PutAsync()50%22100%
DeleteAsync()100%11100%
ResolveResponseAsync()100%11100%

File(s)

D:\Repositories\dotnet-util\src\Http\HttpGateway.cs

#LineLine coverage
 1namespace 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>
 57public class HttpGateway(HttpClient client)
 8{
 9    /// <summary>
 10    /// Gets the underlying <see cref="HttpClient"/> used to perform HTTP requests.
 11    /// </summary>
 1012    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)
 121    {
 122        var response = await Client.GetAsync(route);
 23
 124        return await ResolveResponseAsync<TBody?>(response);
 125    }
 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)
 135    {
 136        var payload = payloadObject?.ToJsonStringContent();
 37
 138        var response = await Client.PatchAsync(route, payload);
 39
 140        return await ResolveResponseAsync<TBody?>(response);
 141    }
 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)
 151    {
 152        var payload = payloadObject?.ToJsonStringContent();
 53
 154        var response = await Client.PostAsync(route, payload);
 55
 156        return await ResolveResponseAsync<TBody?>(response);
 157    }
 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)
 167    {
 168        var payload = payloadObject?.ToJsonStringContent();
 69
 170        var response = await Client.PutAsync(route, payload);
 71
 172        return await ResolveResponseAsync<TBody?>(response);
 173    }
 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)
 182    {
 183        var response = await Client.DeleteAsync(route);
 84
 185        return await ResolveResponseAsync<TBody?>(response);
 186    }
 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)
 595    {
 596        var output = new HttpOutput<TBody?>(response);
 97
 598        await output.ReadContent();
 99
 5100        return output;
 5101    }
 102}