Skip to content

Commit

Permalink
Client Configuration (#59)
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 OKXExchange class
Updated client constructors to accept IOptions from DI
Removed redundant OKXSocketClient constructor
  • Loading branch information
JKorf authored Nov 19, 2024
1 parent ff40aa4 commit 7d4ba15
Show file tree
Hide file tree
Showing 16 changed files with 419 additions and 103 deletions.
10 changes: 6 additions & 4 deletions OKX.Net.UnitTests/OKXRestIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OKX.Net.Objects.Options;

namespace OKX.Net.UnitTests
{
Expand All @@ -28,11 +30,11 @@ public override OKXRestClient GetClient(ILoggerFactory loggerFactory)
var pass = Environment.GetEnvironmentVariable("APIPASS");

Authenticated = key != null && sec != null;
return new OKXRestClient(null, loggerFactory, opts =>
return new OKXRestClient(null, loggerFactory, Options.Create(new OKXRestOptions
{
opts.OutputOriginalData = true;
opts.ApiCredentials = Authenticated ? new OKXApiCredentials(key, sec, pass) : null;
});
OutputOriginalData = true,
ApiCredentials = Authenticated ? new OKXApiCredentials(key, sec, pass) : null
}));
}

[Test]
Expand Down
106 changes: 106 additions & 0 deletions OKX.Net.UnitTests/OXKRestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using CryptoExchange.Net.Clients;
using OKX.Net.Objects;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using CryptoExchange.Net.Objects;
using OKX.Net.Interfaces.Clients;

namespace OKX.Net.UnitTests
{
Expand Down Expand Up @@ -101,5 +105,107 @@ public void CheckInterfaces()
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<OKXRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<OKXSocketClient>();
}


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

var collection = new ServiceCollection();
collection.AddOKX(configuration.GetSection("OKX"));
var provider = collection.BuildServiceProvider();

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

var address = client.UnifiedApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddOKX(configuration.GetSection("OKX"));
var provider = collection.BuildServiceProvider();

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

var address = client.UnifiedApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddOKX(configuration.GetSection("OKX"));
var provider = collection.BuildServiceProvider();

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

var address = client.UnifiedApi.BaseAddress;

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

[Test]
public void TestConstructorConfiguration()
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "ApiCredentials:Key", "123" },
{ "ApiCredentials:Secret", "456" },
{ "ApiCredentials:PassPhrase", "222" },
{ "Socket:ApiCredentials:Key", "456" },
{ "Socket:ApiCredentials:Secret", "789" },
{ "Socket:ApiCredentials:PassPhrase", "111" },
{ "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.AddOKX(configuration);
var provider = collection.BuildServiceProvider();

var restClient = provider.GetRequiredService<IOKXRestClient>();
var socketClient = provider.GetRequiredService<IOKXSocketClient>();

Assert.That(((BaseApiClient)restClient.UnifiedApi).OutputOriginalData, Is.True);
Assert.That(((BaseApiClient)socketClient.UnifiedApi).OutputOriginalData, Is.False);
Assert.That(((BaseApiClient)restClient.UnifiedApi).AuthenticationProvider.ApiKey, Is.EqualTo("123"));
Assert.That(((BaseApiClient)socketClient.UnifiedApi).AuthenticationProvider.ApiKey, Is.EqualTo("456"));
Assert.That(((BaseApiClient)restClient.UnifiedApi).ClientOptions.Proxy.Host, Is.EqualTo("host"));
Assert.That(((BaseApiClient)restClient.UnifiedApi).ClientOptions.Proxy.Port, Is.EqualTo(80));
Assert.That(((BaseApiClient)socketClient.UnifiedApi).ClientOptions.Proxy.Host, Is.EqualTo("host2"));
Assert.That(((BaseApiClient)socketClient.UnifiedApi).ClientOptions.Proxy.Port, Is.EqualTo(81));
}
}
}
19 changes: 8 additions & 11 deletions OKX.Net/Clients/OKXRestClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CryptoExchange.Net.Clients;
using Microsoft.Extensions.Options;
using OKX.Net.Clients.UnifiedApi;
using OKX.Net.Interfaces.Clients;
using OKX.Net.Interfaces.Clients.UnifiedApi;
Expand All @@ -22,25 +23,23 @@ public class OKXRestClient : BaseRestClient, IOKXRestClient
/// Create a new instance of the OKXRestClient using provided options
/// </summary>
/// <param name="optionsDelegate">Option configuration delegate</param>
public OKXRestClient(Action<OKXRestOptions>? optionsDelegate = null) : this(null, null, optionsDelegate)
public OKXRestClient(Action<OKXRestOptions>? optionsDelegate = null)
: this(null, null, Options.Create(ApplyOptionsDelegate(optionsDelegate)))
{
}

/// <summary>
/// Create a new instance of the OKXRestClient
/// </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 OKXRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, Action<OKXRestOptions>? optionsDelegate = null)
public OKXRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, IOptions<OKXRestOptions> options)
: base(loggerFactory, "OKX")
{
var options = OKXRestOptions.Default.Copy();
if (optionsDelegate != null)
optionsDelegate(options);
Initialize(options);
Initialize(options.Value);

UnifiedApi = AddApiClient(new OKXRestClientUnifiedApi(_logger, httpClient, options));
UnifiedApi = AddApiClient(new OKXRestClientUnifiedApi(_logger, httpClient, options.Value));
}
#endregion

Expand All @@ -51,9 +50,7 @@ public OKXRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, Acti
/// <param name="optionsDelegate">Callback for setting the options</param>
public static void SetDefaultOptions(Action<OKXRestOptions> optionsDelegate)
{
var options = OKXRestOptions.Default.Copy();
optionsDelegate(options);
OKXRestOptions.Default = options;
OKXRestOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <summary>
Expand Down
25 changes: 8 additions & 17 deletions OKX.Net/Clients/OKXSocketClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CryptoExchange.Net.Clients;
using Microsoft.Extensions.Options;
using OKX.Net.Clients.UnifiedApi;
using OKX.Net.Interfaces.Clients;
using OKX.Net.Interfaces.Clients.UnifiedApi;
Expand All @@ -16,34 +17,26 @@ public class OKXSocketClient : BaseSocketClient, IOKXSocketClient
public IOKXSocketClientUnifiedApi UnifiedApi { get; }

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

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

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

UnifiedApi = AddApiClient(new OKXSocketClientUnifiedApi(_logger, options));
UnifiedApi = AddApiClient(new OKXSocketClientUnifiedApi(_logger, options.Value));
}
#endregion

Expand All @@ -54,9 +47,7 @@ public OKXSocketClient(Action<OKXSocketOptions> optionsDelegate, ILoggerFactory?
/// <param name="optionsDelegate"></param>
public static void SetDefaultOptions(Action<OKXSocketOptions> optionsDelegate)
{
var options = OKXSocketOptions.Default.Copy();
optionsDelegate(options);
OKXSocketOptions.Default = options;
OKXSocketOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <inheritdoc />
Expand Down
2 changes: 1 addition & 1 deletion OKX.Net/Clients/UnifiedApi/OKXRestClientUnifiedApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal OKXRestClientUnifiedApi(ILogger logger, HttpClient? httpClient, OKXRest

_ref = !string.IsNullOrEmpty(options.BrokerId) ? options.BrokerId! : "1425d83a94fbBCDE";

if (options.Environment.EnvironmentName == TradeEnvironmentNames.Testnet)
if (options.Environment.Name == TradeEnvironmentNames.Testnet)
{
StandardRequestHeaders = new Dictionary<string, string>
{
Expand Down
2 changes: 1 addition & 1 deletion OKX.Net/Clients/UnifiedApi/OKXSocketClientUnifiedApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal OKXSocketClientUnifiedApi(ILogger logger, OKXSocketOptions options) :

_ref = !string.IsNullOrEmpty(options.BrokerId) ? options.BrokerId! : "078ee129065aBCDE";

_demoTrading = options.Environment.EnvironmentName == TradeEnvironmentNames.Testnet;
_demoTrading = options.Environment.Name == TradeEnvironmentNames.Testnet;

RegisterPeriodicQuery("Ping", TimeSpan.FromSeconds(20), x => new OKXPingQuery(), null);

Expand Down
Loading

0 comments on commit 7d4ba15

Please sign in to comment.