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 207 6/30/2025
2.0.149.1 190 6/25/2025
1.1.147.1 1,883 6/23/2025
1.1.146.1 701 6/16/2025
1.1.145.1 351 6/9/2025
1.1.144.1 231 6/2/2025
1.1.143.1 177 5/26/2025
1.1.142.1 212 5/19/2025
1.1.141.1 259 5/12/2025
1.1.140.1 320 5/5/2025
1.1.139.1 588 4/28/2025
1.1.138.1 503 4/26/2025
1.1.137.1 173 4/25/2025
1.1.136.1 714 4/23/2025
1.1.135.1 295 4/21/2025
1.1.134.1 193 4/19/2025
1.1.133.1 116 4/19/2025
1.1.132.1 114 4/19/2025
1.1.131.1 127 4/19/2025
1.1.130.1 355 4/14/2025
1.1.129.1 426 4/7/2025
1.1.128.1 862 3/31/2025
1.1.127.1 634 3/24/2025
1.1.126.1 484 3/17/2025
1.1.125.1 1,226 3/16/2025
1.1.124.1 220 3/10/2025
1.1.123.1 669 3/3/2025
1.1.122.1 836 2/24/2025
1.1.115.1 1,554 1/6/2025
1.1.114.1 172 12/31/2024
1.1.113.1 1,206 12/30/2024
1.1.112.1 413 12/23/2024
1.1.111.1 942 12/16/2024
1.1.110.1 341 12/9/2024
1.1.109.1 289 12/2/2024
1.1.108.1 311 11/25/2024
1.1.107.1 728 11/18/2024
1.1.106.1 815 11/15/2024
1.1.105.1 509 11/11/2024
1.1.104.1 228 11/4/2024
1.1.103.1 1,245 10/28/2024
1.1.102.1 394 10/21/2024
1.1.101.1 419 10/14/2024
1.1.100.1 1,385 10/7/2024
1.1.99.1 575 9/30/2024
1.1.98.1 696 9/23/2024
1.1.97.1 611 9/17/2024
1.1.92.1 745 8/19/2024
1.1.91.1 1,067 8/12/2024
1.1.90.1 635 8/5/2024
1.1.89.1 601 7/29/2024
1.1.88.1 319 7/23/2024
1.1.87.1 551 7/22/2024
1.1.86.1 402 7/15/2024
1.1.85.1 591 7/8/2024
1.1.84.1 661 7/1/2024
1.1.83.1 1,462 6/24/2024
1.1.82.1 555 6/17/2024
1.1.81.1 860 6/10/2024
1.1.80.1 599 6/3/2024
1.1.79.1 720 5/27/2024
1.1.78.1 733 5/20/2024
1.1.77.1 562 5/13/2024
1.1.76.1 730 5/6/2024
1.1.75.1 896 4/29/2024
1.1.74.1 241 4/22/2024
1.1.73.1 268 4/15/2024
1.1.72.1 749 4/8/2024
1.1.71.1 286 4/1/2024
1.1.70.1 846 3/25/2024
1.1.69.1 142 3/18/2024
1.1.68.1 139 3/11/2024
1.1.67.1 622 3/4/2024
1.1.66.1 154 2/26/2024
1.1.65.1 147 2/19/2024
1.1.64.1 156 2/12/2024
1.1.63.1 167 2/7/2024
1.1.62.1 911 2/7/2024
1.1.61.1 675 2/6/2024
1.1.60.1 158 2/6/2024
1.1.59.1 229 2/5/2024
1.1.58.1 180 1/29/2024
1.1.57.1 189 1/22/2024
1.1.56.1 176 1/15/2024
1.1.55.1 212 1/8/2024
1.1.54.1 1,267 1/1/2024
1.1.53.1 292 12/25/2023
1.1.52.1 928 12/18/2023
1.1.51.1 192 12/11/2023
1.1.50.1 1,876 12/4/2023
1.1.49.1 347 11/27/2023
1.1.48.1 303 11/20/2023
1.1.47.1 1,346 11/13/2023
1.1.46.1 281 11/11/2023
1.1.45.1 339 11/11/2023
1.1.44.1 144 11/11/2023
1.1.43.1 146 11/11/2023
1.1.42.1 143 11/11/2023
1.1.41.1 153 11/11/2023
1.1.40.1 141 11/11/2023
1.1.39.1 195 11/9/2023
1.1.38.1 139 11/9/2023
1.1.37.1 144 11/9/2023
1.1.36.1 132 11/9/2023
1.1.35.1 467 11/6/2023
1.1.34.1 359 10/30/2023
1.1.33.1 353 10/27/2023
1.1.32.1 330 10/23/2023
1.1.31.1 330 10/21/2023
1.1.30.1 164 10/21/2023
1.1.29.1 141 10/21/2023
1.1.28.1 161 10/21/2023
1.1.27.1 165 10/20/2023
1.1.26.1 159 10/20/2023
1.1.25.1 206 10/20/2023
1.1.24.1 154 10/20/2023
1.0.23.1 171 10/20/2023