/// <summary> /// HTTP GET to the specified URI /// </summary> /// <typeparam name="T">The type of the object to read.</typeparam> /// <param name="uri">The URI to the end point you wish to interact with.</param> /// <returns>A Task yielding the result (of type T).</returns> /// <exception cref="RoatpApiClientException">Thrown if there was an unsuccessful response.</exception> /// <exception cref="HttpRequestException">Thrown if something unexpected occurred when sending the request.</exception> protected async Task <T> Get <T>(string uri) where T : new() { _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _tokenService.GetToken()); try { using (var response = await _client.GetAsync(new Uri(uri, UriKind.Relative))) { await LogErrorIfUnsuccessfulResponse(response); ThrowExceptionIfUnsuccessfulResponse(response); return(await response.Content.ReadAsAsync <T>()); } } catch (HttpRequestException ex) { _logger.LogError(ex, $"Error when processing request: {HttpMethod.Get} - {uri}"); throw; } }
public RoatpApiClient(HttpClient httpClient, ILogger <RoatpApiClient> logger, IRoatpTokenService tokenService) : base(httpClient, logger) { _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenService.GetToken(httpClient.BaseAddress)); }