< Summary

Information
Class: ArturRios.Util.WebApi.Client.BaseWebApiClientRoute
Assembly: ArturRios.Util.WebApi
File(s): D:\Repositories\dotnet-webapi-util\src\Client\BaseWebApiClientRoute.cs
Line coverage
27%
Covered lines: 3
Uncovered lines: 8
Coverable lines: 11
Total lines: 43
Line coverage: 27.2%
Branch coverage
0%
Covered branches: 0
Total branches: 2
Branch coverage: 0%
Method coverage
50%
Covered methods: 2
Fully covered methods: 1
Total methods: 4
Method coverage: 50%
Full method coverage: 25%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
AuthenticateAsync()0%620%
Authorize(...)100%11100%
AuthenticateAndAuthorizeAsync()100%210%

File(s)

D:\Repositories\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>
 19public abstract class BaseWebApiClientRoute(HttpGateway gateway)
 10{
 11    /// <summary>The HTTP gateway used to issue requests to this route group.</summary>
 112    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)
 023    {
 024        var output = await Gateway.PostAsync<Authentication>(authRoute, credentials);
 25
 026        return output.Body ?? throw new WebApiClientException("Could not authenticate: the authentication response conta
 027    }
 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) =>
 232        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    protected async Task AuthenticateAndAuthorizeAsync(Credentials credentials, string authRoute)
 038    {
 039        var authentication = await AuthenticateAsync(credentials, authRoute);
 40
 041        Authorize(authentication.Token!);
 042    }
 43}