diff --git a/Framework/Core/Http.cs b/Framework/Core/Http.cs index 18fc5c7..968bdb7 100644 --- a/Framework/Core/Http.cs +++ b/Framework/Core/Http.cs @@ -13,15 +13,15 @@ namespace Core T HttpGetJson(string route); TResponse HttpPostJson(string route, TRequest body); string HttpPostJson(string route, TRequest body); - string HttpPostString(string route, string body); TResponse HttpPostString(string route, string body); string HttpPostStream(string route, Stream stream); Stream HttpGetStream(string route); - T TryJsonDeserialize(string json); + T Deserialize(string json); } internal class Http : IHttp { + private static readonly object httpLock = new object(); private readonly ILog log; private readonly ITimeSet timeSet; private readonly Address address; @@ -48,70 +48,60 @@ namespace Core public string HttpGetString(string route) { - return Retry(() => + return LockRetry(() => { - using var client = GetClient(); - var url = GetUrl() + route; - Log(url, ""); - var result = Time.Wait(client.GetAsync(url)); - var str = Time.Wait(result.Content.ReadAsStringAsync()); - Log(url, str); - return str; ; + return GetString(route); }, $"HTTP-GET:{route}"); } public T HttpGetJson(string route) { - var json = HttpGetString(route); - return TryJsonDeserialize(json); + return LockRetry(() => + { + var json = GetString(route); + return Deserialize(json); + }, $"HTTP-GET:{route}"); } public TResponse HttpPostJson(string route, TRequest body) { - var response = PostJson(route, body); - var json = Time.Wait(response.Content.ReadAsStringAsync()); - if (!response.IsSuccessStatusCode) + return LockRetry(() => { - throw new HttpRequestException(json); - } - Log(GetUrl() + route, json); - return TryJsonDeserialize(json); + var response = PostJson(route, body); + var json = Time.Wait(response.Content.ReadAsStringAsync()); + if (!response.IsSuccessStatusCode) + { + throw new HttpRequestException(json); + } + Log(GetUrl() + route, json); + return Deserialize(json); + }, $"HTTP-POST-JSON: {route}"); } public string HttpPostJson(string route, TRequest body) { - var response = PostJson(route, body); - return Time.Wait(response.Content.ReadAsStringAsync()); - } - - public string HttpPostString(string route, string body) - { - return Retry(() => + return LockRetry(() => { - using var client = GetClient(); - var url = GetUrl() + route; - Log(url, body); - var content = new StringContent(body); - content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); - var result = Time.Wait(client.PostAsync(url, content)); - var str = Time.Wait(result.Content.ReadAsStringAsync()); - Log(url, str); - return str; - }, $"HTTP-POST-STRING: {route}"); + var response = PostJson(route, body); + return Time.Wait(response.Content.ReadAsStringAsync()); + }, $"HTTP-POST-JSON: {route}"); } public TResponse HttpPostString(string route, string body) { - var response = HttpPostString(route, body); - if (response == null) throw new Exception("Received no response."); - var result = JsonConvert.DeserializeObject(response); - if (result == null) throw new Exception("Failed to deserialize response"); - return result; + return LockRetry(() => + { + var response = PostJsonString(route, body); + if (response == null) throw new Exception("Received no response."); + var result = JsonConvert.DeserializeObject(response); + if (result == null) throw new Exception("Failed to deserialize response"); + return result; + }, $"HTTO-POST-JSON: {route}"); } public string HttpPostStream(string route, Stream stream) { - return Retry(() => + return LockRetry(() => { using var client = GetClient(); var url = GetUrl() + route; @@ -127,7 +117,7 @@ namespace Core public Stream HttpGetStream(string route) { - return Retry(() => + return LockRetry(() => { var client = GetClient(); var url = GetUrl() + route; @@ -136,7 +126,7 @@ namespace Core }, $"HTTP-GET-STREAM: {route}"); } - public T TryJsonDeserialize(string json) + public T Deserialize(string json) { var errors = new List(); var deserialized = JsonConvert.DeserializeObject(json, new JsonSerializerSettings() @@ -154,7 +144,7 @@ namespace Core } } }); - if (errors.Count() > 0) + if (errors.Count > 0) { throw new JsonSerializationException($"Failed to deserialize JSON '{json}' with exception(s): \n{string.Join("\n", errors)}"); } @@ -165,16 +155,37 @@ namespace Core return deserialized; } + private string GetString(string route) + { + using var client = GetClient(); + var url = GetUrl() + route; + Log(url, ""); + var result = Time.Wait(client.GetAsync(url)); + var str = Time.Wait(result.Content.ReadAsStringAsync()); + Log(url, str); + return str; + } + private HttpResponseMessage PostJson(string route, TRequest body) { - return Retry(() => - { - using var client = GetClient(); - var url = GetUrl() + route; - using var content = JsonContent.Create(body); - Log(url, JsonConvert.SerializeObject(body)); - return Time.Wait(client.PostAsync(url, content)); - }, $"HTTP-POST-JSON: {route}"); + using var client = GetClient(); + var url = GetUrl() + route; + using var content = JsonContent.Create(body); + Log(url, JsonConvert.SerializeObject(body)); + return Time.Wait(client.PostAsync(url, content)); + } + + private string PostJsonString(string route, string body) + { + using var client = GetClient(); + var url = GetUrl() + route; + Log(url, body); + var content = new StringContent(body); + content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); + var result = Time.Wait(client.PostAsync(url, content)); + var str = Time.Wait(result.Content.ReadAsStringAsync()); + Log(url, str); + return str; } private string GetUrl() @@ -194,9 +205,12 @@ namespace Core } } - private T Retry(Func operation, string description) + private T LockRetry(Func operation, string description) { - return Time.Retry(operation, timeSet.HttpMaxNumberOfRetries(), timeSet.HttpCallRetryDelay(), description); + lock (httpLock) + { + return Time.Retry(operation, timeSet.HttpMaxNumberOfRetries(), timeSet.HttpCallRetryDelay(), description); + } } private HttpClient GetClient() diff --git a/ProjectPlugins/CodexPlugin/CodexAccess.cs b/ProjectPlugins/CodexPlugin/CodexAccess.cs index 123090c..9d41c82 100644 --- a/ProjectPlugins/CodexPlugin/CodexAccess.cs +++ b/ProjectPlugins/CodexPlugin/CodexAccess.cs @@ -40,7 +40,7 @@ namespace CodexPlugin }; } - var result = http.TryJsonDeserialize(str); + var result = http.Deserialize(str); result.IsPeerFound = true; return result; }