From 13d15f4c7b64b90e85e202136c5928e0c44eac8b Mon Sep 17 00:00:00 2001 From: hoak Date: Thu, 7 Oct 2021 23:05:50 +0100 Subject: [PATCH] Adding status codes and sanity checks (#5) --- Bonfidanet.Client/BonfidaClient.cs | 11 ++- Bonfidanet.Client/Models/RequestResult.cs | 93 +++++++++++++++---- Bonfidanet.Examples/BonfidaClientExamples.cs | 20 +++- .../BonfidaStreamingClientExample.cs | 2 +- SharedBuildProperties.props | 2 +- 5 files changed, 102 insertions(+), 26 deletions(-) diff --git a/Bonfidanet.Client/BonfidaClient.cs b/Bonfidanet.Client/BonfidaClient.cs index d57bb30..36d410e 100644 --- a/Bonfidanet.Client/BonfidaClient.cs +++ b/Bonfidanet.Client/BonfidaClient.cs @@ -100,9 +100,16 @@ private async Task Post(string endpoint, HttpContent data = /// The task which returns the . private async Task> HandleResponse(HttpResponseMessage message) { - var data = await message.Content.ReadAsStringAsync(); + if (!message.IsSuccessStatusCode) + return new RequestResult(message); + + string data = await message.Content.ReadAsStringAsync(); _logger?.LogInformation(new EventId(0, "REC"), $"Result: {data}"); - return JsonSerializer.Deserialize>(data, _jsonSerializerOptions); + RequestResponse obj = JsonSerializer.Deserialize>(data, _jsonSerializerOptions); + if (obj == null) + return new RequestResult(message); + + return new RequestResult(message, obj.Data); } /// diff --git a/Bonfidanet.Client/Models/RequestResult.cs b/Bonfidanet.Client/Models/RequestResult.cs index 3dbc9ad..7430e56 100644 --- a/Bonfidanet.Client/Models/RequestResult.cs +++ b/Bonfidanet.Client/Models/RequestResult.cs @@ -1,19 +1,76 @@ -namespace Bonfida.Client.Models -{ - /// - /// Represents a result to a request to the Bonfida API. - /// - /// The type of the resulting data. - public class RequestResult - { - /// - /// Represents if the request was successful. - /// - public bool Success { get; set; } - - /// - /// The underlying data associated with the request. - /// - public T Data { get; set; } - } +using System.Net; +using System.Net.Http; + +namespace Bonfida.Client.Models +{ + /// + /// Represents a result to a request to the Bonfida API. + /// + /// The type of the resulting data. + public class RequestResponse + { + /// + /// Returns true if the API request was successful. + /// + public bool Success { get; set; } + + /// + /// The underlying data associated with the request. + /// + public T Data { get; set; } + } + + /// + /// Represents a result to a request to the Bonfida API. + /// + /// The type of the resulting data. + public class RequestResult + { + /// + /// Returns true if the request was successfully handled and parsed. + /// + public bool WasSuccessful => WasHttpRequestSuccessful && WasRequestSuccessfullyHandled; + + /// + /// + /// Returns true if the HTTP request was successful (e.g. Code 200). + /// + public bool WasHttpRequestSuccessful { get; set; } + + /// + /// Returns true if the request was successfully handled by the server and no error parameters are found in the result. + /// + public bool WasRequestSuccessfullyHandled { get; set; } + + + /// + /// Returns the of the request. + /// + public HttpStatusCode HttpStatusCode { get; set; } + + /// + /// The error reason. + /// + public string Reason { get; set; } + + /// + /// The underlying data associated with the request. + /// + public T Data { get; set; } + + /// + /// Initialize the request result. + /// An http request result. + /// The type of the request result. + /// + public RequestResult(HttpResponseMessage resultMsg, T result = default(T)) + { + HttpStatusCode = resultMsg.StatusCode; + WasHttpRequestSuccessful = resultMsg.IsSuccessStatusCode; + Reason = resultMsg.ReasonPhrase; + Data = result; + if (Data != null) + WasRequestSuccessfullyHandled = true; + } + } } \ No newline at end of file diff --git a/Bonfidanet.Examples/BonfidaClientExamples.cs b/Bonfidanet.Examples/BonfidaClientExamples.cs index 63c2a16..e275527 100644 --- a/Bonfidanet.Examples/BonfidaClientExamples.cs +++ b/Bonfidanet.Examples/BonfidaClientExamples.cs @@ -1,5 +1,7 @@ using System; using Bonfida.Client; +using System.Net; +using System.Threading.Tasks; namespace Bonfida.Examples { @@ -7,7 +9,7 @@ public class ExampleApiClient { private static readonly IClient BonfidaClient = ClientFactory.GetClient(); - static void Examples(string[] args) + static void Main(string[] args) { /* Get All Market Pairs */ //GetAllPairs(); @@ -22,7 +24,7 @@ static void Examples(string[] args) //GetAllRecentTrades(); /* Get Volume for ETH/USDT */ - //GetVolume("ETHUSDT"); + GetVolume("ETHUSDT"); /* Get OrderBook for ETH/USDT */ //GetOrderBook("ETHUSDT"); @@ -66,8 +68,18 @@ private static void GetAllRecentTrades() private static void GetVolume(string marketName) { - var volumeData = BonfidaClient.GetVolume(marketName); - Console.WriteLine(volumeData); + while (true) + { + var volumeData = BonfidaClient.GetVolume("SOLUSDC"); + if (volumeData.HttpStatusCode == HttpStatusCode.TooManyRequests) + { + Console.WriteLine($"Rate Limited."); + Task.Delay(30000).Wait(); + continue; + } + Console.WriteLine($"Volume {volumeData.Data[0].VolumeUsd}."); + Task.Delay(500).Wait(); + } } private static void GetOrderBook(string marketName) diff --git a/Bonfidanet.Examples/BonfidaStreamingClientExample.cs b/Bonfidanet.Examples/BonfidaStreamingClientExample.cs index 224eb2d..fce2fbb 100644 --- a/Bonfidanet.Examples/BonfidaStreamingClientExample.cs +++ b/Bonfidanet.Examples/BonfidaStreamingClientExample.cs @@ -9,7 +9,7 @@ public class BonfidaStreamingClientExample { private static readonly IStreamingClient Client = ClientFactory.GetStreamingClient(); - static void Main(string[] args) + static void Example(string[] args) { Client.SubscribeTrades(trade => { diff --git a/SharedBuildProperties.props b/SharedBuildProperties.props index 38759c1..2f40365 100644 --- a/SharedBuildProperties.props +++ b/SharedBuildProperties.props @@ -2,7 +2,7 @@ xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> Bonfidanet - 1.0.0 + 1.0.2 Copyright 2021 © blockmountain Tiago Carvalho & Hugo Carvalho blockmountain