Soenneker.Zendesk.HttpClients

Provides a cached HTTP client configured for a Zendesk account and authentication credential.


Keywords
.net, api, c#, client, csharp, dotnet, httpclient, httpclients, openapi, singleton, zendesk, zendeskopenapihttpclient
License
MIT
Install
Install-Package Soenneker.Zendesk.HttpClients -Version 4.0.31

Documentation

Soenneker.Zendesk.HttpClients

Provides a cached HttpClient configured for one Zendesk account and authentication credential.

Install

dotnet add package Soenneker.Zendesk.HttpClients

Configuration

For 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.

Registration

using Soenneker.Zendesk.HttpClients.Registrars;

services.AddZendeskOpenApiHttpClientAsSingleton();

Scoped registration is also available:

services.AddZendeskOpenApiHttpClientAsScoped();

Usage

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.