Skip to content

Commit

Permalink
Client Configuration (#169)
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 BitfinexExchange class
Updated client constructors to accept IOptions from DI
Removed redundant BitfinexSocketClient constructor
  • Loading branch information
JKorf authored Nov 19, 2024
1 parent ca2c00a commit 737d2cb
Show file tree
Hide file tree
Showing 13 changed files with 403 additions and 93 deletions.
101 changes: 101 additions & 0 deletions Bitfinex.Net.UnitTests/BitfinexClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
using System.Collections.Generic;
using CryptoExchange.Net.Converters.JsonNet;
using CryptoExchange.Net.Testing.Implementations;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Bitfinex.Net.Interfaces.Clients;

namespace Bitfinex.Net.UnitTests
{
Expand Down Expand Up @@ -106,5 +109,103 @@ public void CheckInterfaces()
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<BitfinexRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<BitfinexSocketClient>();
}

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

var collection = new ServiceCollection();
collection.AddBitfinex(configuration.GetSection("Bitfinex"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddBitfinex(configuration.GetSection("Bitfinex"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddBitfinex(configuration.GetSection("Bitfinex"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

var restClient = provider.GetRequiredService<IBitfinexRestClient>();
var socketClient = provider.GetRequiredService<IBitfinexSocketClient>();

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 Bitfinex.Net.UnitTests/BitfinexRestIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Bitfinex.Net.Clients;
using CryptoExchange.Net.Testing;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NUnit.Framework;
using System;
using System.Diagnostics;
Expand All @@ -24,11 +25,11 @@ public override BitfinexRestClient GetClient(ILoggerFactory loggerFactory)
var sec = Environment.GetEnvironmentVariable("APISECRET");

Authenticated = key != null && sec != null;
return new BitfinexRestClient(null, loggerFactory, opts =>
return new BitfinexRestClient(null, loggerFactory, Options.Create(new Objects.Options.BitfinexRestOptions
{
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
2 changes: 1 addition & 1 deletion Bitfinex.Net/Bitfinex.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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 Bitfinex.Net/Bitfinex.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 Bitfinex.Net/BitfinexEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ internal BitfinexEnvironment(string name, string restAddress, string socketAddre
SocketAddress = socketAddress;
}

/// <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 BitfinexEnvironment() : 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 Bitfinex environment by name
/// </summary>
public static BitfinexEnvironment? GetEnvironmentByName(string? name)
=> name switch
{
TradeEnvironmentNames.Live => Live,
"" => Live,
null => Live,
_ => default
};

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

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

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

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

0 comments on commit 737d2cb

Please sign in to comment.