Cabazure.Client
0.3.4
See the version list below for details.
dotnet add package Cabazure.Client --version 0.3.4
NuGet\Install-Package Cabazure.Client -Version 0.3.4
<PackageReference Include="Cabazure.Client" Version="0.3.4" />
paket add Cabazure.Client --version 0.3.4
#r "nuget: Cabazure.Client, 0.3.4"
// Install Cabazure.Client as a Cake Addin #addin nuget:?package=Cabazure.Client&version=0.3.4 // Install Cabazure.Client as a Cake Tool #tool nuget:?package=Cabazure.Client&version=0.3.4
Cabazure.Client
The Cabazure.Client is a library for creating .NET Clients for your AspNetCore REST APIs, using Source Generators directed by attributes.
The main design choices in the Cabazure.Client are:
- Code is used to describe the API, rather than an api specification
- Contracts are shared via library used by both Client and Server
- Endpoints are represented as interfaces, for easy mocking in unit testing
- Dependency Injection is used for registering endpoints
- Azure.Identity is used for Authentication
Getting started
It is recommended to keep your contract data types in a separate library, for easy sharing between your AspNetCore project and your Client project.
Ensure your Client project has a reference to the contract data types used by your service and the Cabazure.Client package.
Adding an endpoint
Endpoints are added by creating interfaces, decorated with attributes like this:
[ClientEndpoint("CustomerClient")]
public interface IGetCustomerEndpoint
{
[Get("/v1/customers/{customerId}")]
public Task<EndpointResponse<Customer>> ExecuteAsync(
[Path("customerId")] string customerId,
ClientRequestOptions options,
CancellationToken cancellationToken);
}
The [ClientEndpoint]
attribute declares that this is an endpoint, that should have the implementation generated. The client name ("CustomerClient"
), is used for identifying which HttpClient
instance name this endpoint should use.
The [Get]
attribute declares that the interface method is targeting a GET endpoint on the specified path. The path can have place holders like {customerId}
which can be referenced by one of the method parameters. The following HTTP methods are supported by corresponding attributes: [Get]
, [Post]
, [Put]
or [Delete]
.
The return type of Task<EndpointResponse<Customer>>
, is a wrapper for the actual contract data type Customer
. The following return type wrappers are supported:
Type | Description |
---|---|
Task<EndpointResponse> |
Used when there is no response content |
Task<EndpointResponse<T>> |
Used for endpoints with a response object |
Task<PagedResponse<T[]>> |
Used for endpoints with a paged response |
The [Path]
attribute on the customerId
parameter of the endpoint method, declares that this parameter corresponds to the endpoint path placeholder. Parameters containing data for an endpoint method should have one of the following attributes describing how they are passed to the endpoint: [Path]
, [Query]
, [Header]
or [Body]
.
Apart from the data parameters, the following parameter types are supported:
Parameter Type | Description |
---|---|
ClientRequestOptions |
Allowing the caller to specify further request options. |
ClientPaginationOptions |
Allowing the caller to specify further request and pagination options. |
CancellationToken |
Allowing the caller to cancel the async call to the endpoint. |
Adding the bootstrap
To register relevant dependencies for the Client, the generated AddCabazureClient
extension method on IServiceCollection
needs to be called. This will register all the endpoints for the specified HttpClient
instance as singletons in the IServiceCollection
.
To make it easy for the user of the Client, it is recommended to do this in a bootstrap method like this:
namespace Microsoft.Extensions.DependencyInjection;
public static class ServiceCollectionExtensions
{
public static void AddCustomerClient(
this IServiceCollection services)
=> services.AddCabazureClient(
"CustomerClient",
b => b
.SetBaseAddress(new Uri("https://customer-api.contoso.com"))
.AddAuthentication(
scope: "app://contoso.com/customer-api/.default",
credential: new DefaultAzureCredential()),
j => j.PropertyNamingPolicy = JsonNamingPolicy.CamelCase);
}
The call to AddCabazureClient
needs the following:
- The name of the
HttpClient
instance (matching the one specified on the endpoints) - Configuration of the
HttpClient
(using anIHttpClientBuilder
) - Configuration of the
JsonSerializerOptions
used for serialization of the contracts
Using the client
To use the Client library, the bootstrap method should be called during composition of the Hosting app, like this:
builder.Services.AddCustomerClient();
An endpoint can be constructor injected by adding the endpoint interface as a constructor parameter, like this:
public class CustomerNameProvider(
IGetCustomerEndpoint endpoint)
{
public async Task<string?> GetCustomerNameAsync(
string customerId,
CancellationToken cancellationToken)
=> await endpoint.ExecuteAsync(
customerId,
new ClientRequestOptions(),
cancellationToken)
switch
{
{ OkContent: { } c } => c.Name,
_ => null,
};
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Azure.Core (>= 1.40.0)
- Microsoft.Extensions.Http (>= 8.0.0)
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 |
---|---|---|
1.0.0 | 163 | 10/22/2024 |
1.0.0-preview1 | 54 | 10/22/2024 |
0.9.0 | 323 | 10/8/2024 |
0.8.0 | 404 | 8/9/2024 |
0.7.1 | 100 | 8/7/2024 |
0.7.0 | 91 | 8/7/2024 |
0.7.0-preview1 | 89 | 8/7/2024 |
0.6.2 | 73 | 8/6/2024 |
0.6.1 | 68 | 8/6/2024 |
0.6.0 | 65 | 8/6/2024 |
0.5.1 | 70 | 8/5/2024 |
0.5.0 | 72 | 8/5/2024 |
0.5.0-preview2 | 104 | 7/8/2024 |
0.5.0-preview1 | 98 | 7/8/2024 |
0.4.2 | 107 | 7/2/2024 |
0.4.1 | 102 | 6/27/2024 |
0.4.0 | 111 | 6/27/2024 |
0.3.4 | 108 | 6/26/2024 |