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
<PackageReference Include="MxIO.ApiClient" Version="2.0.150.1" />
<PackageVersion Include="MxIO.ApiClient" Version="2.0.150.1" />
<PackageReference Include="MxIO.ApiClient" />
paket add MxIO.ApiClient --version 2.0.150.1
#r "nuget: MxIO.ApiClient, 2.0.150.1"
#:package MxIO.ApiClient@2.0.150.1
#addin nuget:?package=MxIO.ApiClient&version=2.0.150.1
#tool nuget:?package=MxIO.ApiClient&version=2.0.150.1
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 | Versions 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. |
-
net9.0
- Azure.Identity (>= 1.14.1)
- Microsoft.Extensions.Caching.Abstractions (>= 9.0.6)
- Microsoft.Extensions.Caching.Memory (>= 9.0.6)
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.6)
- Microsoft.Extensions.Http (>= 9.0.6)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.6)
- Microsoft.Extensions.Options (>= 9.0.6)
- MxIO.ApiClient.Abstractions (>= 2.0.150.1)
- Newtonsoft.Json (>= 13.0.3)
- Polly (>= 8.6.1)
- RestSharp (>= 112.1.0)
NuGet packages (6)
Showing the top 5 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.Repository.Api.Client.V1
Versioned client for the XtremeIdiots Portal Repository API V1. |
|
XtremeIdiots.Portal.Repository.Api.Client.V2
Versioned client for the XtremeIdiots Portal Repository API V2. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
2.0.150.1 | 167 | 6/30/2025 |
2.0.149.1 | 167 | 6/25/2025 |
1.1.147.1 | 1,319 | 6/23/2025 |
1.1.146.1 | 691 | 6/16/2025 |
1.1.145.1 | 342 | 6/9/2025 |
1.1.144.1 | 223 | 6/2/2025 |
1.1.143.1 | 170 | 5/26/2025 |
1.1.142.1 | 204 | 5/19/2025 |
1.1.141.1 | 250 | 5/12/2025 |
1.1.140.1 | 313 | 5/5/2025 |
1.1.139.1 | 579 | 4/28/2025 |
1.1.138.1 | 495 | 4/26/2025 |
1.1.137.1 | 164 | 4/25/2025 |
1.1.136.1 | 706 | 4/23/2025 |
1.1.135.1 | 287 | 4/21/2025 |
1.1.134.1 | 184 | 4/19/2025 |
1.1.133.1 | 107 | 4/19/2025 |
1.1.132.1 | 106 | 4/19/2025 |
1.1.131.1 | 120 | 4/19/2025 |
1.1.130.1 | 347 | 4/14/2025 |
1.1.129.1 | 417 | 4/7/2025 |
1.1.128.1 | 853 | 3/31/2025 |
1.1.127.1 | 625 | 3/24/2025 |
1.1.126.1 | 475 | 3/17/2025 |
1.1.125.1 | 1,218 | 3/16/2025 |
1.1.124.1 | 212 | 3/10/2025 |
1.1.123.1 | 662 | 3/3/2025 |
1.1.122.1 | 829 | 2/24/2025 |
1.1.115.1 | 1,547 | 1/6/2025 |
1.1.114.1 | 165 | 12/31/2024 |
1.1.113.1 | 1,197 | 12/30/2024 |
1.1.112.1 | 406 | 12/23/2024 |
1.1.111.1 | 935 | 12/16/2024 |
1.1.110.1 | 334 | 12/9/2024 |
1.1.109.1 | 282 | 12/2/2024 |
1.1.108.1 | 304 | 11/25/2024 |
1.1.107.1 | 721 | 11/18/2024 |
1.1.106.1 | 808 | 11/15/2024 |
1.1.105.1 | 500 | 11/11/2024 |
1.1.104.1 | 220 | 11/4/2024 |
1.1.103.1 | 1,238 | 10/28/2024 |
1.1.102.1 | 387 | 10/21/2024 |
1.1.101.1 | 412 | 10/14/2024 |
1.1.100.1 | 1,378 | 10/7/2024 |
1.1.99.1 | 568 | 9/30/2024 |
1.1.98.1 | 689 | 9/23/2024 |
1.1.97.1 | 604 | 9/17/2024 |
1.1.92.1 | 738 | 8/19/2024 |
1.1.91.1 | 1,060 | 8/12/2024 |
1.1.90.1 | 627 | 8/5/2024 |
1.1.89.1 | 594 | 7/29/2024 |
1.1.88.1 | 312 | 7/23/2024 |
1.1.87.1 | 544 | 7/22/2024 |
1.1.86.1 | 395 | 7/15/2024 |
1.1.85.1 | 584 | 7/8/2024 |
1.1.84.1 | 654 | 7/1/2024 |
1.1.83.1 | 1,456 | 6/24/2024 |
1.1.82.1 | 547 | 6/17/2024 |
1.1.81.1 | 853 | 6/10/2024 |
1.1.80.1 | 592 | 6/3/2024 |
1.1.79.1 | 713 | 5/27/2024 |
1.1.78.1 | 726 | 5/20/2024 |
1.1.77.1 | 555 | 5/13/2024 |
1.1.76.1 | 723 | 5/6/2024 |
1.1.75.1 | 889 | 4/29/2024 |
1.1.74.1 | 234 | 4/22/2024 |
1.1.73.1 | 261 | 4/15/2024 |
1.1.72.1 | 742 | 4/8/2024 |
1.1.71.1 | 279 | 4/1/2024 |
1.1.70.1 | 838 | 3/25/2024 |
1.1.69.1 | 135 | 3/18/2024 |
1.1.68.1 | 133 | 3/11/2024 |
1.1.67.1 | 613 | 3/4/2024 |
1.1.66.1 | 148 | 2/26/2024 |
1.1.65.1 | 141 | 2/19/2024 |
1.1.64.1 | 150 | 2/12/2024 |
1.1.63.1 | 161 | 2/7/2024 |
1.1.62.1 | 905 | 2/7/2024 |
1.1.61.1 | 669 | 2/6/2024 |
1.1.60.1 | 152 | 2/6/2024 |
1.1.59.1 | 223 | 2/5/2024 |
1.1.58.1 | 174 | 1/29/2024 |
1.1.57.1 | 183 | 1/22/2024 |
1.1.56.1 | 170 | 1/15/2024 |
1.1.55.1 | 206 | 1/8/2024 |
1.1.54.1 | 1,260 | 1/1/2024 |
1.1.53.1 | 286 | 12/25/2023 |
1.1.52.1 | 922 | 12/18/2023 |
1.1.51.1 | 186 | 12/11/2023 |
1.1.50.1 | 1,872 | 12/4/2023 |
1.1.49.1 | 343 | 11/27/2023 |
1.1.48.1 | 299 | 11/20/2023 |
1.1.47.1 | 1,342 | 11/13/2023 |
1.1.46.1 | 277 | 11/11/2023 |
1.1.45.1 | 335 | 11/11/2023 |
1.1.44.1 | 140 | 11/11/2023 |
1.1.43.1 | 142 | 11/11/2023 |
1.1.42.1 | 139 | 11/11/2023 |
1.1.41.1 | 149 | 11/11/2023 |
1.1.40.1 | 137 | 11/11/2023 |
1.1.39.1 | 191 | 11/9/2023 |
1.1.38.1 | 135 | 11/9/2023 |
1.1.37.1 | 140 | 11/9/2023 |
1.1.36.1 | 128 | 11/9/2023 |
1.1.35.1 | 463 | 11/6/2023 |
1.1.34.1 | 354 | 10/30/2023 |
1.1.33.1 | 348 | 10/27/2023 |
1.1.32.1 | 325 | 10/23/2023 |
1.1.31.1 | 325 | 10/21/2023 |
1.1.30.1 | 159 | 10/21/2023 |
1.1.29.1 | 136 | 10/21/2023 |
1.1.28.1 | 156 | 10/21/2023 |
1.1.27.1 | 160 | 10/20/2023 |
1.1.26.1 | 154 | 10/20/2023 |
1.1.25.1 | 201 | 10/20/2023 |
1.1.24.1 | 149 | 10/20/2023 |
1.0.23.1 | 166 | 10/20/2023 |