Skip to content

Commit

Permalink
Refactor dynamic usage to object
Browse files Browse the repository at this point in the history
  • Loading branch information
GhostWalker562 committed Dec 11, 2024
1 parent 6b59f04 commit 5a6ade2
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 23 deletions.
5 changes: 4 additions & 1 deletion Aptos/Aptos.Clients/AccountClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace Aptos;
using Aptos.Exceptions;
using Aptos.Indexer.GraphQL;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public class AccountClient(AptosClient client)
{
Expand Down Expand Up @@ -381,13 +382,15 @@ public async Task<AccountAddress> LookupOriginalAccountAddress(string authentica
}
catch (Exception e)
{
#pragma warning disable CS8602 // Dereference of a possibly null reference.
if (
e is ApiException apex
&& apex.Data["error_code"]?.ToString() == "table_item_not_found"
&& (apex.Data as JObject)["error_code"]?.ToString() == "table_item_not_found"
)
{
return AccountAddress.From(authenticationKey);
}
#pragma warning restore CS8602 // Dereference of a possibly null reference.
throw e;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Aptos/Aptos.Clients/AptosClient/AptosClient.Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ namespace Aptos;

public class GetAptosRequest(
string? path = null,
dynamic? body = null,
object? body = null,
string? contentType = null,
Dictionary<string, string>? queryParams = null,
string? originMethod = null
) : BaseAptosRequest
{
public override string? Path { get; set; } = path;
public override dynamic? Body { get; set; } = body;
public override object? Body { get; set; } = body;
public override string? ContentType { get; set; } = contentType;
public override Dictionary<string, string>? QueryParams { get; set; } = queryParams;
public override string? OriginMethod { get; set; } = originMethod;
Expand Down
4 changes: 2 additions & 2 deletions Aptos/Aptos.Clients/AptosClient/AptosClient.Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ namespace Aptos;

public class PostAptosRequest(
string? path = null,
dynamic? body = null,
object? body = null,
string? contentType = null,
Dictionary<string, string>? queryParams = null,
string? originMethod = null
) : BaseAptosRequest
{
public override string? Path { get; set; } = path;
public override dynamic? Body { get; set; } = body;
public override object? Body { get; set; } = body;
public override string? ContentType { get; set; } = contentType;
public override Dictionary<string, string>? QueryParams { get; set; } = queryParams;
public override string? OriginMethod { get; set; } = originMethod;
Expand Down
10 changes: 5 additions & 5 deletions Aptos/Aptos.Clients/AptosClient/AptosClient.Requests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract class BaseAptosRequest
{
public abstract string? Path { get; set; }

public abstract dynamic? Body { get; set; }
public abstract object? Body { get; set; }

public abstract string? ContentType { get; set; }

Expand All @@ -21,15 +21,15 @@ public class AptosRequest(
string url,
HttpMethod method,
string? path = null,
dynamic? body = null,
object? body = null,
string? contentType = null,
Dictionary<string, string>? queryParams = null,
string? originMethod = null
) : BaseAptosRequest
{
public string Url { get; set; } = url;
public override string? Path { get; set; } = path;
public override dynamic? Body { get; set; } = body;
public override object? Body { get; set; } = body;
public override string? ContentType { get; set; } = contentType;
public override Dictionary<string, string>? QueryParams { get; set; } = queryParams;
public override string? OriginMethod { get; set; } = originMethod;
Expand All @@ -46,7 +46,7 @@ public AptosRequest(string url, HttpMethod method, BaseAptosRequest request)
url,
method,
request.Path,
(object?)request.Body,
request.Body,
request.ContentType,
request.QueryParams,
request.OriginMethod
Expand Down Expand Up @@ -106,7 +106,7 @@ public async Task<AptosResponse<Res>> Request<Res>(ApiType type, AptosRequest re

ClientResponse<Res> clientResponse = await ClientRequest<Res>(clientRequest);

AptosResponse<dynamic> errorResponse =
AptosResponse<object> errorResponse =
new(
clientResponse.Status,
clientResponse.StatusText,
Expand Down
2 changes: 1 addition & 1 deletion Aptos/Aptos.Clients/IndexerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Func<IAptosIndexerClient, Task<IOperationResult<Res>>> query
throw new ApiException(
ApiType.Indexer,
request,
new AptosResponse<dynamic>(
new AptosResponse<object>(
200,
exception.Message,
JObject.FromObject(new { errors = exception.Errors }),
Expand Down
8 changes: 4 additions & 4 deletions Aptos/Aptos.Clients/RequestClient/RequestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Aptos;
public class ClientRequest(
string url,
HttpMethod method,
dynamic? body = null,
object? body = null,
string? contentType = null,
Dictionary<string, string>? queryParams = null,
Dictionary<string, string>? headers = null
Expand All @@ -15,7 +15,7 @@ public class ClientRequest(

public HttpMethod Method = method;

public dynamic? Body = body;
public object? Body = body;

public string? ContentType = contentType;

Expand All @@ -28,7 +28,7 @@ public class ClientResponse<Res>(
int status,
string statusText,
Res? data,
dynamic? error = null,
object? error = null,
HttpResponseMessage? response = null,
Dictionary<string, string>? headers = null
)
Expand All @@ -40,7 +40,7 @@ public class ClientResponse<Res>(
/// <summary>
/// The error object returned by the API. This can be a JObject or a string.
/// </summary>
public dynamic? Error = error;
public object? Error = error;
public HttpResponseMessage? Response = response;
public Dictionary<string, string>? Headers = headers ?? [];
}
Expand Down
2 changes: 1 addition & 1 deletion Aptos/Aptos.Core/Utilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static long FloorToWholeHour(long timestampInSeconds)
return flooredDateTime.ToUnixTimeSeconds();
}

public static dynamic? DeserializeJObjectOrString(string value)
public static object? DeserializeJObjectOrString(string value)
{
try
{
Expand Down
6 changes: 3 additions & 3 deletions Aptos/Aptos.Exceptions/ApiException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public class ApiException : BaseException
/// <summary>
/// The error object returned by the API. This can be a JObject or a string.
/// </summary>
public new readonly dynamic Data;
public new readonly object Data;

public readonly AptosRequest Request;

public ApiException(ApiType type, AptosRequest request, AptosResponse<dynamic> response)
public ApiException(ApiType type, AptosRequest request, AptosResponse<object> response)
: base(DeriveErrorMessage(type, request, response))
{
Url = request.Url;
Expand All @@ -33,7 +33,7 @@ public ApiException(ApiType type, AptosRequest request, AptosResponse<dynamic> r
private static string DeriveErrorMessage(
ApiType type,
AptosRequest request,
AptosResponse<dynamic> response
AptosResponse<object> response
)
{
string? traceId = response.Headers.TryGetValue("traceparent", out string? value)
Expand Down
8 changes: 4 additions & 4 deletions Aptos/Aptos.Transactions/TypeTag/TypeTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract partial class TypeTag(TypeTagVariant variant) : Serializable

public override string ToString() => $"{Variant.ToString().ToLower()}";

public bool CheckType(dynamic value)
public bool CheckType(object value)
{
switch (Variant)
{
Expand All @@ -51,7 +51,7 @@ public bool CheckType(dynamic value)
if (value.GetType().Name.Contains("MoveVector"))
{
// Cannot pattern match on MoveVector<T> because it's a generic type
MoveVector<dynamic>? vector = value as MoveVector<dynamic>;
MoveVector<object>? vector = value as MoveVector<object>;
if (vector == null)
return false;
return typeTagVector.Value.CheckType(vector.Values[0]);
Expand All @@ -71,10 +71,10 @@ public bool CheckType(dynamic value)
if (value.GetType().Name.Contains("MoveOption"))
{
// Cannot pattern match on MoveVector<T> because it's a generic type
MoveOption<dynamic>? option = value as MoveOption<dynamic>;
MoveOption<object>? option = value as MoveOption<object>;
if (option?.Value != null && typeTagStruct.Value.TypeArgs.Count > 0)
{
return typeTagStruct.Value.TypeArgs[0].CheckType(option?.Value);
return typeTagStruct.Value.TypeArgs[0].CheckType(option?.Value!);
}
}
}
Expand Down

0 comments on commit 5a6ade2

Please sign in to comment.