Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JKorf committed Nov 18, 2024
1 parent 6d5e59f commit a26bc98
Show file tree
Hide file tree
Showing 11 changed files with 382 additions and 93 deletions.
103 changes: 103 additions & 0 deletions CoinEx.Net.UnitTests/CoinExClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
using CryptoExchange.Net.Clients;
using System.Net.Http;
using CryptoExchange.Net.Converters.JsonNet;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using CoinEx.Net.Interfaces.Clients;

namespace CoinEx.Net.UnitTests
{
Expand Down Expand Up @@ -160,5 +163,105 @@ public void CheckInterfaces()
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<CoinExRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<CoinExSocketClient>();
}

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

var collection = new ServiceCollection();
collection.AddCoinEx(configuration.GetSection("CoinEx"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApiV2.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddCoinEx(configuration.GetSection("CoinEx"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApiV2.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddCoinEx(configuration.GetSection("CoinEx"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApiV2.BaseAddress;

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

var restClient = provider.GetRequiredService<ICoinExRestClient>();
var socketClient = provider.GetRequiredService<ICoinExSocketClient>();

Assert.That(((BaseApiClient)restClient.SpotApiV2).OutputOriginalData, Is.True);
Assert.That(((BaseApiClient)socketClient.SpotApiV2).OutputOriginalData, Is.False);
Assert.That(((BaseApiClient)restClient.SpotApiV2).AuthenticationProvider.ApiKey, Is.EqualTo("123"));
Assert.That(((BaseApiClient)socketClient.SpotApiV2).AuthenticationProvider.ApiKey, Is.EqualTo("456"));
Assert.That(((BaseApiClient)restClient.SpotApiV2).ClientOptions.Proxy.Host, Is.EqualTo("host"));
Assert.That(((BaseApiClient)restClient.SpotApiV2).ClientOptions.Proxy.Port, Is.EqualTo(80));
Assert.That(((BaseApiClient)socketClient.SpotApiV2).ClientOptions.Proxy.Host, Is.EqualTo("host2"));
Assert.That(((BaseApiClient)socketClient.SpotApiV2).ClientOptions.Proxy.Port, Is.EqualTo(81));
}
}
}
9 changes: 5 additions & 4 deletions CoinEx.Net.UnitTests/CoinExRestIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Testing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NUnit.Framework;
using System;
using System.Collections.Generic;
Expand All @@ -27,11 +28,11 @@ public override CoinExRestClient GetClient(ILoggerFactory loggerFactory)
var sec = Environment.GetEnvironmentVariable("APISECRET");

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

[Test]
Expand Down
23 changes: 10 additions & 13 deletions CoinEx.Net/Clients/CoinExRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using CryptoExchange.Net.Clients;
using CoinEx.Net.Interfaces.Clients.FuturesApi;
using CoinEx.Net.Clients.FuturesApi;
using Microsoft.Extensions.Options;

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

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

FuturesApi = AddApiClient(new CoinExRestClientFuturesApi(_logger, httpClient, options));
SpotApi = AddApiClient(new SpotApiV1.CoinExRestClientSpotApi(_logger, httpClient, options));
SpotApiV2 = AddApiClient(new SpotApiV2.CoinExRestClientSpotApi(_logger, httpClient, options));
FuturesApi = AddApiClient(new CoinExRestClientFuturesApi(_logger, httpClient, options.Value));
SpotApi = AddApiClient(new SpotApiV1.CoinExRestClientSpotApi(_logger, httpClient, options.Value));
SpotApiV2 = AddApiClient(new SpotApiV2.CoinExRestClientSpotApi(_logger, httpClient, options.Value));
}
#endregion

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

/// <inheritdoc />
Expand Down
30 changes: 10 additions & 20 deletions CoinEx.Net/Clients/CoinExSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using CryptoExchange.Net.Clients;
using CoinEx.Net.Interfaces.Clients.FuturesApi;
using CoinEx.Net.Clients.FuturesApi;
using Microsoft.Extensions.Options;

namespace CoinEx.Net.Clients
{
Expand All @@ -25,36 +26,27 @@ public class CoinExSocketClient : BaseSocketClient, ICoinExSocketClient

#region ctor

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

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

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

FuturesApi = AddApiClient(new CoinExSocketClientFuturesApi(_logger, options));
SpotApi = AddApiClient(new SpotApiV1.CoinExSocketClientSpotApi(_logger, options));
SpotApiV2 = AddApiClient(new SpotApiV2.CoinExSocketClientSpotApi(_logger, options));
FuturesApi = AddApiClient(new CoinExSocketClientFuturesApi(_logger, options.Value));
SpotApi = AddApiClient(new SpotApiV1.CoinExSocketClientSpotApi(_logger, options.Value));
SpotApiV2 = AddApiClient(new SpotApiV2.CoinExSocketClientSpotApi(_logger, options.Value));
}
#endregion

Expand All @@ -64,9 +56,7 @@ public CoinExSocketClient(Action<CoinExSocketOptions> optionsDelegate, ILoggerFa
/// <param name="optionsDelegate">Option configuration delegate</param>
public static void SetDefaultOptions(Action<CoinExSocketOptions> optionsDelegate)
{
var options = CoinExSocketOptions.Default.Copy();
optionsDelegate(options);
CoinExSocketOptions.Default = options;
CoinExSocketOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <inheritdoc />
Expand Down
4 changes: 3 additions & 1 deletion CoinEx.Net/CoinEx.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CryptoExchange.Net" Version="8.2.0" />
<PackageReference Include="Crc32.NET" Version="1.2.0" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\CryptoExchange.Net\CryptoExchange.Net\CryptoExchange.Net.csproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit a26bc98

Please sign in to comment.