Skip to content

Commit

Permalink
Adding status codes and sanity checks (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hoakbuilds authored Oct 7, 2021
1 parent 6b06f38 commit 13d15f4
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 26 deletions.
11 changes: 9 additions & 2 deletions Bonfidanet.Client/BonfidaClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,16 @@ private async Task<HttpResponseMessage> Post(string endpoint, HttpContent data =
/// <returns>The task which returns the <see cref="RequestResult{T}"/>.</returns>
private async Task<RequestResult<T>> HandleResponse<T>(HttpResponseMessage message)
{
var data = await message.Content.ReadAsStringAsync();
if (!message.IsSuccessStatusCode)
return new RequestResult<T>(message);

string data = await message.Content.ReadAsStringAsync();
_logger?.LogInformation(new EventId(0, "REC"), $"Result: {data}");
return JsonSerializer.Deserialize<RequestResult<T>>(data, _jsonSerializerOptions);
RequestResponse<T> obj = JsonSerializer.Deserialize<RequestResponse<T>>(data, _jsonSerializerOptions);
if (obj == null)
return new RequestResult<T>(message);

return new RequestResult<T>(message, obj.Data);
}

/// <summary>
Expand Down
93 changes: 75 additions & 18 deletions Bonfidanet.Client/Models/RequestResult.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,76 @@
namespace Bonfida.Client.Models
{
/// <summary>
/// Represents a result to a request to the Bonfida API.
/// </summary>
/// <typeparam name="T">The type of the resulting data.</typeparam>
public class RequestResult<T>
{
/// <summary>
/// Represents if the request was successful.
/// </summary>
public bool Success { get; set; }

/// <summary>
/// The underlying data associated with the request.
/// </summary>
public T Data { get; set; }
}
using System.Net;
using System.Net.Http;

namespace Bonfida.Client.Models
{
/// <summary>
/// Represents a result to a request to the Bonfida API.
/// </summary>
/// <typeparam name="T">The type of the resulting data.</typeparam>
public class RequestResponse<T>
{
/// <summary>
/// Returns <c>true</c> if the API request was successful.
/// </summary>
public bool Success { get; set; }

/// <summary>
/// The underlying data associated with the request.
/// </summary>
public T Data { get; set; }
}

/// <summary>
/// Represents a result to a request to the Bonfida API.
/// </summary>
/// <typeparam name="T">The type of the resulting data.</typeparam>
public class RequestResult<T>
{
/// <summary>
/// Returns <c>true</c> if the request was successfully handled and parsed.
/// </summary>
public bool WasSuccessful => WasHttpRequestSuccessful && WasRequestSuccessfullyHandled;

/// <summary>
/// <summary>
/// Returns <c>true</c> if the HTTP request was successful (e.g. Code 200).
/// </summary>
public bool WasHttpRequestSuccessful { get; set; }

/// <summary>
/// Returns <c>true</c> if the request was successfully handled by the server and no error parameters are found in the result.
/// </summary>
public bool WasRequestSuccessfullyHandled { get; set; }


/// <summary>
/// Returns the <see cref="HttpStatusCode"/> of the request.
/// </summary>
public HttpStatusCode HttpStatusCode { get; set; }

/// <summary>
/// The error reason.
/// </summary>
public string Reason { get; set; }

/// <summary>
/// The underlying data associated with the request.
/// </summary>
public T Data { get; set; }

/// <summary>
/// Initialize the request result.
/// <param name="resultMsg">An http request result.</param>
/// <param name="result">The type of the request result.</param>
/// </summary>
public RequestResult(HttpResponseMessage resultMsg, T result = default(T))
{
HttpStatusCode = resultMsg.StatusCode;
WasHttpRequestSuccessful = resultMsg.IsSuccessStatusCode;
Reason = resultMsg.ReasonPhrase;
Data = result;
if (Data != null)
WasRequestSuccessfullyHandled = true;
}
}
}
20 changes: 16 additions & 4 deletions Bonfidanet.Examples/BonfidaClientExamples.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using Bonfida.Client;
using System.Net;
using System.Threading.Tasks;

namespace Bonfida.Examples
{
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();
Expand All @@ -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");
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Bonfidanet.Examples/BonfidaStreamingClientExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
{
Expand Down
2 changes: 1 addition & 1 deletion SharedBuildProperties.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Product>Bonfidanet</Product>
<Version>1.0.0</Version>
<Version>1.0.2</Version>
<Copyright>Copyright 2021 &#169; blockmountain</Copyright>
<Authors>Tiago Carvalho &amp; Hugo Carvalho</Authors>
<PublisherName>blockmountain</PublisherName>
Expand Down

0 comments on commit 13d15f4

Please sign in to comment.