Provides a cached HttpClient configured for one Zendesk account and authentication credential.
dotnet add package Soenneker.Zendesk.HttpClientsFor API-token authentication, Base64-encode the UTF-8 value {email_address}/token:{api_token} and supply the result as Credentials:
{
"Zendesk": {
"ClientBaseUrl": "https://acme.zendesk.com/",
"Credentials": "base64-encoded-credentials"
}
}The default header is Authorization: Basic {token}. OAuth can be configured without changing code:
{
"Zendesk": {
"ClientBaseUrl": "https://acme.zendesk.com/",
"Credentials": "oauth-access-token",
"AuthHeaderValueTemplate": "Bearer {token}"
}
}AuthHeaderName can override the header name when required. ClientBaseUrl should be the account origin; generated API paths already include /api/v2.
using Soenneker.Zendesk.HttpClients.Registrars;
services.AddZendeskOpenApiHttpClientAsSingleton();Scoped registration is also available:
services.AddZendeskOpenApiHttpClientAsScoped();public sealed class ZendeskTicketReader
{
private readonly IZendeskOpenApiHttpClient _zendesk;
public ZendeskTicketReader(IZendeskOpenApiHttpClient zendesk)
{
_zendesk = zendesk;
}
public async Task<string> GetTickets(CancellationToken cancellationToken)
{
HttpClient client = await _zendesk.Get(cancellationToken);
return await client.GetStringAsync("api/v2/tickets.json", cancellationToken);
}
}Each provider owns its cached named client. Disposing the provider removes that client from the shared cache; disposing the cache itself remains the responsibility of its DI lifetime.