TestingCommons.RestApiClient
1.0.3
dotnet add package TestingCommons.RestApiClient --version 1.0.3
NuGet\Install-Package TestingCommons.RestApiClient -Version 1.0.3
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="TestingCommons.RestApiClient" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TestingCommons.RestApiClient" Version="1.0.3" />
<PackageReference Include="TestingCommons.RestApiClient" />
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 TestingCommons.RestApiClient --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TestingCommons.RestApiClient, 1.0.3"
#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 TestingCommons.RestApiClient@1.0.3
#: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=TestingCommons.RestApiClient&version=1.0.3
#tool nuget:?package=TestingCommons.RestApiClient&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
TestingCommons.RestApiClient
The RestApiClient project provides a base class for creating HTTP clients with optional Azure AD authentication. It simplifies the creation of REST API clients for testing scenarios by handling common configuration and authentication patterns.
Key Components
RestClientBase
- Abstract base class for REST API clients
- Pre-configured HttpClient with base URL
- Optional Azure AD authentication integration
- Two constructor overloads: with and without authentication
IRestClientOptions
- Interface defining configuration options for REST clients
- Contains BaseUrl property for API endpoint configuration
- Designed for dependency injection and configuration binding
Usage Examples
Basic REST Client Without Authentication
Configuration:
// appsettings.json
{
"ApiSettings": {
"BaseUrl": "https://api.example.com/"
}
}
// Options class
public class ApiOptions : IRestClientOptions
{
public string BaseUrl { get; set; }
}
// Service registration
services.Configure<ApiOptions>(configuration.GetSection("ApiSettings"));
services.AddTransient<MyApiClient>();
Client Implementation:
public class MyApiClient : RestClientBase
{
public MyApiClient(IOptions<ApiOptions> options) : base(options)
{
}
public async Task<User> GetUserAsync(int userId)
{
var response = await Client.GetAsync($"users/{userId}");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<User>(json);
}
public async Task<User> CreateUserAsync(User user)
{
var json = JsonSerializer.Serialize(user);
var content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await Client.PostAsync("users", content);
response.EnsureSuccessStatusCode();
var responseJson = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<User>(responseJson);
}
}
REST Client with Azure AD Authentication
Configuration:
// appsettings.json
{
"ApiSettings": {
"BaseUrl": "https://secure-api.example.com/"
},
"AzureAd": {
"TenantId": "your-tenant-id",
"ClientId": "your-client-id",
"ClientSecret": "your-client-secret",
"Scope": ["https://secure-api.example.com/.default"]
}
}
// Service registration
services.Configure<ApiOptions>(configuration.GetSection("ApiSettings"));
services.AddAzureAdAuthenticator(configuration);
services.AddTransient<SecureApiClient>();
Authenticated Client Implementation:
public class SecureApiClient : RestClientBase
{
public SecureApiClient(IOptions<ApiOptions> options, AzureAdAuthenticator authenticator)
: base(options, authenticator)
{
}
public async Task<Order> GetOrderAsync(int orderId)
{
// Add authentication header
await AddAuthenticationHeaderAsync();
var response = await Client.GetAsync($"orders/{orderId}");
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<Order>(json);
}
private async Task AddAuthenticationHeaderAsync()
{
if (Authenticator != null)
{
var token = await Authenticator.GetAccessTokenAsync();
Client.DefaultRequestHeaders.Authorization =
new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
}
}
}
Key Features
- Base URL Configuration: Automatic base URL setup for consistent API calls
- Optional Authentication: Support for Azure AD authentication when needed
- HttpClient Management: Pre-configured HttpClient with proper disposal
- Flexible Construction: Two constructor overloads for different scenarios
- Configuration-Driven: Easy setup through dependency injection
- Extensible Design: Abstract base class allows for custom implementations
- Authentication Integration: Seamless integration with Azure AD authenticator
- Testing-Friendly: Designed specifically for testing scenarios
Design Patterns
- Template Method: Base class provides structure, derived classes implement specifics
- Dependency Injection: Full support for .NET DI container
- Options Pattern: Configuration through strongly-typed options classes
- Factory Pattern: HttpClient creation and configuration handled in base class
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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.
-
net8.0
- TestingCommons.Azure (>= 1.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.