From 37cd7053fd81a5bf7690c38df612b68fa221c353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Terzio=C4=9Flu?= Date: Wed, 15 Apr 2020 13:57:24 +0300 Subject: [PATCH 01/10] Add Trace Api endpoint 'get_block' --- EosSharp/EosSharp.Core/Api/v1/TraceApi.cs | 36 +++++++++++ .../EosSharp.Core/Api/v1/TraceApiTypes.cs | 64 +++++++++++++++++++ EosSharp/EosSharp.Core/EosBase.cs | 17 +++++ 3 files changed, 117 insertions(+) create mode 100644 EosSharp/EosSharp.Core/Api/v1/TraceApi.cs create mode 100644 EosSharp/EosSharp.Core/Api/v1/TraceApiTypes.cs 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 } } From 8dcf934b4b10dc06e99791f1c62bf7fdf0682000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Terzio=C4=9Flu?= Date: Fri, 17 Apr 2020 18:16:36 +0300 Subject: [PATCH 02/10] Change ApiError.code to ulong --- EosSharp/EosSharp.Core/Exceptions/ApiErrorException.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From 30b2e6b9739bd1cc47f31bde46ed434642458045 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Terzio=C4=9Flu?= Date: Thu, 15 Oct 2020 13:51:44 +0300 Subject: [PATCH 03/10] Fix --- EosSharp/EosSharp/HttpHelper.cs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/EosSharp/EosSharp/HttpHelper.cs b/EosSharp/EosSharp/HttpHelper.cs index 6861f98..442d696 100644 --- a/EosSharp/EosSharp/HttpHelper.cs +++ b/EosSharp/EosSharp/HttpHelper.cs @@ -206,12 +206,28 @@ 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 ArgumentNullException(nameof(response)); + } - if (response.IsSuccessStatusCode) - return stream; + if(response.Content == null) + { + throw new ApiException + { + StatusCode = (int)response.StatusCode, + Content = "Content is null" + }; + } + + var stream = await response.Content.ReadAsStreamAsync(); + if (response.IsSuccessStatusCode) return stream; var content = await StreamToStringAsync(stream); + if(content == null) + { + throw new ApplicationException($"Couldn't parse stream data."); + } ApiErrorException apiError = null; try From d7deac263bd5269b3ecb805e8bc6130c0595c091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Terzio=C4=9Flu?= Date: Fri, 16 Oct 2020 21:51:20 +0300 Subject: [PATCH 04/10] Add exception handling --- EosSharp/EosSharp/HttpHelper.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/EosSharp/EosSharp/HttpHelper.cs b/EosSharp/EosSharp/HttpHelper.cs index 442d696..63f2507 100644 --- a/EosSharp/EosSharp/HttpHelper.cs +++ b/EosSharp/EosSharp/HttpHelper.cs @@ -208,7 +208,7 @@ public async Task BuildSendResponse(HttpResponseMessage response) { if(response == null) { - throw new ArgumentNullException(nameof(response)); + throw new ApplicationException("Respionse is null!"); } if(response.Content == null) @@ -243,6 +243,10 @@ public async Task BuildSendResponse(HttpResponseMessage response) }; } + if(apiError == null) + { + throw new ApplicationException("Api error is null!"); + } throw apiError; } From b4c0d31f7742e142ef53171e34016fdc3f444114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Terzio=C4=9Flu?= Date: Fri, 16 Oct 2020 22:04:30 +0300 Subject: [PATCH 05/10] Update error --- EosSharp/EosSharp/HttpHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EosSharp/EosSharp/HttpHelper.cs b/EosSharp/EosSharp/HttpHelper.cs index 63f2507..85188db 100644 --- a/EosSharp/EosSharp/HttpHelper.cs +++ b/EosSharp/EosSharp/HttpHelper.cs @@ -245,7 +245,7 @@ public async Task BuildSendResponse(HttpResponseMessage response) if(apiError == null) { - throw new ApplicationException("Api error is null!"); + throw new NullReferenceException($"Api error is null! Response was: {content}"); } throw apiError; } From a534f674659774f5c9af22989b9b86065804ce0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Terzio=C4=9Flu?= Date: Fri, 16 Oct 2020 22:29:50 +0300 Subject: [PATCH 06/10] Add logs --- EosSharp/EosSharp/HttpHelper.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/EosSharp/EosSharp/HttpHelper.cs b/EosSharp/EosSharp/HttpHelper.cs index 85188db..5732d85 100644 --- a/EosSharp/EosSharp/HttpHelper.cs +++ b/EosSharp/EosSharp/HttpHelper.cs @@ -224,9 +224,10 @@ public async Task BuildSendResponse(HttpResponseMessage response) if (response.IsSuccessStatusCode) return stream; var content = await StreamToStringAsync(stream); - if(content == null) + if(string.IsNullOrEmpty(content)) { - throw new ApplicationException($"Couldn't parse stream data."); + var contentString = await response.Content.ReadAsStringAsync(); + throw new ApplicationException($"Couldn't parse stream data. Content: {contentString}"); } ApiErrorException apiError = null; @@ -245,7 +246,8 @@ public async Task BuildSendResponse(HttpResponseMessage response) if(apiError == null) { - throw new NullReferenceException($"Api error is null! Response was: {content}"); + var contentString = await response.Content.ReadAsStringAsync(); + throw new NullReferenceException($"Api error is null! Response was: {contentString}"); } throw apiError; } From 830b04df9d55bf94024e56395d023e679c11c5f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Terzio=C4=9Flu?= Date: Fri, 16 Oct 2020 22:37:10 +0300 Subject: [PATCH 07/10] Update error log --- EosSharp/EosSharp/HttpHelper.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/EosSharp/EosSharp/HttpHelper.cs b/EosSharp/EosSharp/HttpHelper.cs index 5732d85..316867e 100644 --- a/EosSharp/EosSharp/HttpHelper.cs +++ b/EosSharp/EosSharp/HttpHelper.cs @@ -226,8 +226,7 @@ public async Task BuildSendResponse(HttpResponseMessage response) var content = await StreamToStringAsync(stream); if(string.IsNullOrEmpty(content)) { - var contentString = await response.Content.ReadAsStringAsync(); - throw new ApplicationException($"Couldn't parse stream data. Content: {contentString}"); + throw new ApplicationException($"Couldn't parse stream data. Content: {content}"); } ApiErrorException apiError = null; @@ -246,8 +245,7 @@ public async Task BuildSendResponse(HttpResponseMessage response) if(apiError == null) { - var contentString = await response.Content.ReadAsStringAsync(); - throw new NullReferenceException($"Api error is null! Response was: {contentString}"); + throw new NullReferenceException($"Api error is null! Response was: {content}"); } throw apiError; } From 0da23249a0c44e70fbe7b583a53a8cda6de3f4ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Terzio=C4=9Flu?= Date: Fri, 16 Oct 2020 22:45:12 +0300 Subject: [PATCH 08/10] Update exception --- EosSharp/EosSharp/HttpHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EosSharp/EosSharp/HttpHelper.cs b/EosSharp/EosSharp/HttpHelper.cs index 316867e..b53e547 100644 --- a/EosSharp/EosSharp/HttpHelper.cs +++ b/EosSharp/EosSharp/HttpHelper.cs @@ -226,7 +226,7 @@ public async Task BuildSendResponse(HttpResponseMessage response) var content = await StreamToStringAsync(stream); if(string.IsNullOrEmpty(content)) { - throw new ApplicationException($"Couldn't parse stream data. Content: {content}"); + throw new NullReferenceException($"Couldn't parse stream data. Content: {content}, Status code: {response.IsSuccessStatusCode}"); } ApiErrorException apiError = null; From b2b0c37cc32a73ef45333e846ae2751d62ee4970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Terzio=C4=9Flu?= Date: Fri, 16 Oct 2020 22:50:46 +0300 Subject: [PATCH 09/10] Update exception --- EosSharp/EosSharp/HttpHelper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EosSharp/EosSharp/HttpHelper.cs b/EosSharp/EosSharp/HttpHelper.cs index b53e547..092e754 100644 --- a/EosSharp/EosSharp/HttpHelper.cs +++ b/EosSharp/EosSharp/HttpHelper.cs @@ -226,7 +226,7 @@ public async Task BuildSendResponse(HttpResponseMessage response) var content = await StreamToStringAsync(stream); if(string.IsNullOrEmpty(content)) { - throw new NullReferenceException($"Couldn't parse stream data. Content: {content}, Status code: {response.IsSuccessStatusCode}"); + throw new NullReferenceException($"Couldn't parse stream data. Content: {content}, Status code: {response.StatusCode}"); } ApiErrorException apiError = null; From ad0b82c9c69c4423010a10bd567a615e9b2b32a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Faruk=20Terzio=C4=9Flu?= Date: Mon, 7 Dec 2020 23:07:11 +0300 Subject: [PATCH 10/10] Add additional catch --- EosSharp/EosSharp/HttpHelper.cs | 45 ++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/EosSharp/EosSharp/HttpHelper.cs b/EosSharp/EosSharp/HttpHelper.cs index 092e754..6d04e9f 100644 --- a/EosSharp/EosSharp/HttpHelper.cs +++ b/EosSharp/EosSharp/HttpHelper.cs @@ -223,31 +223,52 @@ public async Task BuildSendResponse(HttpResponseMessage response) var stream = await response.Content.ReadAsStreamAsync(); if (response.IsSuccessStatusCode) return stream; - var content = await StreamToStringAsync(stream); - if(string.IsNullOrEmpty(content)) + string content = null; + try { - throw new NullReferenceException($"Couldn't parse stream data. Content: {content}, Status code: {response.StatusCode}"); + content = await StreamToStringAsync(stream); } - - ApiErrorException apiError = null; - try + catch (System.Exception ex) { - apiError = JsonConvert.DeserializeObject(content); + 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." }; } - if(apiError == null) + 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 NullReferenceException($"Api error is null! Response was: {content}"); + throw new ApiException + { + StatusCode = (int)response.StatusCode, + Content = $"Couldn't deserialized api error, exception: {ex.ToString()}, content: {content}" + }; } - throw apiError; } ///