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 BingXExchange class
Updated client constructors to accept IOptions from DI
Removed redundant BingXSocketClient constructor
  • Loading branch information
JKorf authored Nov 19, 2024
1 parent a481808 commit 711111b
Show file tree
Hide file tree
Showing 13 changed files with 406 additions and 92 deletions.
103 changes: 103 additions & 0 deletions BingX.Net.UnitTests/BingXRestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
using System.Net.Http;
using System.Collections.Generic;
using CryptoExchange.Net.Converters.JsonNet;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using CryptoExchange.Net.Objects;
using BingX.Net.Interfaces.Clients;

namespace BingX.Net.UnitTests
{
Expand Down Expand Up @@ -49,5 +53,104 @@ public void CheckInterfaces()
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<BingXRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<BingXSocketClient>();
}


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

var collection = new ServiceCollection();
collection.AddBingX(configuration.GetSection("BingX"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddBingX(configuration.GetSection("BingX"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddBingX(configuration.GetSection("BingX"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

[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.AddBingX(configuration);
var provider = collection.BuildServiceProvider();

var restClient = provider.GetRequiredService<IBingXRestClient>();
var socketClient = provider.GetRequiredService<IBingXSocketClient>();

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 BingX.Net.UnitTests/BingXRestIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using BingX.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,11 +26,11 @@ public override BingXRestClient GetClient(ILoggerFactory loggerFactory)
var sec = Environment.GetEnvironmentVariable("APISECRET");

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

[Test]
Expand Down
4 changes: 2 additions & 2 deletions BingX.Net/BingX.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="Microsoft.CodeAnalysis.NetAnalyzers" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
95 changes: 80 additions & 15 deletions BingX.Net/BingX.Net.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions BingX.Net/BingXEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ internal BingXEnvironment(
SocketClientSwapAddress = streamSwapAddress;
}

/// <summary>
/// ctor for DI, use <see cref="CreateCustom"/> for creating a custom environment
/// </summary>
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
public BingXEnvironment() : base(TradeEnvironmentNames.Live)
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
{ }

/// <summary>
/// Get the BingX environment by name
/// </summary>
public static BingXEnvironment? GetEnvironmentByName(string? name)
=> name switch
{
TradeEnvironmentNames.Live => Live,
"" => Live,
null => Live,
_ => default
};

/// <summary>
/// Live environment
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions BingX.Net/BingXExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public static class BingXExchange
/// </summary>
public static string ExchangeName => "BingX";

/// <summary>
/// Exchange name
/// </summary>
public static string DisplayName => "BingX";

/// <summary>
/// Url to exchange image
/// </summary>
public static string ImageUrl { get; } = "https://raw.githubusercontent.com/JKorf/BingX.Net/master/BingX.Net/Icon/BingX.png";

/// <summary>
/// Url to the main website
/// </summary>
Expand Down
Loading

0 comments on commit 711111b

Please sign in to comment.