Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/RedDog.Search/Http/ApiConnection.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand Down Expand Up @@ -28,8 +29,10 @@ internal ApiConnection(Uri baseUri, string apiKey, HttpClientHandler handler)
_client.DefaultRequestHeaders.Add("Accept", "application/json;odata.metadata=none");

// Configure serialization.
_formatter = new JsonMediaTypeFormatter();
_formatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
_formatter = new JsonMediaTypeFormatter
{
SerializerSettings = {ContractResolver = new CamelCasePropertyNamesContractResolver()}
};
_formatter.SerializerSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
_formatter.SerializerSettings.Converters.Add(new XsdDurationConverter());
}
Expand All @@ -42,13 +45,13 @@ public Uri BaseUri

public static ApiConnection Create(Uri baseUri, string apiKey, IWebProxy proxy = null)
{
var handler = new HttpClientHandler() { Proxy = proxy };
var handler = new HttpClientHandler { Proxy = proxy };
return new ApiConnection(baseUri, apiKey, handler);
}

public static ApiConnection Create(string serviceName, string apiKey, IWebProxy proxy = null)
{
var handler = new HttpClientHandler() { Proxy = proxy };
var handler = new HttpClientHandler { Proxy = proxy };
return new ApiConnection(new Uri(String.Format(ApiConstants.BaseUrl, serviceName)), apiKey, handler);
}

Expand Down Expand Up @@ -128,6 +131,15 @@ private async Task<IApiResponse<TResponse>> BuildResponse<TResponse>(HttpRespons
}
else
{
if (response.StatusCode == HttpStatusCode.RequestEntityTooLarge)
{
response.Error = new Error
{
Message = message.ReasonPhrase,
Code = ((int)response.StatusCode).ToString(CultureInfo.InvariantCulture)
};
return response;
}
// Errors should also be deserialized.
var errorResponse = await message.Content.ReadAsAsync<ErrorResponse>(cancelToken)
.ConfigureAwait(false);
Expand Down