diff --git a/EosSharp/EosSharp.Core/Api/v1/TraceApi.cs b/EosSharp/EosSharp.Core/Api/v1/TraceApi.cs new file mode 100644 index 0000000..c5caffc --- /dev/null +++ b/EosSharp/EosSharp.Core/Api/v1/TraceApi.cs @@ -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 +{ + /// + /// TraceApi defines api methods for Trace Api Plug-In + /// + public class TraceApi + { + public EosConfigurator Config { get; set; } + public IHttpHandler HttpHandler { get; set; } + + /// + /// Eos Client Trace Api constructor. + /// + /// Configures client parameters + /// Http handler implementation + public TraceApi(EosConfigurator config, IHttpHandler httpHandler) + { + Config = config; + HttpHandler = httpHandler; + } + + private string MethodUrl(string methodName) => $"{Config.HttpEndpoint}/v1/trace_api/{methodName}"; + + public async Task GetBlockTrace(GetBlockTraceRequest data) + { + return await HttpHandler.PostJsonAsync(MethodUrl("get_block"), data); + } + } +} diff --git a/EosSharp/EosSharp.Core/Api/v1/TraceApiTypes.cs b/EosSharp/EosSharp.Core/Api/v1/TraceApiTypes.cs new file mode 100644 index 0000000..7fa4359 --- /dev/null +++ b/EosSharp/EosSharp.Core/Api/v1/TraceApiTypes.cs @@ -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 transactions { get; set; } + } + + [Serializable] + public class Transaction + { + public string id { get; set; } + public List 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 { 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 +} \ No newline at end of file diff --git a/EosSharp/EosSharp.Core/EosBase.cs b/EosSharp/EosSharp.Core/EosBase.cs index bf1427d..e14610f 100644 --- a/EosSharp/EosSharp.Core/EosBase.cs +++ b/EosSharp/EosSharp.Core/EosBase.cs @@ -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; } /// @@ -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); } @@ -544,5 +546,20 @@ public async Task> GetControlledAccounts(string accountName) } #endregion + + #region Trace Api Methods + /// + /// Query for blockchain block information with Trace Api + /// + /// block number to query information + /// block information + public Task GetBlockTrace(UInt32 blockNum) + { + return TraceApi.GetBlockTrace(new Api.v1.Trace.GetBlockTraceRequest() + { + block_num = blockNum.ToString() + }); + } + #endregion } } diff --git a/EosSharp/EosSharp.Core/Exceptions/ApiErrorException.cs b/EosSharp/EosSharp.Core/Exceptions/ApiErrorException.cs index 5e018ae..6496f83 100644 --- a/EosSharp/EosSharp.Core/Exceptions/ApiErrorException.cs +++ b/EosSharp/EosSharp.Core/Exceptions/ApiErrorException.cs @@ -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 details; diff --git a/EosSharp/EosSharp/HttpHelper.cs b/EosSharp/EosSharp/HttpHelper.cs index 6861f98..6d04e9f 100644 --- a/EosSharp/EosSharp/HttpHelper.cs +++ b/EosSharp/EosSharp/HttpHelper.cs @@ -206,28 +206,69 @@ public string GetRequestHashKey(string url, object data) /// Stream with response public async Task 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(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(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}" + }; + } } ///