Setting up HttpClient
Configure a reusable .NET HttpClient for authenticated Efimis REST API calls.
Configure the client
Use a single HttpClient with the tenant base URL and a bearer token from the OAuth 2.0 client credentials flow.
The base URL includes the region and tenant, for example https://api.uk.efimis.com/acme/.
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Text.Json;
var token = await RequestAccessToken();
using var client = new HttpClient
{
BaseAddress = new Uri("https://api.uk.efimis.com/{tenant}/")
};
client.DefaultRequestHeaders.Authorization =
new AuthenticationHeaderValue("Bearer", token);
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
async Task<T> Send<T>(HttpRequestMessage request, Func<JsonDocument, T> read)
{
using var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
await using var stream = await response.Content.ReadAsStreamAsync();
using var result = await JsonDocument.ParseAsync(stream);
return read(result);
} Send requests
Build relative requests such as v1/entities or v1/matters, then pass them through a helper that checks the HTTP status and reads the JSON response.
Reuse the client
Keep one configured client for the lifetime of your integration worker. Refresh the bearer token when it expires, but avoid creating a new HTTP client for every request.