Skip to content

Commit

Permalink
Change error response to dynamic to support strings
Browse files Browse the repository at this point in the history
  • Loading branch information
GhostWalker562 committed Oct 9, 2024
1 parent b728651 commit efd510a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Aptos/Aptos.Clients/AptosClient/AptosClient.Requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public async Task<AptosResponse<Res>> Request<Res>(ApiType type, AptosRequest re
new(
clientResponse.Status,
clientResponse.StatusText,
clientResponse.Error ?? [],
clientResponse.Error ?? new JObject(),
aptosRequest.Url,
clientResponse.Headers ?? [],
aptosRequest
Expand Down
3 changes: 2 additions & 1 deletion Aptos/Aptos.Clients/RequestClient/AptosRequestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Aptos;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using Aptos.Core;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand Down Expand Up @@ -142,7 +143,7 @@ public override async Task<ClientResponse<Res>> Post<Res>(ClientRequest request)
response.ReasonPhrase ?? "",
response.IsSuccessStatusCode ? JsonConvert.DeserializeObject<Res>(content)! : null,
!response.IsSuccessStatusCode
? JsonConvert.DeserializeObject<JObject>(content)
? Utilities.DeserializeJObjectOrString(content)
: null,
response,
response.Headers.ToDictionary(h => h.Key, h => string.Join(", ", h.Value))
Expand Down
8 changes: 6 additions & 2 deletions Aptos/Aptos.Clients/RequestClient/RequestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,19 @@ public class ClientResponse<Res>(
int status,
string statusText,
Res? data,
JObject? error = null,
dynamic? error = null,
HttpResponseMessage? response = null,
Dictionary<string, string>? headers = null
)
{
public int Status = status;
public string StatusText = statusText;
public Res? Data = data;
public JObject? Error = error;

/// <summary>
/// The error object returned by the API. This can be a JObject or a string.
/// </summary>
public dynamic? Error = error;
public HttpResponseMessage? Response = response;
public Dictionary<string, string>? Headers = headers ?? [];
}
Expand Down
12 changes: 12 additions & 0 deletions Aptos/Aptos.Core/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,17 @@ public static long FloorToWholeHour(long timestampInSeconds)
// Convert back to Unix timestamp and return
return flooredDateTime.ToUnixTimeSeconds();
}

public static dynamic? DeserializeJObjectOrString(string value)
{
try
{
return JsonConvert.DeserializeObject<JObject>(value);
}
catch (Exception)
{
return value;
}
}
}
}
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<!-- There should only be one <DefaultVersion> in the file. If moved, it should be updated in the GitHub Actions workflow. -->
<DefaultVersion>0.0.7</DefaultVersion>
<DefaultVersion>0.0.8</DefaultVersion>
<DefaultTargetFrameworks>net8.0;net7.0;net6.0;netstandard2.1</DefaultTargetFrameworks>
<DefaultTestingFrameworks>net8.0</DefaultTestingFrameworks>
</PropertyGroup>
Expand Down

0 comments on commit efd510a

Please sign in to comment.