Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client Configuration #1

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions WhiteBit.Net.UnitTests/WhiteBitRestClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using CryptoExchange.Net.Converters.JsonNet;
using CryptoExchange.Net.Objects;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NUnit.Framework;
using System.Collections.Generic;
using System.Net.Http;
using WhiteBit.Net.Clients;
using WhiteBit.Net.Interfaces.Clients;

namespace WhiteBit.Net.UnitTests
{
@@ -42,5 +46,105 @@ public void CheckInterfaces()
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<WhiteBitRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<WhiteBitSocketClient>();
}

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

var collection = new ServiceCollection();
collection.AddWhiteBit(configuration.GetSection("WhiteBit"));
var provider = collection.BuildServiceProvider();

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

var address = client.V4Api.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddWhiteBit(configuration.GetSection("WhiteBit"));
var provider = collection.BuildServiceProvider();

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

var address = client.V4Api.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddWhiteBit(configuration.GetSection("WhiteBit"));
var provider = collection.BuildServiceProvider();

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

var address = client.V4Api.BaseAddress;

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

var restClient = provider.GetRequiredService<IWhiteBitRestClient>();
var socketClient = provider.GetRequiredService<IWhiteBitSocketClient>();

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

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

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

[Test]
19 changes: 8 additions & 11 deletions WhiteBit.Net/Clients/WhiteBitRestClient.cs
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
using CryptoExchange.Net.Clients;
using WhiteBit.Net.Interfaces.Clients.V4Api;
using WhiteBit.Net.Clients.V4Api;
using Microsoft.Extensions.Options;

namespace WhiteBit.Net.Clients
{
@@ -28,24 +29,22 @@ public class WhiteBitRestClient : BaseRestClient, IWhiteBitRestClient
/// Create a new instance of the WhiteBitRestClient using provided options
/// </summary>
/// <param name="optionsDelegate">Option configuration delegate</param>
public WhiteBitRestClient(Action<WhiteBitRestOptions>? optionsDelegate = null) : this(null, null, optionsDelegate)
public WhiteBitRestClient(Action<WhiteBitRestOptions>? optionsDelegate = null)
: this(null, null, Options.Create(ApplyOptionsDelegate(optionsDelegate)))
{
}

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

V4Api = AddApiClient(new WhiteBitRestClientV4Api(_logger, httpClient, options));
V4Api = AddApiClient(new WhiteBitRestClientV4Api(_logger, httpClient, options.Value));
}

#endregion
@@ -56,9 +55,7 @@ public WhiteBitRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory,
/// <param name="optionsDelegate">Option configuration delegate</param>
public static void SetDefaultOptions(Action<WhiteBitRestOptions> optionsDelegate)
{
var options = WhiteBitRestOptions.Default.Copy();
optionsDelegate(options);
WhiteBitRestOptions.Default = options;
WhiteBitRestOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <inheritdoc />
28 changes: 9 additions & 19 deletions WhiteBit.Net/Clients/WhiteBitSocketClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CryptoExchange.Net.Authentication;
using CryptoExchange.Net.Clients;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using WhiteBit.Net.Clients.V4Api;
using WhiteBit.Net.Interfaces.Clients;
@@ -23,34 +24,25 @@ public class WhiteBitSocketClient : BaseSocketClient, IWhiteBitSocketClient
#endregion

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

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

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

V4Api = AddApiClient(new WhiteBitSocketClientV4Api(_logger, options));
Initialize(options.Value);

V4Api = AddApiClient(new WhiteBitSocketClientV4Api(_logger, options.Value));
}
#endregion

@@ -60,9 +52,7 @@ public WhiteBitSocketClient(Action<WhiteBitSocketOptions>? optionsDelegate, ILog
/// <param name="optionsDelegate">Option configuration delegate</param>
public static void SetDefaultOptions(Action<WhiteBitSocketOptions> optionsDelegate)
{
var options = WhiteBitSocketOptions.Default.Copy();
optionsDelegate(options);
WhiteBitSocketOptions.Default = options;
WhiteBitSocketOptions.Default = ApplyOptionsDelegate(optionsDelegate);
}

/// <inheritdoc />
Loading
Loading