Skip to content

Commit

Permalink
Client Configuration (#20)
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 MexcExchange class
Updated client constructors to accept IOptions from DI
Removed redundant MexcSocketClient constructor
  • Loading branch information
JKorf authored Nov 19, 2024
1 parent f74f35d commit 24c684c
Show file tree
Hide file tree
Showing 14 changed files with 421 additions and 90 deletions.
104 changes: 104 additions & 0 deletions Mexc.Net.UnitTests/MexcClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
using System.Diagnostics;
using System.Reflection;
using CryptoExchange.Net.Converters.JsonNet;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Mexc.Net.Interfaces.Clients;
using CryptoExchange.Net.Objects;

namespace Mexc.Net.UnitTests
{
Expand Down Expand Up @@ -90,5 +94,105 @@ public void CheckInterfaces()
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<MexcRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<MexcSocketClient>();
}

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

var collection = new ServiceCollection();
collection.AddMexc(configuration.GetSection("Mexc"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddMexc(configuration.GetSection("Mexc"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddMexc(configuration.GetSection("Mexc"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

Assert.That(address, Is.EqualTo("https://api.mexc.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.AddMexc(configuration);
var provider = collection.BuildServiceProvider();

var restClient = provider.GetRequiredService<IMexcRestClient>();
var socketClient = provider.GetRequiredService<IMexcSocketClient>();

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 Mexc.Net.UnitTests/MexcRestIntegrationTests.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 Mexc.Net.UnitTests
{
Expand All @@ -27,11 +28,11 @@ public override MexcRestClient GetClient(ILoggerFactory loggerFactory)
var sec = Environment.GetEnvironmentVariable("APISECRET");

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

[Test]
Expand Down
25 changes: 14 additions & 11 deletions Mexc.Net/Clients/MexcRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Mexc.Net.Interfaces.Clients.SpotApi;
using Mexc.Net.Objects.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Net.Http;

Expand All @@ -21,24 +22,22 @@ public class MexcRestClient : BaseRestClient, IMexcRestClient
/// Create a new instance of the MexcRestClient using provided options
/// </summary>
/// <param name="optionsDelegate">Option configuration delegate</param>
public MexcRestClient(Action<MexcRestOptions>? optionsDelegate = null) : this(null, null, optionsDelegate)
public MexcRestClient(Action<MexcRestOptions>? optionsDelegate = null)
: this(null, null, Options.Create(ApplyOptionsDelegate(optionsDelegate)))
{
}

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

SpotApi = AddApiClient(new MexcRestClientSpotApi(_logger, httpClient, options));
SpotApi = AddApiClient(new MexcRestClientSpotApi(_logger, httpClient, options.Value));
}

#endregion
Expand All @@ -49,9 +48,13 @@ public MexcRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, Act
/// <param name="optionsDelegate">Option configuration delegate</param>
public static void SetDefaultOptions(Action<MexcRestOptions> optionsDelegate)
{
var options = MexcRestOptions.Default.Copy();
optionsDelegate(options);
MexcRestOptions.Default = options;
MexcRestOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <inheritdoc />
public void SetApiCredentials(ApiCredentials apiCredentials)
{
SpotApi.SetApiCredentials(apiCredentials);
}
}
}
26 changes: 8 additions & 18 deletions Mexc.Net/Clients/MexcSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Mexc.Net.Interfaces.Clients.SpotApi;
using Mexc.Net.Objects.Options;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;
Expand All @@ -18,34 +19,25 @@ public class MexcSocketClient : BaseSocketClient, IMexcSocketClient
public IMexcSocketClientSpotApi SpotApi { get; set; }

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

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

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

SpotApi = AddApiClient(new MexcSocketClientSpotApi(_logger, options));
SpotApi = AddApiClient(new MexcSocketClientSpotApi(_logger, options.Value));
}
#endregion

Expand All @@ -55,9 +47,7 @@ public MexcSocketClient(Action<MexcSocketOptions> optionsDelegate, ILoggerFactor
/// <param name="optionsDelegate">Option configuration delegate</param>
public static void SetDefaultOptions(Action<MexcSocketOptions> optionsDelegate)
{
var options = MexcSocketOptions.Default.Copy();
optionsDelegate(options);
MexcSocketOptions.Default = options;
MexcSocketOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <inheritdoc />
Expand Down
Loading

0 comments on commit 24c684c

Please sign in to comment.