Skip to content

Commit

Permalink
Client Configuration (#12)
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 GateIoExchange class
Updated client constructors to accept IOptions from DI
Removed redundant GateIoSocketClient constructor
  • Loading branch information
JKorf authored Nov 19, 2024
1 parent 55596bb commit 4cd74d6
Show file tree
Hide file tree
Showing 13 changed files with 407 additions and 94 deletions.
102 changes: 102 additions & 0 deletions GateIo.Net.UnitTests/GateIoRestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
using System.Net.Http;
using GateIo.Net.Clients;
using GateIo.Net;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using GateIo.Net.Interfaces.Clients;
using CryptoExchange.Net.Objects;

namespace Gate.io.Net.UnitTests
{
Expand Down Expand Up @@ -43,5 +47,103 @@ public void CheckInterfaces()
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<GateIoRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<GateIoSocketClient>();
}

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

var collection = new ServiceCollection();
collection.AddGateIo(configuration.GetSection("GateIo"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddGateIo(configuration.GetSection("GateIo"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddGateIo(configuration.GetSection("GateIo"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var restClient = provider.GetRequiredService<IGateIoRestClient>();
var socketClient = provider.GetRequiredService<IGateIoSocketClient>();

Assert.That(((BaseApiClient)restClient.SpotApi).OutputOriginalData, Is.True);
Assert.That(((BaseApiClient)socketClient.SpotApi).OutputOriginalData, Is.False);
Assert.That(((BaseApiClient)restClient.SpotApi).AuthenticationProvider.ApiKey, Is.EqualTo("123"));
Assert.That(((BaseApiClient)socketClient.SpotApi).AuthenticationProvider.ApiKey, Is.EqualTo("456"));
Assert.That(((BaseApiClient)restClient.SpotApi).ClientOptions.Proxy.Host, Is.EqualTo("host"));
Assert.That(((BaseApiClient)restClient.SpotApi).ClientOptions.Proxy.Port, Is.EqualTo(80));
Assert.That(((BaseApiClient)socketClient.SpotApi).ClientOptions.Proxy.Host, Is.EqualTo("host2"));
Assert.That(((BaseApiClient)socketClient.SpotApi).ClientOptions.Proxy.Port, Is.EqualTo(81));
}
}
}
9 changes: 5 additions & 4 deletions GateIo.Net.UnitTests/GateIoRestIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;

namespace GateIo.Net.UnitTests
{
Expand All @@ -27,11 +28,11 @@ public override GateIoRestClient GetClient(ILoggerFactory loggerFactory)
var sec = Environment.GetEnvironmentVariable("APISECRET");

Authenticated = key != null && sec != null;
return new GateIoRestClient(null, loggerFactory, opts =>
return new GateIoRestClient(null, loggerFactory, Options.Create(new Objects.Options.GateIoRestOptions
{
opts.OutputOriginalData = true;
opts.ApiCredentials = Authenticated ? new ApiCredentials(key, sec) : null;
});
OutputOriginalData = true,
ApiCredentials = Authenticated ? new ApiCredentials(key, sec) : null
}));
}

[Test]
Expand Down
21 changes: 9 additions & 12 deletions GateIo.Net/Clients/GateIoRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using GateIo.Net.Clients.FuturesApi;
using CryptoExchange.Net.Clients;
using GateIo.Net.Interfaces.Clients.PerpetualFuturesApi;
using Microsoft.Extensions.Options;

namespace GateIo.Net.Clients
{
Expand All @@ -30,25 +31,23 @@ public class GateIoRestClient : BaseRestClient, IGateIoRestClient
/// Create a new instance of the GateIoRestClient using provided options
/// </summary>
/// <param name="optionsDelegate">Option configuration delegate</param>
public GateIoRestClient(Action<GateIoRestOptions>? optionsDelegate = null) : this(null, null, optionsDelegate)
public GateIoRestClient(Action<GateIoRestOptions>? optionsDelegate = null)
: this(null, null, Options.Create(ApplyOptionsDelegate(optionsDelegate)))
{
}

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

SpotApi = AddApiClient(new GateIoRestClientSpotApi(_logger, httpClient, options));
PerpetualFuturesApi = AddApiClient(new GateIoRestClientPerpetualFuturesApi(_logger, httpClient, options));
SpotApi = AddApiClient(new GateIoRestClientSpotApi(_logger, httpClient, options.Value));
PerpetualFuturesApi = AddApiClient(new GateIoRestClientPerpetualFuturesApi(_logger, httpClient, options.Value));
}

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

/// <inheritdoc />
Expand Down
27 changes: 9 additions & 18 deletions GateIo.Net/Clients/GateIoSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using GateIo.Net.Interfaces.Clients.SpotApi;
using GateIo.Net.Objects.Options;
using GateIo.Net.Interfaces.Clients.PerpetualFuturesApi;
using Microsoft.Extensions.Options;

namespace GateIo.Net.Clients
{
Expand All @@ -28,35 +29,27 @@ public class GateIoSocketClient : BaseSocketClient, IGateIoSocketClient
#endregion

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

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

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

SpotApi = AddApiClient(new GateIoSocketClientSpotApi(_logger, options));
PerpetualFuturesApi = AddApiClient(new GateIoSocketClientPerpetualFuturesApi(_logger, options));
SpotApi = AddApiClient(new GateIoSocketClientSpotApi(_logger, options.Value));
PerpetualFuturesApi = AddApiClient(new GateIoSocketClientPerpetualFuturesApi(_logger, options.Value));
}
#endregion

Expand All @@ -66,9 +59,7 @@ public GateIoSocketClient(Action<GateIoSocketOptions>? optionsDelegate, ILoggerF
/// <param name="optionsDelegate">Option configuration delegate</param>
public static void SetDefaultOptions(Action<GateIoSocketOptions> optionsDelegate)
{
var options = GateIoSocketOptions.Default.Copy();
optionsDelegate(options);
GateIoSocketOptions.Default = options;
GateIoSocketOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <inheritdoc />
Expand Down
Loading

0 comments on commit 4cd74d6

Please sign in to comment.