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

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
104 changes: 104 additions & 0 deletions BitMart.Net.UnitTests/BitMartRestClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
using System.Net.Http;
using BitMart.Net.Clients;
using BitMart.Net.Objects;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using CryptoExchange.Net.Objects;
using BitMart.Net.Interfaces.Clients;

namespace BitMart.Net.UnitTests
{
Expand Down Expand Up @@ -43,5 +47,105 @@ public void CheckInterfaces()
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces<BitMartRestClient>();
CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces<BitMartSocketClient>();
}

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

var collection = new ServiceCollection();
collection.AddBitMart(configuration.GetSection("BitMart"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddBitMart(configuration.GetSection("BitMart"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

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

var collection = new ServiceCollection();
collection.AddBitMart(configuration.GetSection("BitMart"));
var provider = collection.BuildServiceProvider();

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

var address = client.SpotApi.BaseAddress;

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

[Test]
public void TestConstructorConfiguration()
{
var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "ApiCredentials:Key", "123" },
{ "ApiCredentials:Secret", "456" },
{ "ApiCredentials:Passphrase", "000" },
{ "Socket:ApiCredentials:Key", "456" },
{ "Socket:ApiCredentials:Secret", "789" },
{ "Socket:ApiCredentials:Passphrase", "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.AddBitMart(configuration);
var provider = collection.BuildServiceProvider();

var restClient = provider.GetRequiredService<IBitMartRestClient>();
var socketClient = provider.GetRequiredService<IBitMartSocketClient>();

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 BitMart.Net.UnitTests/BitMartRestIntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using BitMart.Net.Clients;
using BitMart.Net.Objects;
using BitMart.Net.Objects.Options;
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 +29,11 @@ public override BitMartRestClient GetClient(ILoggerFactory loggerFactory)
var pass = Environment.GetEnvironmentVariable("APIPASS");

Authenticated = key != null && sec != null;
return new BitMartRestClient(null, loggerFactory, opts =>
return new BitMartRestClient(null, loggerFactory, Options.Create(new BitMartRestOptions
{
opts.OutputOriginalData = true;
opts.ApiCredentials = Authenticated ? new BitMartApiCredentials(key, sec, pass) : null;
});
OutputOriginalData = true,
ApiCredentials = Authenticated ? new BitMartApiCredentials(key, sec, pass) : null
}));
}

[Test]
Expand Down
4 changes: 2 additions & 2 deletions BitMart.Net/BitMart.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>10.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
101 changes: 83 additions & 18 deletions BitMart.Net/BitMart.Net.xml

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

2 changes: 1 addition & 1 deletion BitMart.Net/BitMartAuthenticationProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace BitMart.Net
internal class BitMartAuthenticationProvider : AuthenticationProvider<BitMartApiCredentials>
{
private static IMessageSerializer _serializer = new SystemTextJsonMessageSerializer();
public string GetMemo() => _credentials.Memo;
public string GetMemo() => _credentials.PassPhrase;

public BitMartAuthenticationProvider(BitMartApiCredentials credentials) : base(credentials)
{
Expand Down
20 changes: 20 additions & 0 deletions BitMart.Net/BitMartEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ internal BitMartEnvironment(
SocketClientPerpetualFuturesAddress = streamPerpetualFuturesAddress;
}

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

/// <summary>
/// Live environment, using Futures V2 API
/// </summary>
Expand Down
10 changes: 10 additions & 0 deletions BitMart.Net/BitMartExchange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public static class BitMartExchange
/// </summary>
public static string ExchangeName => "BitMart";

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

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

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