MxIO.ApiClient 2.0.150.1

dotnet add package MxIO.ApiClient --version 2.0.150.1
                    
NuGet\Install-Package MxIO.ApiClient -Version 2.0.150.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MxIO.ApiClient" Version="2.0.150.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MxIO.ApiClient" Version="2.0.150.1" />
                    
Directory.Packages.props
<PackageReference Include="MxIO.ApiClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MxIO.ApiClient --version 2.0.150.1
                    
#r "nuget: MxIO.ApiClient, 2.0.150.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MxIO.ApiClient@2.0.150.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MxIO.ApiClient&version=2.0.150.1
                    
Install as a Cake Addin
#tool nuget:?package=MxIO.ApiClient&version=2.0.150.1
                    
Install as a Cake Tool

MxIO.ApiClient

This library provides a base implementation for creating strongly typed API clients with standardized error handling, authentication, and resilience features.

Features

  • Support for multiple authentication methods (API Key and Entra ID authentication)
  • Automatic token acquisition and caching
  • Built-in retry policies with exponential backoff
  • Thread-safe REST client management
  • Standardized error handling and response processing
  • Support for primary/secondary API key failover

Installation

dotnet add package MxIO.ApiClient

Usage

Basic Setup

// Register the API client services
services.AddApiClient()
    .WithApiKeyAuthentication("your-api-key");

// Or with Entra ID authentication
services.AddApiClient()
    .WithEntraIdAuthentication("api://your-api-audience");

// Configure client options
services.Configure<ApiClientOptions>(options =>
{
    options.BaseUrl = "https://api.example.com";
    options.ApiPathPrefix = "v2";
    options.MaxRetryCount = 3;
});

Creating a Custom API Client

// Inherit from BaseApi
public class MyApiClient : BaseApi
{
    private readonly ILogger<MyApiClient> logger;

    public MyApiClient(
        ILogger<MyApiClient> logger,
        IApiTokenProvider apiTokenProvider,
        IRestClientService restClientService,
        IOptions<ApiClientOptions> options)
        : base(logger, apiTokenProvider, restClientService, options)
    {
        this.logger = logger;
    }

    // Implement custom API methods
    public async Task<ApiResponse<ResourceDto>> GetResourceAsync(string id, CancellationToken cancellationToken = default)
    {
        try
        {
            var request = await CreateRequestAsync($"resources/{id}", Method.Get, cancellationToken);
            var response = await ExecuteAsync(request, false, cancellationToken);
            
            return response.ToApiResponse<ResourceDto>();
        }
        catch (Exception ex) when (ex is not OperationCanceledException)
        {
            logger.LogError(ex, "Failed to retrieve resource with ID {ResourceId}", id);
            return HttpStatusCode.InternalServerError.CreateResponse<ResourceDto>("An unexpected error occurred");
        }
    }
}

Updating API Key at Runtime

// Inject IOptions<ApiClientOptions> and update
var options = apiClientOptions.Value;
options.PrimaryApiKey = "new-api-key";

Authentication Methods

API Key Authentication

Use this when your API requires an API key in a header (like Azure API Management).

services.AddApiClient()
    .WithApiKeyAuthentication("your-api-key", "X-API-Key"); // Custom header name

Entra ID Authentication

Use this when your API requires OAuth tokens from Entra ID (formerly Azure AD).

services.AddApiClient()
    .WithEntraIdAuthentication("api://your-api-audience");

With custom credential options:

services.AddApiClient()
    .WithEntraIdAuthentication("api://your-api-audience", options => 
    {
        options.ExcludeManagedIdentityCredential = true;
        // Other DefaultAzureCredentialOptions
    });

Error Handling and Resilience

The library includes built-in retry policies with exponential backoff for transient failures:

services.Configure<ApiClientOptions>(options =>
{
    options.MaxRetryCount = 3; // Configure retry count (default is 3)
});

You can also customize the retry behavior:

// Custom retry policy
services.AddApiClient()
    .WithCustomRetryPolicy(retryCount => 
        Policy
            .Handle<HttpRequestException>()
            .OrResult<RestResponse>(r => r.StatusCode == HttpStatusCode.TooManyRequests)
            .WaitAndRetryAsync(
                retryCount, 
                retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)) + 
                                TimeSpan.FromMilliseconds(Random.Shared.Next(0, 1000))
            )
    );

Advanced Usage

Working with Collections and Filtering

public async Task<ApiResponse<CollectionModel<ResourceDto>>> GetResourcesAsync(FilterOptions filter, CancellationToken cancellationToken = default)
{
    try
    {
        var request = await CreateRequestAsync("resources", Method.Get, cancellationToken);
        
        // Add filter options to request
        request.AddFilterOptions(filter);
        
        var response = await ExecuteAsync(request, false, cancellationToken);
        return response.ToApiResponse<CollectionModel<ResourceDto>>();
    }
    catch (Exception ex) when (ex is not OperationCanceledException)
    {
        logger.LogError(ex, "Failed to retrieve resources");
        return HttpStatusCode.InternalServerError.CreateResponse<CollectionModel<ResourceDto>>("An unexpected error occurred");
    }
}
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on MxIO.ApiClient:

Package Downloads
MX.GeoLocation.GeoLocationApi.Client

This package provides a web service client to query the geolocation service.

XtremeIdiots.Portal.RepositoryApiClient

Client for the XtremeIdiots Portal Repository API.

XtremeIdiots.Portal.ServersApiClient

Client for the XtremeIdiots Portal Servers API.

XtremeIdiots.Portal.RepositoryApiClient.V1

Versioned client for the XtremeIdiots Portal Repository API V1.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.150.1 251 6/30/2025
2.0.149.1 230 6/25/2025
1.1.147.1 2,563 6/23/2025
1.1.146.1 978 6/16/2025
1.1.145.1 401 6/9/2025
1.1.144.1 274 6/2/2025
1.1.143.1 205 5/26/2025
1.1.142.1 250 5/19/2025
1.1.141.1 298 5/12/2025
1.1.140.1 376 5/5/2025
1.1.139.1 667 4/28/2025
1.1.138.1 597 4/26/2025
1.1.137.1 228 4/25/2025
1.1.136.1 832 4/23/2025
1.1.135.1 351 4/21/2025
1.1.134.1 240 4/19/2025
1.1.133.1 154 4/19/2025
1.1.132.1 153 4/19/2025
1.1.131.1 166 4/19/2025
1.1.130.1 400 4/14/2025
1.1.129.1 470 4/7/2025
1.1.128.1 913 3/31/2025
1.1.127.1 674 3/24/2025
1.1.126.1 530 3/17/2025
1.1.125.1 1,419 3/16/2025
1.1.124.1 258 3/10/2025
1.1.123.1 719 3/3/2025
1.1.122.1 972 2/24/2025
1.1.115.1 1,743 1/6/2025
1.1.114.1 209 12/31/2024
1.1.113.1 1,397 12/30/2024
1.1.112.1 459 12/23/2024
1.1.111.1 1,100 12/16/2024
1.1.110.1 381 12/9/2024
1.1.109.1 329 12/2/2024
1.1.108.1 351 11/25/2024
1.1.107.1 791 11/18/2024
1.1.106.1 1,007 11/15/2024
1.1.105.1 567 11/11/2024
1.1.104.1 274 11/4/2024
1.1.103.1 1,420 10/28/2024
1.1.102.1 479 10/21/2024
1.1.101.1 482 10/14/2024
1.1.100.1 1,577 10/7/2024
1.1.99.1 644 9/30/2024
1.1.98.1 824 9/23/2024
1.1.97.1 749 9/17/2024
1.1.92.1 823 8/19/2024
1.1.91.1 1,215 8/12/2024
1.1.90.1 715 8/5/2024
1.1.89.1 730 7/29/2024
1.1.88.1 401 7/23/2024
1.1.87.1 650 7/22/2024
1.1.86.1 476 7/15/2024
1.1.85.1 674 7/8/2024
1.1.84.1 920 7/1/2024
1.1.83.1 1,755 6/24/2024
1.1.82.1 647 6/17/2024
1.1.81.1 1,053 6/10/2024
1.1.80.1 720 6/3/2024
1.1.79.1 831 5/27/2024
1.1.78.1 876 5/20/2024
1.1.77.1 648 5/13/2024
1.1.76.1 862 5/6/2024
1.1.75.1 1,231 4/29/2024
1.1.74.1 286 4/22/2024
1.1.73.1 314 4/15/2024
1.1.72.1 822 4/8/2024
1.1.71.1 332 4/1/2024
1.1.70.1 1,004 3/25/2024
1.1.69.1 179 3/18/2024
1.1.68.1 178 3/11/2024
1.1.67.1 708 3/4/2024
1.1.66.1 191 2/26/2024
1.1.65.1 184 2/19/2024
1.1.64.1 193 2/12/2024
1.1.63.1 205 2/7/2024
1.1.62.1 996 2/7/2024
1.1.61.1 801 2/6/2024
1.1.60.1 195 2/6/2024
1.1.59.1 283 2/5/2024
1.1.58.1 217 1/29/2024
1.1.57.1 228 1/22/2024
1.1.56.1 214 1/15/2024
1.1.55.1 239 1/8/2024
1.1.54.1 1,530 1/1/2024
1.1.53.1 383 12/25/2023
1.1.52.1 1,067 12/18/2023
1.1.51.1 229 12/11/2023
1.1.50.1 2,118 12/4/2023
1.1.49.1 427 11/27/2023
1.1.48.1 348 11/20/2023
1.1.47.1 1,490 11/13/2023
1.1.46.1 326 11/11/2023
1.1.45.1 414 11/11/2023
1.1.44.1 179 11/11/2023
1.1.43.1 181 11/11/2023
1.1.42.1 178 11/11/2023
1.1.41.1 189 11/11/2023
1.1.40.1 176 11/11/2023
1.1.39.1 240 11/9/2023
1.1.38.1 174 11/9/2023
1.1.37.1 179 11/9/2023
1.1.36.1 167 11/9/2023
1.1.35.1 612 11/6/2023
1.1.34.1 439 10/30/2023
1.1.33.1 422 10/27/2023
1.1.32.1 422 10/23/2023
1.1.31.1 435 10/21/2023
1.1.30.1 204 10/21/2023
1.1.29.1 167 10/21/2023
1.1.28.1 196 10/21/2023
1.1.27.1 200 10/20/2023
1.1.26.1 185 10/20/2023
1.1.25.1 247 10/20/2023
1.1.24.1 190 10/20/2023
1.0.23.1 206 10/20/2023