-
Notifications
You must be signed in to change notification settings - Fork 10
Description
Description
Some REST APIs will specify a specific API server instance after authenticating and will expect all API requests to use that server.
For instance, authentication may take place at auth.example.com but the upon successful authentication, you are directed to send API queries to api-na1.example.com.
The ability to customize the base address should be added.
_restClient.WithCustomization
(
x =>
{
x.BaseAddress = authResponse.ResourceServerBaseUri;
x.AddHeader("Authorization", $"bearer {authResponse.AccessToken}");
}
);Why This is Needed
Without the ability to customize the base address, it places a requirement on the user to fully qualify all endpoint calls, which becomes difficult when the directed API base address changes between calls.
Alternatives Considered
One option currently is for the user to do something like this, where _cache is an IMemoryCache and IAuthenticationResponse.ResourceServerBaseUri is the API base address I was directed to use. However, for obvious reasons, this is less than ideal.
/// <summary>
/// Returns a list of agents currently configured in the business unit.
/// </summary>
/// <param name="startDate">The earliest date to look for agents.</param>
/// <param name="cancellationToken">A cancellation token for this operation.</param>
/// <returns>A list of agents from the API.</returns>
public async Task<Result<IGetAgentsResult>> DownloadAgentListAsync(DateTime startDate, CancellationToken cancellationToken = default)
{
var currentAuthSession = _cache.Get<IAuthenticationResponse>(InContactAuthResponse);
return await _restClient.GetAsync<IGetAgentsResult>
(
$"{currentAuthSession.ResourceServerBaseUri}inContactAPI/services/{_config.ApiVersion}/agents",
builder =>
{
builder.WithJson(json =>
{
json.Write("updatedSince", startDate.ToString("O", CultureInfo.InvariantCulture), _jsonOptions);
json.Write("isActive", true, _jsonOptions);
json.Write("searchString", _optionalString, _jsonOptions);
json.Write("fields", "agentId, userName, firstName, lastName, userId, teamId", _jsonOptions);
});
},
ct: cancellationToken
);
}