Added full response checking

This commit is contained in:
Brandon Smith
2015-08-22 04:03:09 -03:00
parent c16cb31782
commit 9c1f8ffebe
4 changed files with 52 additions and 44 deletions

View File

@@ -20,7 +20,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>

View File

@@ -19,8 +19,8 @@
<OutputPath>bin\Debug\</OutputPath> <OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants> <DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>2</WarningLevel>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors> <TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType> <DebugType>pdbonly</DebugType>

View File

@@ -4,25 +4,32 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Net.Http; using System.Net.Http;
using System.Reflection; using System.Reflection;
using System.Diagnostics;
using System.Net; using System.Net;
using System.IO; using System.IO;
using System.Globalization; using System.Globalization;
#if TEST_RESPONSES
using System.Diagnostics;
using JsonErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs;
#endif
namespace Discord.Helpers namespace Discord.Helpers
{ {
internal static class Http internal static class Http
{ {
#if DEBUG #if TEST_RESPONSES
private const bool _isDebug = true; private const bool _isDebug = true;
#else #else
private const bool _isDebug = false; private const bool _isDebug = false;
#endif #endif
private static readonly HttpClient _client; private static readonly HttpClient _client;
private static readonly HttpMethod _patch = new HttpMethod("PATCH"); //Not sure why this isn't a default... private static readonly HttpMethod _patch;
private static readonly JsonSerializerSettings _settings;
static Http() static Http()
{ {
_patch = new HttpMethod("PATCH"); //Not sure why this isn't a default...
_client = new HttpClient(new HttpClientHandler _client = new HttpClient(new HttpClientHandler
{ {
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
@@ -34,7 +41,13 @@ namespace Discord.Helpers
string version = typeof(Http).GetTypeInfo().Assembly.GetName().Version.ToString(2); string version = typeof(Http).GetTypeInfo().Assembly.GetName().Version.ToString(2);
_client.DefaultRequestHeaders.Add("user-agent", $"Discord.Net/{version} (https://github.com/RogueException/Discord.Net)"); _client.DefaultRequestHeaders.Add("user-agent", $"Discord.Net/{version} (https://github.com/RogueException/Discord.Net)");
}
_settings = new JsonSerializerSettings();
#if TEST_RESPONSES
_settings.CheckAdditionalContent = true;
_settings.MissingMemberHandling = MissingMemberHandling.Error;
#endif
}
private static string _token; private static string _token;
public static string Token public static string Token
@@ -109,16 +122,13 @@ namespace Discord.Helpers
where ResponseT : class where ResponseT : class
{ {
string responseJson = await SendRequest(method, path, content, true); string responseJson = await SendRequest(method, path, content, true);
var response = JsonConvert.DeserializeObject<ResponseT>(responseJson); var response = JsonConvert.DeserializeObject<ResponseT>(responseJson, _settings);
#if DEBUG
CheckResponse(responseJson, response);
#endif
return response; return response;
} }
private static async Task<string> Send(HttpMethod method, string path, HttpContent content) private static async Task<string> Send(HttpMethod method, string path, HttpContent content)
{ {
string responseJson = await SendRequest(method, path, content, _isDebug); string responseJson = await SendRequest(method, path, content, _isDebug);
#if DEBUG #if TEST_RESPONSES
CheckEmptyResponse(responseJson); CheckEmptyResponse(responseJson);
#endif #endif
return responseJson; return responseJson;
@@ -126,46 +136,40 @@ namespace Discord.Helpers
private static async Task<string> SendRequest(HttpMethod method, string path, HttpContent content, bool hasResponse) private static async Task<string> SendRequest(HttpMethod method, string path, HttpContent content, bool hasResponse)
{ {
#if DEBUG #if TEST_RESPONSES
Stopwatch stopwatch = Stopwatch.StartNew(); Stopwatch stopwatch = Stopwatch.StartNew();
#endif #endif
HttpRequestMessage msg = new HttpRequestMessage(method, path);
if (content != null)
msg.Content = content;
string result; string result;
HttpResponseMessage response; using (HttpRequestMessage msg = new HttpRequestMessage(method, path))
if (hasResponse)
{ {
response = await _client.SendAsync(msg, HttpCompletionOption.ResponseContentRead); if (content != null)
if (!response.IsSuccessStatusCode) msg.Content = content;
throw new HttpException(response.StatusCode);
result = await response.Content.ReadAsStringAsync(); HttpResponseMessage response;
} if (hasResponse)
else {
{ response = await _client.SendAsync(msg, HttpCompletionOption.ResponseContentRead);
response = await _client.SendAsync(msg, HttpCompletionOption.ResponseHeadersRead); if (!response.IsSuccessStatusCode)
if (!response.IsSuccessStatusCode) throw new HttpException(response.StatusCode);
throw new HttpException(response.StatusCode); result = await response.Content.ReadAsStringAsync();
result = null; }
else
{
response = await _client.SendAsync(msg, HttpCompletionOption.ResponseHeadersRead);
if (!response.IsSuccessStatusCode)
throw new HttpException(response.StatusCode);
result = null;
}
} }
#if DEBUG #if TEST_RESPONSES
stopwatch.Stop(); stopwatch.Stop();
Debug.WriteLine($"{method} {path}: {Math.Round(stopwatch.ElapsedTicks / (double)TimeSpan.TicksPerMillisecond, 2)}ms"); Debug.WriteLine($"{method} {path}: {Math.Round(stopwatch.ElapsedTicks / (double)TimeSpan.TicksPerMillisecond, 2)}ms");
#endif #endif
return result; return result;
}
#if DEBUG
private static void CheckResponse<T>(string json, T obj)
{
/*JToken token = JToken.Parse(json);
JToken token2 = JToken.FromObject(obj);
if (!JToken.DeepEquals(token, token2))
throw new Exception("API check failed: Objects do not match.");*/
} }
#if TEST_RESPONSES
private static void CheckEmptyResponse(string json) private static void CheckEmptyResponse(string json)
{ {
if (!string.IsNullOrEmpty(json)) if (!string.IsNullOrEmpty(json))

View File

@@ -9,8 +9,12 @@
"type": "git", "type": "git",
"url": "git://github.com/RogueException/Discord.Net" "url": "git://github.com/RogueException/Discord.Net"
}, },
"compilationOptions": { "configurations": {
"warningsAsErrors": true "FullDebug": {
"compilationOptions": {
"define": ["DEBUG","TRACE","TEST_RESPONSES"]
}
}
}, },
"dependencies": { "dependencies": {