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 #124

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
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
102 changes: 102 additions & 0 deletions Kraken.Net.UnitTests/KrakenClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
using System.Net.Http;
using CryptoExchange.Net.Testing.Implementations;
using System.Text.Json;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using CryptoExchange.Net.Objects;
using Kraken.Net.Interfaces.Clients;

namespace Kraken.Net.UnitTests
{
Expand Down Expand Up @@ -170,5 +174,103 @@ public void CheckInterfaces()
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<KrakenRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<KrakenSocketClient>();
}

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

var collection = new ServiceCollection();
collection.AddKraken(configuration.GetSection("Kraken"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddKraken(configuration.GetSection("Kraken"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddKraken(configuration.GetSection("Kraken"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var restClient = provider.GetRequiredService<IKrakenRestClient>();
var socketClient = provider.GetRequiredService<IKrakenSocketClient>();

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));
}
}
}
10 changes: 6 additions & 4 deletions Kraken.Net.UnitTests/KrakenRestIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using NUnit.Framework;
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Kraken.Net.Objects.Options;

namespace Kraken.Net.UnitTests
{
Expand All @@ -23,11 +25,11 @@ public override KrakenRestClient GetClient(ILoggerFactory loggerFactory)
var sec = Environment.GetEnvironmentVariable("APISECRET");

Authenticated = key != null && sec != null;
return new KrakenRestClient(null, loggerFactory, opts =>
return new KrakenRestClient(null, loggerFactory, Options.Create(new KrakenRestOptions
{
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 Kraken.Net/Clients/KrakenRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Kraken.Net.Interfaces.Clients.FuturesApi;
using Kraken.Net.Interfaces.Clients.SpotApi;
using Kraken.Net.Objects.Options;
using Microsoft.Extensions.Options;

namespace Kraken.Net.Clients
{
Expand All @@ -26,26 +27,24 @@ public class KrakenRestClient : BaseRestClient, IKrakenRestClient
/// Create a new instance of the KrakenRestClient using provided options
/// </summary>
/// <param name="optionsDelegate">Option configuration delegate</param>
public KrakenRestClient(Action<KrakenRestOptions>? optionsDelegate = null) : this(null, null, optionsDelegate)
public KrakenRestClient(Action<KrakenRestOptions>? optionsDelegate = null)
: this(null, null, Options.Create(ApplyOptionsDelegate(optionsDelegate)))
{
}

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

SpotApi = AddApiClient(new KrakenRestClientSpotApi(_logger, httpClient, options));
FuturesApi = AddApiClient(new KrakenRestClientFuturesApi(_logger, httpClient, options));
SpotApi = AddApiClient(new KrakenRestClientSpotApi(_logger, httpClient, options.Value));
FuturesApi = AddApiClient(new KrakenRestClientFuturesApi(_logger, httpClient, options.Value));
}
#endregion

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

/// <inheritdoc />
Expand Down
28 changes: 9 additions & 19 deletions Kraken.Net/Clients/KrakenSocketClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Kraken.Net.Interfaces.Clients.FuturesApi;
using Kraken.Net.Interfaces.Clients.SpotApi;
using Kraken.Net.Objects.Options;
using Microsoft.Extensions.Options;

namespace Kraken.Net.Clients
{
Expand All @@ -21,35 +22,26 @@ public class KrakenSocketClient : BaseSocketClient, IKrakenSocketClient
#endregion

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

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

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

SpotApi = AddApiClient(new KrakenSocketClientSpotApi(_logger, options));
FuturesApi = AddApiClient(new KrakenSocketClientFuturesApi(_logger, options));
SpotApi = AddApiClient(new KrakenSocketClientSpotApi(_logger, options.Value));
FuturesApi = AddApiClient(new KrakenSocketClientFuturesApi(_logger, options.Value));
}
#endregion

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

/// <inheritdoc />
Expand Down
Loading
Loading