diff --git a/src/Discord.Net.Commands.Net45/Discord.Net.Commands.csproj b/src/Discord.Net.Commands.Net45/Discord.Net.Commands.csproj index bf7a0d06d..f3db55a0d 100644 --- a/src/Discord.Net.Commands.Net45/Discord.Net.Commands.csproj +++ b/src/Discord.Net.Commands.Net45/Discord.Net.Commands.csproj @@ -20,7 +20,7 @@ DEBUG;TRACE prompt 4 - true + false pdbonly diff --git a/src/Discord.Net.Net45/Discord.Net.csproj b/src/Discord.Net.Net45/Discord.Net.csproj index f69857016..8dec9397e 100644 --- a/src/Discord.Net.Net45/Discord.Net.csproj +++ b/src/Discord.Net.Net45/Discord.Net.csproj @@ -19,8 +19,8 @@ bin\Debug\ DEBUG;TRACE prompt - 4 - true + 2 + false pdbonly diff --git a/src/Discord.Net/Helpers/Http.cs b/src/Discord.Net/Helpers/Http.cs index e8075365f..55c99edef 100644 --- a/src/Discord.Net/Helpers/Http.cs +++ b/src/Discord.Net/Helpers/Http.cs @@ -4,25 +4,32 @@ using System.Text; using System.Threading.Tasks; using System.Net.Http; using System.Reflection; -using System.Diagnostics; using System.Net; using System.IO; using System.Globalization; +#if TEST_RESPONSES +using System.Diagnostics; +using JsonErrorEventArgs = Newtonsoft.Json.Serialization.ErrorEventArgs; +#endif + namespace Discord.Helpers { internal static class Http { -#if DEBUG +#if TEST_RESPONSES private const bool _isDebug = true; #else private const bool _isDebug = false; #endif 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() { + _patch = new HttpMethod("PATCH"); //Not sure why this isn't a default... + _client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip, @@ -34,7 +41,13 @@ namespace Discord.Helpers string version = typeof(Http).GetTypeInfo().Assembly.GetName().Version.ToString(2); _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; public static string Token @@ -109,16 +122,13 @@ namespace Discord.Helpers where ResponseT : class { string responseJson = await SendRequest(method, path, content, true); - var response = JsonConvert.DeserializeObject(responseJson); -#if DEBUG - CheckResponse(responseJson, response); -#endif + var response = JsonConvert.DeserializeObject(responseJson, _settings); return response; } private static async Task Send(HttpMethod method, string path, HttpContent content) { string responseJson = await SendRequest(method, path, content, _isDebug); -#if DEBUG +#if TEST_RESPONSES CheckEmptyResponse(responseJson); #endif return responseJson; @@ -126,46 +136,40 @@ namespace Discord.Helpers private static async Task SendRequest(HttpMethod method, string path, HttpContent content, bool hasResponse) { -#if DEBUG +#if TEST_RESPONSES Stopwatch stopwatch = Stopwatch.StartNew(); -#endif - HttpRequestMessage msg = new HttpRequestMessage(method, path); - if (content != null) - msg.Content = content; - +#endif string result; - HttpResponseMessage response; - if (hasResponse) - { - response = await _client.SendAsync(msg, HttpCompletionOption.ResponseContentRead); - if (!response.IsSuccessStatusCode) - throw new HttpException(response.StatusCode); - result = await response.Content.ReadAsStringAsync(); - } - else + using (HttpRequestMessage msg = new HttpRequestMessage(method, path)) { - response = await _client.SendAsync(msg, HttpCompletionOption.ResponseHeadersRead); - if (!response.IsSuccessStatusCode) - throw new HttpException(response.StatusCode); - result = null; + if (content != null) + msg.Content = content; + + HttpResponseMessage response; + if (hasResponse) + { + response = await _client.SendAsync(msg, HttpCompletionOption.ResponseContentRead); + if (!response.IsSuccessStatusCode) + throw new HttpException(response.StatusCode); + result = await response.Content.ReadAsStringAsync(); + } + 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(); Debug.WriteLine($"{method} {path}: {Math.Round(stopwatch.ElapsedTicks / (double)TimeSpan.TicksPerMillisecond, 2)}ms"); #endif - return result; - } - -#if DEBUG - private static void CheckResponse(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.");*/ + return result; } +#if TEST_RESPONSES private static void CheckEmptyResponse(string json) { if (!string.IsNullOrEmpty(json)) diff --git a/src/Discord.Net/project.json b/src/Discord.Net/project.json index 153d2c2f8..764f5df9c 100644 --- a/src/Discord.Net/project.json +++ b/src/Discord.Net/project.json @@ -9,8 +9,12 @@ "type": "git", "url": "git://github.com/RogueException/Discord.Net" }, - "compilationOptions": { - "warningsAsErrors": true + "configurations": { + "FullDebug": { + "compilationOptions": { + "define": ["DEBUG","TRACE","TEST_RESPONSES"] + } + } }, "dependencies": {