Skip to content

Commit

Permalink
Authentication, error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Nov 8, 2024
1 parent 3f7424f commit b044d02
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 119 deletions.
104 changes: 67 additions & 37 deletions CoinGecko.Net/Clients/CoinGeckoRestClientApi.cs

Large diffs are not rendered by default.

145 changes: 76 additions & 69 deletions CoinGecko.Net/CoinGecko.Net.xml

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions CoinGecko.Net/CoinGeckoAuthenticationProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using CoinGecko.Net.Objects;
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Objects;

namespace CoinGecko.Net
{
internal class CoinGeckoAuthenticationProvider : AuthenticationProvider<CoinGeckoApiCredentials>
{
/// <summary>
/// Whether or not a demo key is configured
/// </summary>
public bool IsDemo => _credentials.DemoKey;

public CoinGeckoAuthenticationProvider(CoinGeckoApiCredentials credentials) : base(credentials)
{
}

public override void AuthenticateRequest(
RestApiClient apiClient,
Uri uri,
HttpMethod method,
ref IDictionary<string, object>? uriParameters,
ref IDictionary<string, object>? bodyParameters,
ref Dictionary<string, string>? headers,
bool auth,
ArrayParametersSerialization arraySerialization,
HttpMethodParameterPosition parameterPosition,
RequestBodyFormat requestBodyFormat)
{
uriParameters ??= new ParameterCollection();
if (_credentials.DemoKey)
uriParameters.Add("x_cg_demo_api_key", _credentials.Key);
else
uriParameters.Add("x_cg_pro_api_key", _credentials.Key);
}
}
}
24 changes: 15 additions & 9 deletions CoinGecko.Net/CoinGeckoEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,38 @@ namespace CoinGecko.Net
public class CoinGeckoEnvironment : TradeEnvironment
{
/// <summary>
/// Rest client address
/// Rest client address public API
/// </summary>
public string RestApiAddress { get; }
public string RestApiAddressPublic { get; }
/// <summary>
/// Rest client address pro API
/// </summary>
public string RestApiAddressPro { get; }

internal CoinGeckoEnvironment(string name,
string restBaseAddress) : base(name)
string restBaseAddressPublic,
string restBaseAddressPro) : base(name)
{
RestApiAddress = restBaseAddress;
RestApiAddressPublic = restBaseAddressPublic;
RestApiAddressPro = restBaseAddressPro;
}

/// <summary>
/// Live environment
/// </summary>
public static CoinGeckoEnvironment Live { get; }
= new CoinGeckoEnvironment(TradeEnvironmentNames.Live,
CoinGeckoApiAddresses.Default.RestClientAddress);
CoinGeckoApiAddresses.Default.RestClientAddressPublic,
CoinGeckoApiAddresses.Default.RestClientAddressPro);

/// <summary>
/// Create a custom environment
/// </summary>
/// <param name="name"></param>
/// <param name="restAddress"></param>
/// <returns></returns>
public static CoinGeckoEnvironment CreateCustom(
string name,
string restAddress)
=> new CoinGeckoEnvironment(name, restAddress);
string restAddressPublic,
string restAddressPro)
=> new CoinGeckoEnvironment(name, restAddressPublic, restAddressPro);
}
}
11 changes: 8 additions & 3 deletions CoinGecko.Net/Objects/CoinGeckoApiAddresses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,22 @@
public class CoinGeckoApiAddresses
{
/// <summary>
/// The address used by the BybitClient for the Spot rest API
/// The address used by the CoinGeckoRestClient for the public API
/// </summary>
public string RestClientAddress { get; set; } = "";
public string RestClientAddressPublic { get; set; } = "";
/// <summary>
/// The address used by the CoinGeckoRestClient for the pro API
/// </summary>
public string RestClientAddressPro { get; set; } = "";


/// <summary>
/// The default addresses to connect to the Bybit.com API
/// </summary>
public static CoinGeckoApiAddresses Default = new CoinGeckoApiAddresses
{
RestClientAddress = "https://api.coingecko.com/"
RestClientAddressPublic = "https://api.coingecko.com/",
RestClientAddressPro = "https://pro-api.coingecko.com/"
};
}
}
38 changes: 38 additions & 0 deletions CoinGecko.Net/Objects/CoinGeckoApiCredentials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using CryptoExchange.Net.Authentication;
using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;

namespace CoinGecko.Net.Objects
{
/// <summary>
/// CoinGecko API credentials
/// </summary>
public class CoinGeckoApiCredentials : ApiCredentials
{
/// <summary>
/// Wheter using a demo key
/// </summary>
public bool DemoKey { get; }

/// <summary>
/// ctor
/// </summary>
/// <param name="apiKey">The API key</param>
/// <param name="demoKey">Whether or not this is a demo key</param>
public CoinGeckoApiCredentials(string apiKey, bool demoKey = false) : base(apiKey, "-")
{
DemoKey = demoKey;
}

/// <summary>
/// Copy
/// </summary>
/// <returns></returns>
public override ApiCredentials Copy()
{
return new CoinGeckoApiCredentials(Key, DemoKey);
}
}
}
2 changes: 1 addition & 1 deletion CoinGecko.Net/Objects/Options/CoinGeckoRestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace CoinGecko.Net.Objects.Options
/// <summary>
/// CoinGecko Rest API options
/// </summary>
public class CoinGeckoRestOptions : RestExchangeOptions<CoinGeckoEnvironment>
public class CoinGeckoRestOptions : RestExchangeOptions<CoinGeckoEnvironment, CoinGeckoApiCredentials>
{
/// <summary>
/// Default options for the CoinGecko client
Expand Down

0 comments on commit b044d02

Please sign in to comment.