/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="restRequest"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private async Task <HttpResponseMessage> GetHttpResponseMessage <T>(RestRequest restRequest, CancellationToken cancellationToken = default(CancellationToken))
{
//RWM If we've specified a DateFormat for the Client, but not not the Request, pass it down.
if (!string.IsNullOrWhiteSpace(DateFormat) && string.IsNullOrWhiteSpace(restRequest.DateFormat))
{
restRequest.DateFormat = DateFormat;
}
//RWM: If we've specified JsonSerializerSettings for the Client, but not not the Request, pass it down.
if (JsonSerializerSettings != null && restRequest.JsonSerializerSettings == null)
{
restRequest.JsonSerializerSettings = JsonSerializerSettings;
}
//RWM: We've moved this call to inside the HttpHandler setter... let's see if that solves our Mono problems.
//ConfigureHandler(HttpHandler);
_client = new HttpClient(HttpHandler);
if (string.IsNullOrWhiteSpace(UserAgent))
{
SetUserAgent <T>();
}
_client.DefaultRequestHeaders.Add("user-agent", UserAgent);
var message = new HttpRequestMessage(restRequest.Method, restRequest.GetResourceUri(BaseUrl));
//RWM: Add the global headers for all requests.
foreach (var header in Headers)
{
message.Headers.Add(header.Key, header.Value);
}
//RWM: Add request-specific headers.
foreach (var header in restRequest.Headers)
{
message.Headers.Add(header.Key, header.Value.ToString());
}
//RWM: Not sure if this is sufficient, or if HEAD supports a body, will need to check into the RFC.
if (restRequest.Method != HttpMethod.Get && restRequest.Method != HttpMethod.Head && restRequest.Method != HttpMethod.Trace)
{
//RWM: This feels hacky. May need some tweaking.
if (restRequest.ContentType == ContentTypes.ByteArray)
{
//RWM: A fix for an issue uncovered by @scottisafool.
if (restRequest.Parameters.Count > 0)
{
message.Content = new ByteArrayContent(restRequest.Parameters[0].GetEncodedValue() as byte[]);
}
}
else
{
var contentString = new StringContent(restRequest.GetRequestBody(), Encoding.UTF8, restRequest.GetContentType());
message.Content = contentString;
}
}
return(await _client.SendAsync(message, cancellationToken).ConfigureAwait(false));
}