Fix for marketplace request deserialization

This commit is contained in:
benbierens 2023-08-31 11:19:53 +02:00
parent 647022a27e
commit cf9d2ffe19
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
2 changed files with 25 additions and 15 deletions

View File

@ -75,7 +75,7 @@ namespace DistTestCore.Codex
public string RequestStorage(CodexSalesRequestStorageRequest request, string contentId) public string RequestStorage(CodexSalesRequestStorageRequest request, string contentId)
{ {
return Http().HttpPostJson<CodexSalesRequestStorageRequest, string>($"storage/request/{contentId}", request); return Http().HttpPostJson($"storage/request/{contentId}", request);
} }
public CodexStoragePurchase GetPurchaseStatus(string purchaseId) public CodexStoragePurchase GetPurchaseStatus(string purchaseId)

View File

@ -55,25 +55,20 @@ namespace DistTestCore
public TResponse HttpPostJson<TRequest, TResponse>(string route, TRequest body) public TResponse HttpPostJson<TRequest, TResponse>(string route, TRequest body)
{ {
var response = HttpPostJson(route, body); var response = PostJson(route, body);
var json = Time.Wait(response.Content.ReadAsStringAsync()); var json = Time.Wait(response.Content.ReadAsStringAsync());
if(!response.IsSuccessStatusCode) { if (!response.IsSuccessStatusCode)
{
throw new HttpRequestException(json); throw new HttpRequestException(json);
} }
Log(GetUrl() + route, json); Log(GetUrl() + route, json);
return TryJsonDeserialize<TResponse>(json); return TryJsonDeserialize<TResponse>(json);
} }
public HttpResponseMessage HttpPostJson<TRequest>(string route, TRequest body) public string HttpPostJson<TRequest>(string route, TRequest body)
{ {
return Retry(() => var response = PostJson<TRequest>(route, body);
{ return Time.Wait(response.Content.ReadAsStringAsync());
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}");
} }
public string HttpPostString(string route, string body) public string HttpPostString(string route, string body)
@ -122,7 +117,8 @@ namespace DistTestCore
public T TryJsonDeserialize<T>(string json) public T TryJsonDeserialize<T>(string json)
{ {
var errors = new List<string>(); var errors = new List<string>();
var deserialized = JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings(){ var deserialized = JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings()
{
Error = delegate(object? sender, Serialization.ErrorEventArgs args) Error = delegate(object? sender, Serialization.ErrorEventArgs args)
{ {
if (args.CurrentObject == args.ErrorContext.OriginalObject) 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)}"); 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"); throw new JsonSerializationException($"Failed to deserialize JSON '{json}': resulting deserialized object is null");
} }
return deserialized; return deserialized;
} }
private HttpResponseMessage PostJson<TRequest>(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() private string GetUrl()
{ {
return $"{address.Host}:{address.Port}{baseUrl}"; return $"{address.Host}:{address.Port}{baseUrl}";