< Summary

Information
Class: ArturRios.Util.WebApi.Client.BaseWebApiClientRoute
Assembly: ArturRios.Util.WebApi
File(s): /home/runner/work/dotnet-webapi-util/dotnet-webapi-util/src/Client/BaseWebApiClientRoute.cs
Line coverage
100%
Covered lines: 11
Uncovered lines: 0
Coverable lines: 11
Total lines: 49
Line coverage: 100%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage
100%
Covered methods: 4
Fully covered methods: 4
Total methods: 4
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
AuthenticateAsync()50%22100%
Authorize(...)100%11100%
AuthenticateAndAuthorizeAsync()100%44100%

File(s)

/home/runner/work/dotnet-webapi-util/dotnet-webapi-util/src/Client/BaseWebApiClientRoute.cs

#LineLine coverage
 1using System.Net.Http.Headers;
 2using ArturRios.Util.Http;
 3using ArturRios.Util.WebApi.Security.Records;
 4
 5namespace ArturRios.Util.WebApi.Client;
 6
 7/// <summary>Base class for a group of related routes (endpoints) on a remote web API, sharing a base URL and gateway.</
 8/// <param name="gateway">The HTTP gateway used to issue requests.</param>
 39public abstract class BaseWebApiClientRoute(HttpGateway gateway)
 10{
 11    /// <summary>The HTTP gateway used to issue requests to this route group.</summary>
 312    protected readonly HttpGateway Gateway = gateway;
 13
 14    /// <summary>The base URL of the routes exposed by this class.</summary>
 15    public abstract string BaseUrl { get; }
 16
 17    /// <summary>Authenticates against <paramref name="authRoute"/> using <paramref name="credentials"/> and returns the
 18    /// <param name="credentials">The credentials to authenticate with.</param>
 19    /// <param name="authRoute">The relative authentication route.</param>
 20    /// <returns>The <see cref="Authentication"/> response returned by the API.</returns>
 21    /// <exception cref="WebApiClientException">Thrown when the authentication response contains no body.</exception>
 22    protected async Task<Authentication> AuthenticateAsync(Credentials credentials, string authRoute)
 23    {
 224        var output = await Gateway.PostAsync<Authentication>(authRoute, credentials);
 25
 226        return output.Body ?? throw new WebApiClientException("Could not authenticate: the authentication response conta
 227    }
 28
 29    /// <summary>Sets the bearer token used for subsequent requests made through <see cref="Gateway"/>.</summary>
 30    /// <param name="authToken">The JWT to send as a Bearer token.</param>
 31    protected void Authorize(string authToken) =>
 332        Gateway.Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
 33
 34    /// <summary>Authenticates against <paramref name="authRoute"/> and applies the resulting token to the gateway's def
 35    /// <param name="credentials">The credentials to authenticate with.</param>
 36    /// <param name="authRoute">The relative authentication route.</param>
 37    /// <exception cref="WebApiClientException">Thrown when the response has no body, is not valid, or carries no token.
 38    protected async Task AuthenticateAndAuthorizeAsync(Credentials credentials, string authRoute)
 39    {
 240        var authentication = await AuthenticateAsync(credentials, authRoute);
 41
 242        if (!authentication.Valid || string.IsNullOrWhiteSpace(authentication.Token))
 43        {
 144            throw new WebApiClientException("Could not authenticate: the authentication response carried no valid token.
 45        }
 46
 147        Authorize(authentication.Token);
 148    }
 49}