RA.Utilities.Integrations
10.2.0
Prefix Reserved
See the version list below for details.
dotnet add package RA.Utilities.Integrations --version 10.2.0
NuGet\Install-Package RA.Utilities.Integrations -Version 10.2.0
<PackageReference Include="RA.Utilities.Integrations" Version="10.2.0" />
<PackageVersion Include="RA.Utilities.Integrations" Version="10.2.0" />
<PackageReference Include="RA.Utilities.Integrations" />
paket add RA.Utilities.Integrations --version 10.2.0
#r "nuget: RA.Utilities.Integrations, 10.2.0"
#:package RA.Utilities.Integrations@10.2.0
#addin nuget:?package=RA.Utilities.Integrations&version=10.2.0
#tool nuget:?package=RA.Utilities.Integrations&version=10.2.0
RA.Utilities.Integrations
RA.Utilities.Integrations is a utility library that simplifies and standardizes HTTP client calls in .NET applications. It solves the problem of managing external API integrations by providing a robust and repeatable pattern that centralizes configuration, simplifies registration, and improves observability with built-in logging and header-forwarding handlers.
By building on IHttpClientFactory, it promotes best practices like using typed HttpClients, which provides better compile-time safety and intellisense.
Getting started
Install the package via the .NET CLI:
dotnet add package RA.Utilities.Integrations
Or through the NuGet Package Manager console:
Install-Package RA.Utilities.Integrations
π Dependencies
RA.Utilities.Core.ConstantsRA.Utilities.Logging.SharedMicrosoft.AspNetCore.Appshared framework reference
Additional documentation
For more information on how this package fits into the larger RA.Utilities ecosystem, please see the main repository documentation.
π Usage Guide
Hereβs a step-by-step guide to setting up a typed client for an external API.
1. Add Configuration to appsettings.json
Define the settings for your integration. The section name (e.g., "MyApi") will be used later for binding.
{
"Integrations": {
"MyApi": {
"BaseUrl": "https://api.example.com/v1/",
"Timeout": 60,
"Actions": {
"GetProduct": "products/{0}"
}
}
}
}
2. Create a Settings Class
Create a class that inherits from BaseApiSettings<T> to represent your configuration. The generic type argument holds the API action/endpoint names bound from the Actions configuration section.
using RA.Utilities.Integrations.Options;
public class MyApiSettings : BaseApiSettings<MyApiActions>
{
// You can add custom properties specific to this integration if needed.
}
public class MyApiActions
{
public string GetProduct { get; set; } = string.Empty;
}
3. Create a Typed HttpClient
This is the client your application will use to communicate with the API. It takes HttpClient in its constructor, which will be configured and provided by the IHttpClientFactory.
using System.Text.Json;
public interface IMyApiClient
{
Task<Product?> GetProductAsync(int id);
}
public class MyApiClient : IMyApiClient
{
private readonly HttpClient _httpClient;
private readonly ILogger<MyApiClient> _logger;
public MyApiClient(HttpClient httpClient, ILogger<MyApiClient> logger)
{
_httpClient = httpClient;
_logger = logger;
}
public async Task<Product?> GetProductAsync(int id)
{
try
{
var response = await _httpClient.GetAsync($"products/{id}");
response.EnsureSuccessStatusCode();
var content = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Product>(content);
}
catch (HttpRequestException ex)
{
_logger.LogError(ex, "Failed to retrieve product with ID {ProductId}.", id);
return null;
}
}
}
// Example DTO
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
4. Register the Client in Program.cs
Use the AddHttpClientIntegration extension method to register your typed client and bind its settings.
using RA.Utilities.Integrations.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Register the typed client for "MyApi"
builder.Services.AddHttpClientIntegration<IMyApiClient, MyApiClient, MyApiSettings>(
builder.Configuration.GetSection("Integrations:MyApi")
);
// ... other services
var app = builder.Build();
5. Use the Client in Your Services
Inject your typed client interface (IMyApiClient) wherever you need to call the external API.
public class ProductService
{
private readonly IMyApiClient _myApiClient;
public ProductService(IMyApiClient myApiClient)
{
_myApiClient = myApiClient;
}
public async Task<Product?> GetProductDetails(int productId)
{
return await _myApiClient.GetProductAsync(productId);
}
}
Strongly-typed query & header parameters
BaseHttpClient supports all four HTTP verbs β GetAsync, PostAsync, PutAsync and DeleteAsync β with optional, strongly-typed query string and header parameters. Mark a partial class with [QueryParameters] or [HeaderParameters] and a source generator implements the parameter contract for you, mapping each public property to a key-value pair. Null property values are skipped; non-nullable value types are always included. Use [QueryParameterName("...")] or [HeaderParameterName("...")] on a property to override the emitted key or header name.
using RA.Utilities.Integrations;
using RA.Utilities.Integrations.Attributes;
[QueryParameters]
public partial class GetProductsQuery
{
[QueryParameterName("category_id")]
public int? CategoryId { get; init; }
public string? Search { get; init; }
public int[]? Ids { get; init; }
}
[HeaderParameters]
public partial class RequestHeaders
{
[HeaderParameterName("x-request-id")]
public string? XCorrelationId { get; init; }
}
public class Product
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
}
public class ProductsClient : BaseHttpClient
{
public ProductsClient(HttpClient httpClient, IOptions<IIntegrationSettings> settings)
: base(httpClient, settings)
{
}
public Task<Product?> GetProductsAsync(GetProductsQuery query) =>
GetAsync<Product>("products", query, new RequestHeaders { XCorrelationId = "..." });
public Task<Product?> CreateProductAsync(Product product) =>
PostAsync<Product, Product>("products", product);
public Task<Product?> UpdateProductAsync(Product product) =>
PutAsync<Product, Product>($"products/{product.Id}", product);
public Task DeleteProductAsync(int id) =>
DeleteAsync($"products/{id}");
}
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. 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. |
-
net10.0
- RA.Utilities.Core.Constants (>= 10.1.0)
- RA.Utilities.Logging.Shared (>= 10.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.