Skip to content

Commit

Permalink
Client Configuration (#3)
Browse files Browse the repository at this point in the history
Updated CryptoExchange.Net to version 8.3.0
Added support for loading client settings from IConfiguration
Added DI registration method for configuring Rest and Socket options at the same time
Added DisplayName and ImageUrl properties to CoinbaseExchange class
Updated client constructors to accept IOptions from DI
Removed redundant CoinbaseSocketClient constructor
  • Loading branch information
JKorf authored Nov 19, 2024
1 parent 3593ffb commit ad95cfe
Show file tree
Hide file tree
Showing 13 changed files with 406 additions and 92 deletions.
104 changes: 104 additions & 0 deletions Coinbase.Net.UnitTests/CoinbaseRestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using System.Collections.Generic;
using System.Net.Http;
using Coinbase.Net.Clients;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using CryptoExchange.Net.Objects;
using Coinbase.Net.Interfaces.Clients;

namespace Coinbase.Net.UnitTests
{
Expand All @@ -17,5 +21,105 @@ public void CheckInterfaces()
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<CoinbaseRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<CoinbaseSocketClient>();
}

[Test]
[TestCase(TradeEnvironmentNames.Live, "https://api.coinbase.com")]
[TestCase("", "https://api.coinbase.com")]
public void TestConstructorEnvironments(string environmentName, string expected)
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "Coinbase:Environment:Name", environmentName },
}).Build();

var collection = new ServiceCollection();
collection.AddCoinbase(configuration.GetSection("Coinbase"));
var provider = collection.BuildServiceProvider();

var client = provider.GetRequiredService<ICoinbaseRestClient>();

var address = client.AdvancedTradeApi.BaseAddress;

Assert.That(address, Is.EqualTo(expected));
}

[Test]
public void TestConstructorNullEnvironment()
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "Coinbase", null },
}).Build();

var collection = new ServiceCollection();
collection.AddCoinbase(configuration.GetSection("Coinbase"));
var provider = collection.BuildServiceProvider();

var client = provider.GetRequiredService<ICoinbaseRestClient>();

var address = client.AdvancedTradeApi.BaseAddress;

Assert.That(address, Is.EqualTo("https://api.coinbase.com"));
}

[Test]
public void TestConstructorApiOverwriteEnvironment()
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "Coinbase:Environment:Name", "test" },
{ "Coinbase:Rest:Environment:Name", "live" },
}).Build();

var collection = new ServiceCollection();
collection.AddCoinbase(configuration.GetSection("Coinbase"));
var provider = collection.BuildServiceProvider();

var client = provider.GetRequiredService<ICoinbaseRestClient>();

var address = client.AdvancedTradeApi.BaseAddress;

Assert.That(address, Is.EqualTo("https://api.coinbase.com"));
}

[Test]
public void TestConstructorConfiguration()
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "ApiCredentials:Key", "123" },
{ "ApiCredentials:Secret", "456" },
{ "ApiCredentials:Memo", "000" },
{ "Socket:ApiCredentials:Key", "456" },
{ "Socket:ApiCredentials:Secret", "789" },
{ "Socket:ApiCredentials:Memo", "xxx" },
{ "Rest:OutputOriginalData", "true" },
{ "Socket:OutputOriginalData", "false" },
{ "Rest:Proxy:Host", "host" },
{ "Rest:Proxy:Port", "80" },
{ "Socket:Proxy:Host", "host2" },
{ "Socket:Proxy:Port", "81" },
}).Build();

var collection = new ServiceCollection();
collection.AddCoinbase(configuration);
var provider = collection.BuildServiceProvider();

var restClient = provider.GetRequiredService<ICoinbaseRestClient>();
var socketClient = provider.GetRequiredService<ICoinbaseSocketClient>();

Assert.That(((BaseApiClient)restClient.AdvancedTradeApi).OutputOriginalData, Is.True);
Assert.That(((BaseApiClient)socketClient.AdvancedTradeApi).OutputOriginalData, Is.False);
Assert.That(((BaseApiClient)restClient.AdvancedTradeApi).AuthenticationProvider.ApiKey, Is.EqualTo("123"));
Assert.That(((BaseApiClient)socketClient.AdvancedTradeApi).AuthenticationProvider.ApiKey, Is.EqualTo("456"));
Assert.That(((BaseApiClient)restClient.AdvancedTradeApi).ClientOptions.Proxy.Host, Is.EqualTo("host"));
Assert.That(((BaseApiClient)restClient.AdvancedTradeApi).ClientOptions.Proxy.Port, Is.EqualTo(80));
Assert.That(((BaseApiClient)socketClient.AdvancedTradeApi).ClientOptions.Proxy.Host, Is.EqualTo("host2"));
Assert.That(((BaseApiClient)socketClient.AdvancedTradeApi).ClientOptions.Proxy.Port, Is.EqualTo(81));
}
}
}
11 changes: 6 additions & 5 deletions Coinbase.Net.UnitTests/CoinbaseRestIntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Coinbase.Net.Clients;
using CryptoExchange.Net.Testing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NUnit.Framework;
using System;
using System.Diagnostics;
Expand All @@ -25,12 +26,12 @@ public override CoinbaseRestClient GetClient(ILoggerFactory loggerFactory)
var sec = Environment.GetEnvironmentVariable("APISECRET");

Authenticated = key != null && sec != null;
return new CoinbaseRestClient(null, loggerFactory, opts =>
return new CoinbaseRestClient(null, loggerFactory, Options.Create(new Objects.Options.CoinbaseRestOptions
{
opts.AutoTimestamp = false;
opts.OutputOriginalData = true;
opts.ApiCredentials = Authenticated ? new CryptoExchange.Net.Authentication.ApiCredentials(key, sec) : null;
});
AutoTimestamp = false,
OutputOriginalData = true,
ApiCredentials = Authenticated ? new CryptoExchange.Net.Authentication.ApiCredentials(key, sec) : null
}));
}

[Test]
Expand Down
19 changes: 8 additions & 11 deletions Coinbase.Net/Clients/CoinbaseRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using CryptoExchange.Net.Clients;
using Coinbase.Net.Interfaces.Clients.AdvancedTradeApi;
using Coinbase.Net.Clients.AdvancedTradeApi;
using Microsoft.Extensions.Options;

namespace Coinbase.Net.Clients
{
Expand All @@ -27,24 +28,22 @@ public class CoinbaseRestClient : BaseRestClient, ICoinbaseRestClient
/// Create a new instance of the CoinbaseRestClient using provided options
/// </summary>
/// <param name="optionsDelegate">Option configuration delegate</param>
public CoinbaseRestClient(Action<CoinbaseRestOptions>? optionsDelegate = null) : this(null, null, optionsDelegate)
public CoinbaseRestClient(Action<CoinbaseRestOptions>? optionsDelegate = null)
: this(null, null, Options.Create(ApplyOptionsDelegate(optionsDelegate)))
{
}

/// <summary>
/// Create a new instance of the CoinbaseRestClient using provided options
/// </summary>
/// <param name="optionsDelegate">Option configuration delegate</param>
/// <param name="options">Option configuration</param>
/// <param name="loggerFactory">The logger factory</param>
/// <param name="httpClient">Http client for this client</param>
public CoinbaseRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, Action<CoinbaseRestOptions>? optionsDelegate = null) : base(loggerFactory, "Coinbase")
public CoinbaseRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, IOptions<CoinbaseRestOptions> options) : base(loggerFactory, "Coinbase")
{
var options = CoinbaseRestOptions.Default.Copy();
if (optionsDelegate != null)
optionsDelegate(options);
Initialize(options);
Initialize(options.Value);

AdvancedTradeApi = AddApiClient(new CoinbaseRestClientAdvancedTradeApi(this, _logger, httpClient, options));
AdvancedTradeApi = AddApiClient(new CoinbaseRestClientAdvancedTradeApi(this, _logger, httpClient, options.Value));
}

#endregion
Expand All @@ -55,9 +54,7 @@ public CoinbaseRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory,
/// <param name="optionsDelegate">Option configuration delegate</param>
public static void SetDefaultOptions(Action<CoinbaseRestOptions> optionsDelegate)
{
var options = CoinbaseRestOptions.Default.Copy();
optionsDelegate(options);
CoinbaseRestOptions.Default = options;
CoinbaseRestOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <inheritdoc />
Expand Down
25 changes: 8 additions & 17 deletions Coinbase.Net/Clients/CoinbaseSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Coinbase.Net.Objects.Options;
using Coinbase.Net.Interfaces.Clients.AdvancedTradeApi;
using Coinbase.Net.Clients.AdvancedTradeApi;
using Microsoft.Extensions.Options;

namespace Coinbase.Net.Clients
{
Expand All @@ -23,34 +24,26 @@ public class CoinbaseSocketClient : BaseSocketClient, ICoinbaseSocketClient
#endregion

#region constructor/destructor
/// <summary>
/// Create a new instance of CoinbaseSocketClient
/// </summary>
/// <param name="loggerFactory">The logger factory</param>
public CoinbaseSocketClient(ILoggerFactory? loggerFactory = null) : this((x) => { }, loggerFactory)
{
}

/// <summary>
/// Create a new instance of CoinbaseSocketClient
/// </summary>
/// <param name="optionsDelegate">Option configuration delegate</param>
public CoinbaseSocketClient(Action<CoinbaseSocketOptions> optionsDelegate) : this(optionsDelegate, null)
public CoinbaseSocketClient(Action<CoinbaseSocketOptions>? optionsDelegate = null)
: this(Options.Create(ApplyOptionsDelegate(optionsDelegate)), null)
{
}

/// <summary>
/// Create a new instance of CoinbaseSocketClient
/// </summary>
/// <param name="loggerFactory">The logger factory</param>
/// <param name="optionsDelegate">Option configuration delegate</param>
public CoinbaseSocketClient(Action<CoinbaseSocketOptions>? optionsDelegate, ILoggerFactory? loggerFactory = null) : base(loggerFactory, "Coinbase")
/// <param name="options">Option configuration</param>
public CoinbaseSocketClient(IOptions<CoinbaseSocketOptions> options, ILoggerFactory? loggerFactory = null) : base(loggerFactory, "Coinbase")
{
var options = CoinbaseSocketOptions.Default.Copy();
optionsDelegate?.Invoke(options);
Initialize(options);
Initialize(options.Value);

AdvancedTradeApi = AddApiClient(new CoinbaseSocketClientAdvancedTradeApi(_logger, options));
AdvancedTradeApi = AddApiClient(new CoinbaseSocketClientAdvancedTradeApi(_logger, options.Value));
}
#endregion

Expand All @@ -60,9 +53,7 @@ public CoinbaseSocketClient(Action<CoinbaseSocketOptions>? optionsDelegate, ILog
/// <param name="optionsDelegate">Option configuration delegate</param>
public static void SetDefaultOptions(Action<CoinbaseSocketOptions> optionsDelegate)
{
var options = CoinbaseSocketOptions.Default.Copy();
optionsDelegate(options);
CoinbaseSocketOptions.Default = options;
CoinbaseSocketOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <inheritdoc />
Expand Down
4 changes: 2 additions & 2 deletions Coinbase.Net/Coinbase.Net.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<LangVersion>12.0</LangVersion>
Expand Down Expand Up @@ -48,7 +48,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="8.2.0" />
<PackageReference Include="CryptoExchange.Net" Version="8.3.0" />
<PackageReference Include="jose-jwt" Version="5.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
Loading

0 comments on commit ad95cfe

Please sign in to comment.