in provisioning/service/src/Contract/ContractApiHttp.cs [81:161]
public async Task<ContractApiResponse> RequestAsync(
HttpMethod httpMethod,
Uri requestUri,
IDictionary<string, string> customHeaders,
string body,
string ifMatch,
CancellationToken cancellationToken)
{
ContractApiResponse response;
using (var msg = new HttpRequestMessage(httpMethod, requestUri))
{
if (!string.IsNullOrEmpty(body))
{
msg.Content = new StringContent(body, Encoding.UTF8, MediaTypeForDeviceManagementApis);
}
msg.Headers.Add(HttpRequestHeader.Authorization.ToString(), _authenticationHeaderProvider.GetAuthorizationHeader());
msg.Headers.Add(HttpRequestHeader.UserAgent.ToString(), Utils.GetClientVersion());
if (customHeaders != null)
{
foreach (KeyValuePair<string, string> header in customHeaders)
{
msg.Headers.Add(header.Key, header.Value);
}
}
InsertIfMatch(msg, ifMatch);
try
{
using HttpResponseMessage httpResponse = await _httpClientObj.SendAsync(msg, cancellationToken).ConfigureAwait(false);
if (httpResponse == null)
{
throw new ProvisioningServiceClientTransportException(
$"The response message was null when executing operation {httpMethod}.");
}
response = new ContractApiResponse(
await httpResponse.Content.ReadHttpContentAsStringAsync(cancellationToken).ConfigureAwait(false),
httpResponse.StatusCode,
httpResponse.Headers.ToDictionary(x => x.Key, x => x.Value.FirstOrDefault()),
httpResponse.ReasonPhrase);
}
catch (AggregateException ex)
{
ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions;
if (innerExceptions.Any(e => e is TimeoutException))
{
throw new ProvisioningServiceClientTransportException(ex.Message, ex);
}
throw;
}
catch (TimeoutException ex)
{
throw new ProvisioningServiceClientTransportException(ex.Message, ex);
}
catch (IOException ex)
{
throw new ProvisioningServiceClientTransportException(ex.Message, ex);
}
catch (HttpRequestException ex)
{
throw new ProvisioningServiceClientTransportException(ex.Message, ex);
}
catch (TaskCanceledException ex)
{
// Unfortunately TaskCanceledException is thrown when HttpClient times out.
if (cancellationToken.IsCancellationRequested)
{
throw new ProvisioningServiceClientException(ex.Message, ex);
}
throw new ProvisioningServiceClientTransportException($"The {httpMethod} operation timed out.", ex);
}
}
ValidateHttpResponse(response);
return response;
}