RA.Utilities.Integrations 10.2.0

Prefix Reserved
There is a newer version of this package available.
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
                    
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="RA.Utilities.Integrations" Version="10.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RA.Utilities.Integrations" Version="10.2.0" />
                    
Directory.Packages.props
<PackageReference Include="RA.Utilities.Integrations" />
                    
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 RA.Utilities.Integrations --version 10.2.0
                    
#r "nuget: RA.Utilities.Integrations, 10.2.0"
                    
#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 RA.Utilities.Integrations@10.2.0
                    
#: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=RA.Utilities.Integrations&version=10.2.0
                    
Install as a Cake Addin
#tool nuget:?package=RA.Utilities.Integrations&version=10.2.0
                    
Install as a Cake Tool

RA.Utilities.Integrations

NuGet version Codecov NuGet Downloads Documentation GitHub license

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

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.2.1 130 10/19/2026
10.2.0 99 10/18/2026
10.1.1 102 10/10/2026
10.1.0 115 9/28/2026
10.0.0 268 11/23/2025
10.0.0-rc.2 172 10/31/2025