Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CoinGecko.Net.UnitTests/RestRequestTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task ValidateAuthCalls()
var client = new CoinGeckoRestClient(opts =>
{
opts.AutoTimestamp = false;
opts.ApiCredentials = new Objects.CoinGeckoApiCredentials("123");
opts.ApiCredentials = new CoinGeckoCredentials("123");
});
var tester = new RestRequestValidator<CoinGeckoRestClient>(client, "Endpoints", "https://pro-api.coingecko.com", IsAuthenticated);
await tester.ValidateAsync(client => client.Api.GetApiUsageAsync(), "GetApiUsage");
Expand Down
8 changes: 1 addition & 7 deletions CoinGecko.Net/Clients/CoinGeckoRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace CoinGecko.Net.Clients
{
/// <inheritdoc />
public class CoinGeckoRestClient: BaseRestClient, ICoinGeckoRestClient
public class CoinGeckoRestClient: BaseRestClient<CoinGeckoEnvironment, CoinGeckoCredentials>, ICoinGeckoRestClient
{
/// <inheritdoc />
public ICoinGeckoRestClientApi Api { get; }
Expand Down Expand Up @@ -40,12 +40,6 @@ public CoinGeckoRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory
}
#endregion

/// <inheritdoc />
public void SetOptions(UpdateOptions options)
{
Api.SetOptions(options);
}

/// <summary>
/// Set the default options to be used when creating new clients
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions CoinGecko.Net/Clients/CoinGeckoRestClientApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
namespace CoinGecko.Net.Clients
{
/// <inheritdoc />
internal class CoinGeckoRestClientApi : RestApiClient, ICoinGeckoRestClientApi
internal class CoinGeckoRestClientApi : RestApiClient<CoinGeckoEnvironment, CoinGeckoAuthenticationProvider, CoinGeckoCredentials>, ICoinGeckoRestClientApi
{
private static readonly RequestDefinitionCache _definitions = new RequestDefinitionCache();
private readonly CoinGeckoRestOptions _options;
Expand Down Expand Up @@ -676,6 +676,7 @@ private string GetBaseAddress()
}

/// <inheritdoc />
protected override AuthenticationProvider CreateAuthenticationProvider(ApiCredentials credentials) => new CoinGeckoAuthenticationProvider((CoinGeckoApiCredentials)credentials);
protected override CoinGeckoAuthenticationProvider CreateAuthenticationProvider(CoinGeckoCredentials credentials)
=> new CoinGeckoAuthenticationProvider(credentials);
}
}
3 changes: 2 additions & 1 deletion CoinGecko.Net/CoinGecko.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<EnableConfigurationBindingGenerator>true</EnableConfigurationBindingGenerator>
<PackageReleaseNotes>https://github.com/JKorf/CoinGecko.Net?tab=readme-ov-file#release-notes</PackageReleaseNotes>
<NoWarn>$(NoWarn);SYSLIB1100;SYSLIB1101</NoWarn>
</PropertyGroup>
<PropertyGroup Label="AOT" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">
<IsAotCompatible>true</IsAotCompatible>
Expand Down Expand Up @@ -52,7 +53,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="10.8.0" />
<PackageReference Include="CryptoExchange.Net" Version="11.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="10.0.101">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
14 changes: 6 additions & 8 deletions CoinGecko.Net/CoinGeckoAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,25 @@

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

public override ApiCredentialsType[] SupportedCredentialTypes => [ApiCredentialsType.Hmac];

public CoinGeckoAuthenticationProvider(CoinGeckoApiCredentials credentials) : base(credentials)
public CoinGeckoAuthenticationProvider(CoinGeckoCredentials credentials) : base(credentials, credentials.Credential)
{
}

public override void ProcessRequest(RestApiClient apiClient, RestRequestConfiguration request)
{
request.QueryParameters ??= new Dictionary<string, object>();

if (_credentials.DemoKey)
request.QueryParameters.Add("x_cg_demo_api_key", _credentials.Key);
if (IsDemo)
request.QueryParameters.Add("x_cg_demo_api_key", Credential.Key);
else
request.QueryParameters.Add("x_cg_pro_api_key", _credentials.Key);
request.QueryParameters.Add("x_cg_pro_api_key", Credential.Key);
}
}
}
74 changes: 74 additions & 0 deletions CoinGecko.Net/CoinGeckoCredentials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using CryptoExchange.Net.Authentication;
using System;

namespace CoinGecko.Net
{
/// <summary>
/// CoinGecko credentials
/// </summary>
public class CoinGeckoCredentials : ApiCredentials
{
/// <summary>
/// Whether using a demo key
/// </summary>
public bool DemoKey { get; set; }

/// <summary>
/// API key credential
/// </summary>
public ApiKeyCredential Credential { get; set; }

/// <summary>
/// Create new credentials
/// </summary>
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
public CoinGeckoCredentials() { }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.

/// <summary>
/// Create new credentials providing Api key credentials
/// </summary>
/// <param name="credential">Api key credentials</param>
public CoinGeckoCredentials(ApiKeyCredential credential)
{
Credential = credential;
}

/// <summary>
/// Create new credentials providing Api key credentials
/// </summary>
/// <param name="key">The API key</param>
/// <param name="demoKey">Whether this is a demo key</param>
public CoinGeckoCredentials(string key, bool demoKey = false)
{
Credential = new ApiKeyCredential(key);
DemoKey = demoKey;
}

/// <summary>
/// Specify the ApiKey
/// </summary>
/// <param name="key">The API key</param>
/// <param name="demoKey">Whether this is a demo key</param>
public CoinGeckoCredentials WithApiKey(string key, bool demoKey = false)
{
if (Credential != null) throw new InvalidOperationException("Credentials already set");

DemoKey = demoKey;
Credential = new ApiKeyCredential(key);
return this;
}

/// <inheritdoc />
public override ApiCredentials Copy() => new CoinGeckoCredentials { Credential = Credential };

/// <inheritdoc />
public override void Validate()
{
if (Credential == null)
throw new ArgumentException($"No credentials provided on {GetType().Name}");

Credential.Validate();
}
}
}
20 changes: 0 additions & 20 deletions CoinGecko.Net/CryptoClientExtensions.cs

This file was deleted.

19 changes: 0 additions & 19 deletions CoinGecko.Net/ExtensionMethods/CryptoClientExtensions.cs

This file was deleted.

12 changes: 10 additions & 2 deletions CoinGecko.Net/ExtensionMethods/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static class ServiceCollectionExtensions
{
/// <summary>
/// Add services such as the ICoinGeckoRestClient. Configures the services based on the provided configuration.
/// See <see href="https://github.com/JKorf/CoinGecko.Net/blob/main/Examples/example-config.json" /> for an example of how to set up the configuration.
/// </summary>
/// <param name="services">The service collection</param>
/// <param name="configuration">The configuration(section) containing the options</param>
Expand All @@ -30,7 +31,15 @@ public static IServiceCollection AddCoinGecko(
IConfiguration configuration)
{
var options = new CoinGeckoRestOptions();
configuration.Bind(options);

try
{
configuration.Bind(options);
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException("Invalid configuration provided", ex);
}

var restEnvName = options.Environment?.Name ?? options.Environment?.Name ?? CoinGeckoEnvironment.Live.Name;
options.Environment = CoinGeckoEnvironment.GetEnvironmentByName(restEnvName) ?? options.Environment!;
Expand Down Expand Up @@ -69,7 +78,6 @@ private static IServiceCollection AddCoinGeckoCore(
return LibraryHelpers.CreateHttpClientMessageHandler(options);
}).SetHandlerLifetime(Timeout.InfiniteTimeSpan);

services.AddTransient<ICryptoRestClient, CryptoRestClient>();
return services;
}
}
Expand Down
11 changes: 3 additions & 8 deletions CoinGecko.Net/Interfaces/ICoinGeckoRestClient.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
using CryptoExchange.Net.Objects.Options;
using CryptoExchange.Net.Interfaces.Clients;
using CryptoExchange.Net.Objects.Options;

namespace CoinGecko.Net.Interfaces
{
/// <summary>
/// Client for accessing the CoinGecko Rest API.
/// </summary>
public interface ICoinGeckoRestClient
public interface ICoinGeckoRestClient : IRestClient<CoinGeckoCredentials>
{
/// <summary>
/// Api endpoints
/// </summary>
/// <see cref="ICoinGeckoRestClientApi"/>
ICoinGeckoRestClientApi Api { get; }

/// <summary>
/// Update specific options
/// </summary>
/// <param name="options">Options to update. Only specific options are changeable after the client has been created</param>
void SetOptions(UpdateOptions options);
}
}
2 changes: 1 addition & 1 deletion CoinGecko.Net/Interfaces/ICoinGeckoRestClientApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace CoinGecko.Net.Interfaces
/// <summary>
/// CoinGecko API endpoints
/// </summary>
public interface ICoinGeckoRestClientApi: IRestApiClient
public interface ICoinGeckoRestClientApi: IRestApiClient<CoinGeckoCredentials>
{
/// <summary>
/// Get asset categories
Expand Down
34 changes: 0 additions & 34 deletions CoinGecko.Net/Objects/CoinGeckoApiCredentials.cs

This file was deleted.

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, CoinGeckoApiCredentials>
public class CoinGeckoRestOptions : RestExchangeOptions<CoinGeckoEnvironment, CoinGeckoCredentials>
{
/// <summary>
/// Default options for the CoinGecko client
Expand Down
25 changes: 25 additions & 0 deletions Examples/example-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
// Options section, select this section during DI registration using Configuration.GetSection("CoinGecko")
"CoinGecko": {
// API credentials for both REST and Websocket client
"ApiCredentials": {
"DemoKey": false,
"Credential": {
"Key": "APIKEY"
}
},
// Set the environment by name
"Environment": {
"name": "live"
},
"RequestTimeout": "00:00:20",
"CachingEnabled": true,
"OutputOriginalData": true,
"Proxy": {
"Host": "https://127.0.0.1",
"Port": 8080,
"Login": "User",
"Password": "Pass"
}
}
}
Loading