From cf9d2ffe19e13cd7f1fd993f3ef636a3c20d45f1 Mon Sep 17 00:00:00 2001 From: benbierens Date: Thu, 31 Aug 2023 11:19:53 +0200 Subject: [PATCH] Fix for marketplace request deserialization --- DistTestCore/Codex/CodexAccess.cs | 2 +- DistTestCore/Http.cs | 38 +++++++++++++++++++------------ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/DistTestCore/Codex/CodexAccess.cs b/DistTestCore/Codex/CodexAccess.cs index f88b390..cf6ac19 100644 --- a/DistTestCore/Codex/CodexAccess.cs +++ b/DistTestCore/Codex/CodexAccess.cs @@ -75,7 +75,7 @@ namespace DistTestCore.Codex public string RequestStorage(CodexSalesRequestStorageRequest request, string contentId) { - return Http().HttpPostJson($"storage/request/{contentId}", request); + return Http().HttpPostJson($"storage/request/{contentId}", request); } public CodexStoragePurchase GetPurchaseStatus(string purchaseId) diff --git a/DistTestCore/Http.cs b/DistTestCore/Http.cs index 94d73f2..773f9a5 100644 --- a/DistTestCore/Http.cs +++ b/DistTestCore/Http.cs @@ -55,25 +55,20 @@ namespace DistTestCore public TResponse HttpPostJson(string route, TRequest body) { - var response = HttpPostJson(route, body); + var response = PostJson(route, body); var json = Time.Wait(response.Content.ReadAsStringAsync()); - if(!response.IsSuccessStatusCode) { + if (!response.IsSuccessStatusCode) + { throw new HttpRequestException(json); } Log(GetUrl() + route, json); return TryJsonDeserialize(json); } - public HttpResponseMessage HttpPostJson(string route, TRequest body) + public string HttpPostJson(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}"); + var response = PostJson(route, body); + return Time.Wait(response.Content.ReadAsStringAsync()); } public string HttpPostString(string route, string body) @@ -122,7 +117,8 @@ namespace DistTestCore public T TryJsonDeserialize(string json) { var errors = new List(); - var deserialized = JsonConvert.DeserializeObject(json, new JsonSerializerSettings(){ + var deserialized = JsonConvert.DeserializeObject(json, new JsonSerializerSettings() + { Error = delegate(object? sender, Serialization.ErrorEventArgs args) { if (args.CurrentObject == args.ErrorContext.OriginalObject) @@ -136,15 +132,29 @@ namespace DistTestCore } } }); - if (errors.Count() > 0) { + if (errors.Count() > 0) + { throw new JsonSerializationException($"Failed to deserialize JSON '{json}' with exception(s): \n{string.Join("\n", errors)}"); } - else if (deserialized == null) { + else if (deserialized == null) + { throw new JsonSerializationException($"Failed to deserialize JSON '{json}': resulting deserialized object is null"); } return deserialized; } + 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}"); + } + private string GetUrl() { return $"{address.Host}:{address.Port}{baseUrl}";