Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trace Api Requests #54

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
36 changes: 36 additions & 0 deletions EosSharp/EosSharp.Core/Api/v1/TraceApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using EosSharp.Core;
using EosSharp.Core.Api.v1;
using EosSharp.Core.Api.v1.Trace;
using EosSharp.Core.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace EosSharp.Core.Api.v1
{
/// <summary>
/// TraceApi defines api methods for Trace Api Plug-In
/// </summary>
public class TraceApi
{
public EosConfigurator Config { get; set; }
public IHttpHandler HttpHandler { get; set; }

/// <summary>
/// Eos Client Trace Api constructor.
/// </summary>
/// <param name="config">Configures client parameters</param>
/// <param name="httpHandler">Http handler implementation</param>
public TraceApi(EosConfigurator config, IHttpHandler httpHandler)
{
Config = config;
HttpHandler = httpHandler;
}

private string MethodUrl(string methodName) => $"{Config.HttpEndpoint}/v1/trace_api/{methodName}";

public async Task<GetBlockTraceResponse> GetBlockTrace(GetBlockTraceRequest data)
{
return await HttpHandler.PostJsonAsync<GetBlockTraceResponse>(MethodUrl("get_block"), data);
}
}
}
64 changes: 64 additions & 0 deletions EosSharp/EosSharp.Core/Api/v1/TraceApiTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;

namespace EosSharp.Core.Api.v1.Trace
{
#region trace api types
[Serializable]
public class GetBlockTraceRequest
{
public string block_num;
}

public static class BlockStatuses
{
public const string Irreversible = "irreversible";
}

[Serializable]
public class GetBlockTraceResponse
{
public string id { get; set; }
public UInt32 number { get; set; }
public string previous_id { get; set; }
public string status { get; set; }
public DateTime timestamp { get; set; }
public string producer { get; set; }
public List<Transaction> transactions { get; set; }
}

[Serializable]
public class Transaction
{
public string id { get; set; }
public List<Action> actions { get; set; }
}

[Serializable]
public class Action
{
public string receiver { get; set; }
public string account { get; set; }
public string action { get; set; }
public List<Authorization> authorization { get; set; }
public string data { get; set; }
public Params @params { get; set; }
}

[Serializable]
public class Authorization
{
public string account { get; set; }
public string permission { get; set; }
}

[Serializable]
public class Params
{
public string from { get; set; }
public string to { get; set; }
public string quantity { get; set; }
public string memo { get; set; }
}
#endregion
}
17 changes: 17 additions & 0 deletions EosSharp/EosSharp.Core/EosBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class EosBase
{
private EosConfigurator EosConfig { get; set; }
private EosApi Api { get; set; }
private TraceApi TraceApi { get; set; }
private AbiSerializationProvider AbiSerializer { get; set; }

/// <summary>
Expand All @@ -32,6 +33,7 @@ public EosBase(EosConfigurator config, IHttpHandler httpHandler)
throw new ArgumentNullException("config");
}
Api = new EosApi(EosConfig, httpHandler);
TraceApi = new TraceApi(EosConfig, httpHandler);
AbiSerializer = new AbiSerializationProvider(Api);
}

Expand Down Expand Up @@ -544,5 +546,20 @@ public async Task<List<string>> GetControlledAccounts(string accountName)
}

#endregion

#region Trace Api Methods
/// <summary>
/// Query for blockchain block information with Trace Api
/// </summary>
/// <param name="blockNum">block number to query information</param>
/// <returns>block information</returns>
public Task<Api.v1.Trace.GetBlockTraceResponse> GetBlockTrace(UInt32 blockNum)
{
return TraceApi.GetBlockTrace(new Api.v1.Trace.GetBlockTraceRequest()
{
block_num = blockNum.ToString()
});
}
#endregion
}
}
2 changes: 1 addition & 1 deletion EosSharp/EosSharp.Core/Exceptions/ApiErrorException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
[Serializable]
public class ApiError
{
public int code;
public ulong code;
public string name;
public string what;
public List<ApiErrorDetail> details;
Expand Down
59 changes: 50 additions & 9 deletions EosSharp/EosSharp/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,28 +206,69 @@ public string GetRequestHashKey(string url, object data)
/// <returns>Stream with response</returns>
public async Task<Stream> BuildSendResponse(HttpResponseMessage response)
{
var stream = await response.Content.ReadAsStreamAsync();
if(response == null)
{
throw new ApplicationException("Respionse is null!");
}

if (response.IsSuccessStatusCode)
return stream;
if(response.Content == null)
{
throw new ApiException
{
StatusCode = (int)response.StatusCode,
Content = "Content is null"
};
}

var content = await StreamToStringAsync(stream);
var stream = await response.Content.ReadAsStreamAsync();
if (response.IsSuccessStatusCode) return stream;

ApiErrorException apiError = null;
string content = null;
try
{
apiError = JsonConvert.DeserializeObject<ApiErrorException>(content);
content = await StreamToStringAsync(stream);
}
catch (System.Exception ex)
{
throw new ApiException
{
StatusCode = (int)response.StatusCode,
Content = $"Couldn't parse stream data. exception: {ex.ToString()}"
};
}
catch(Exception)

if(string.IsNullOrEmpty(content))
{
throw new ApiException
{
StatusCode = (int)response.StatusCode,
Content = content
Content = $"Couldn't parse stream data."
};
}

throw apiError;
try
{
ApiErrorException apiError = JsonConvert.DeserializeObject<ApiErrorException>(content);

if(apiError == null)
{
throw new ApiException
{
StatusCode = (int)response.StatusCode,
Content = $"Api error is null! Status code: {response.StatusCode}, content: {content}"
};
}

throw apiError;
}
catch(Exception ex)
{
throw new ApiException
{
StatusCode = (int)response.StatusCode,
Content = $"Couldn't deserialized api error, exception: {ex.ToString()}, content: {content}"
};
}
}

/// <summary>
Expand Down