diff --git a/BitMart.Net.UnitTests/BitMartRestClientTests.cs b/BitMart.Net.UnitTests/BitMartRestClientTests.cs index 203e0fb..7593b8a 100644 --- a/BitMart.Net.UnitTests/BitMartRestClientTests.cs +++ b/BitMart.Net.UnitTests/BitMartRestClientTests.cs @@ -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 { @@ -43,5 +47,105 @@ public void CheckInterfaces() CryptoExchange.Net.Testing.TestHelpers.CheckForMissingRestInterfaces(); CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces(); } + + [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 + { + { "BitMart:Environment:Name", environmentName }, + }).Build(); + + var collection = new ServiceCollection(); + collection.AddBitMart(configuration.GetSection("BitMart")); + var provider = collection.BuildServiceProvider(); + + var client = provider.GetRequiredService(); + + var address = client.SpotApi.BaseAddress; + + Assert.That(address, Is.EqualTo(expected)); + } + + [Test] + public void TestConstructorNullEnvironment() + { + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + { "BitMart", null }, + }).Build(); + + var collection = new ServiceCollection(); + collection.AddBitMart(configuration.GetSection("BitMart")); + var provider = collection.BuildServiceProvider(); + + var client = provider.GetRequiredService(); + + 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 + { + { "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(); + + 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 + { + { "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(); + var socketClient = provider.GetRequiredService(); + + 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)); + } } } diff --git a/BitMart.Net.UnitTests/BitMartRestIntegrationTests.cs b/BitMart.Net.UnitTests/BitMartRestIntegrationTests.cs index 875c8b6..0655dc8 100644 --- a/BitMart.Net.UnitTests/BitMartRestIntegrationTests.cs +++ b/BitMart.Net.UnitTests/BitMartRestIntegrationTests.cs @@ -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; @@ -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] diff --git a/BitMart.Net/BitMart.Net.csproj b/BitMart.Net/BitMart.Net.csproj index 5c47c87..79c9266 100644 --- a/BitMart.Net/BitMart.Net.csproj +++ b/BitMart.Net/BitMart.Net.csproj @@ -1,4 +1,4 @@ - + netstandard2.0;netstandard2.1 10.0 @@ -48,7 +48,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/BitMart.Net/BitMart.Net.xml b/BitMart.Net/BitMart.Net.xml index fc9ad38..cd7f755 100644 --- a/BitMart.Net/BitMart.Net.xml +++ b/BitMart.Net/BitMart.Net.xml @@ -29,6 +29,16 @@ Socket Perpetual Futures API address + + + ctor for DI, use for creating a custom environment + + + + + Get the BitMart environment by name + + Live environment, using Futures V2 API @@ -60,6 +70,16 @@ Exchange name + + + Exchange name + + + + + Url to exchange image + + Url to the main website @@ -130,11 +150,11 @@ Option configuration delegate - + Create a new instance of the BitMartRestClient using provided options - Option configuration delegate + Option configuration The logger factory Http client for this client @@ -156,24 +176,18 @@ - - - Create a new instance of BitMartSocketClient - - The logger factory - Create a new instance of BitMartSocketClient Option configuration delegate - + Create a new instance of BitMartSocketClient The logger factory - Option configuration delegate + Option configuration @@ -2998,9 +3012,9 @@ - + - Memo + API key memo/passphrase @@ -3009,7 +3023,7 @@ The API key The API secret - API key memo + API key memo/passphrase @@ -5982,6 +5996,36 @@ Submission time + + + BitMart options + + + + + Rest client options + + + + + Socket client options + + + + + Trade environment. Contains info about URL's to use to connect to the API. Use `BitMartEnvironment` to swap environment, for example `Environment = BitMartEnvironment.Live` + + + + + The api credentials used for signing requests. + + + + + The DI service lifetime for the IBitMartSocketClient + + Options for the BitMart SymbolOrderBook @@ -6012,6 +6056,11 @@ Default options for new clients + + + ctor + + Set a broker id which will be send in the request headers @@ -6037,6 +6086,11 @@ Default options for new clients + + + ctor + + UsdFutures API options @@ -6221,15 +6275,26 @@ Extensions for DI - + + + Add services such as the IBitMartRestClient and IBitMartSocketClient. Configures the services based on the provided configuration. + + The service collection + The configuration(section) containing the options + + + - Add the IBitMartRestClient and IBitMartSocketClient to the sevice collection so they can be injected + Add services such as the IBitMartRestClient and IBitMartSocketClient. Services will be configured based on the provided options. The service collection - Set default options for the rest client - Set default options for the socket client - The lifetime of the IBitMartSocketClient for the service collection. Defaults to Singleton. + Set options for the BitMart services + + + DEPRECATED; use instead + + diff --git a/BitMart.Net/BitMartAuthenticationProvider.cs b/BitMart.Net/BitMartAuthenticationProvider.cs index a99b787..decb5ef 100644 --- a/BitMart.Net/BitMartAuthenticationProvider.cs +++ b/BitMart.Net/BitMartAuthenticationProvider.cs @@ -14,7 +14,7 @@ namespace BitMart.Net internal class BitMartAuthenticationProvider : AuthenticationProvider { private static IMessageSerializer _serializer = new SystemTextJsonMessageSerializer(); - public string GetMemo() => _credentials.Memo; + public string GetMemo() => _credentials.PassPhrase; public BitMartAuthenticationProvider(BitMartApiCredentials credentials) : base(credentials) { diff --git a/BitMart.Net/BitMartEnvironment.cs b/BitMart.Net/BitMartEnvironment.cs index 03370fd..e1e071e 100644 --- a/BitMart.Net/BitMartEnvironment.cs +++ b/BitMart.Net/BitMartEnvironment.cs @@ -41,6 +41,26 @@ internal BitMartEnvironment( SocketClientPerpetualFuturesAddress = streamPerpetualFuturesAddress; } + /// + /// ctor for DI, use for creating a custom environment + /// +#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. + { } + + /// + /// Get the BitMart environment by name + /// + public static BitMartEnvironment? GetEnvironmentByName(string? name) + => name switch + { + TradeEnvironmentNames.Live => Live, + "" => Live, + null => Live, + _ => default + }; + /// /// Live environment, using Futures V2 API /// diff --git a/BitMart.Net/BitMartExchange.cs b/BitMart.Net/BitMartExchange.cs index 9e4022d..2202702 100644 --- a/BitMart.Net/BitMartExchange.cs +++ b/BitMart.Net/BitMartExchange.cs @@ -20,6 +20,16 @@ public static class BitMartExchange /// public static string ExchangeName => "BitMart"; + /// + /// Exchange name + /// + public static string DisplayName => "BitMart"; + + /// + /// Url to exchange image + /// + public static string ImageUrl { get; } = "https://raw.githubusercontent.com/JKorf/BitMart.Net/master/BitMart.Net/Icon/icon.png"; + /// /// Url to the main website /// diff --git a/BitMart.Net/Clients/BitMartRestClient.cs b/BitMart.Net/Clients/BitMartRestClient.cs index f958a2a..7dc7c16 100644 --- a/BitMart.Net/Clients/BitMartRestClient.cs +++ b/BitMart.Net/Clients/BitMartRestClient.cs @@ -9,6 +9,7 @@ using BitMart.Net.Interfaces.Clients.SpotApi; using BitMart.Net.Clients.UsdFuturesApi; using BitMart.Net.Clients.SpotApi; +using Microsoft.Extensions.Options; namespace BitMart.Net.Clients { @@ -31,25 +32,23 @@ public class BitMartRestClient : BaseRestClient, IBitMartRestClient /// Create a new instance of the BitMartRestClient using provided options /// /// Option configuration delegate - public BitMartRestClient(Action? optionsDelegate = null) : this(null, null, optionsDelegate) + public BitMartRestClient(Action? optionsDelegate = null) + : this(null, null, Options.Create(ApplyOptionsDelegate(optionsDelegate))) { } /// /// Create a new instance of the BitMartRestClient using provided options /// - /// Option configuration delegate + /// Option configuration /// The logger factory /// Http client for this client - public BitMartRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, Action? optionsDelegate = null) : base(loggerFactory, "BitMart") + public BitMartRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, IOptions options) : base(loggerFactory, "BitMart") { - var options = BitMartRestOptions.Default.Copy(); - if (optionsDelegate != null) - optionsDelegate(options); - Initialize(options); + Initialize(options.Value); - UsdFuturesApi = AddApiClient(new BitMartRestClientUsdFuturesApi(_logger, this, httpClient, options)); - SpotApi = AddApiClient(new BitMartRestClientSpotApi(_logger, httpClient, options)); + UsdFuturesApi = AddApiClient(new BitMartRestClientUsdFuturesApi(_logger, this, httpClient, options.Value)); + SpotApi = AddApiClient(new BitMartRestClientSpotApi(_logger, httpClient, options.Value)); } #endregion @@ -60,9 +59,7 @@ public BitMartRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, /// Option configuration delegate public static void SetDefaultOptions(Action optionsDelegate) { - var options = BitMartRestOptions.Default.Copy(); - optionsDelegate(options); - BitMartRestOptions.Default = options; + BitMartRestOptions.Default = ApplyOptionsDelegate(optionsDelegate); } /// diff --git a/BitMart.Net/Clients/BitMartSocketClient.cs b/BitMart.Net/Clients/BitMartSocketClient.cs index e13d606..62ba229 100644 --- a/BitMart.Net/Clients/BitMartSocketClient.cs +++ b/BitMart.Net/Clients/BitMartSocketClient.cs @@ -8,6 +8,7 @@ using BitMart.Net.Interfaces.Clients.SpotApi; using BitMart.Net.Clients.SpotApi; using BitMart.Net.Clients.UsdFuturesApi; +using Microsoft.Extensions.Options; namespace BitMart.Net.Clients { @@ -28,19 +29,13 @@ public class BitMartSocketClient : BaseSocketClient, IBitMartSocketClient #endregion #region constructor/destructor - /// - /// Create a new instance of BitMartSocketClient - /// - /// The logger factory - public BitMartSocketClient(ILoggerFactory? loggerFactory = null) : this((x) => { }, loggerFactory) - { - } /// /// Create a new instance of BitMartSocketClient /// /// Option configuration delegate - public BitMartSocketClient(Action optionsDelegate) : this(optionsDelegate, null) + public BitMartSocketClient(Action? optionsDelegate = null) + : this(Options.Create(ApplyOptionsDelegate(optionsDelegate)), null) { } @@ -48,15 +43,13 @@ public BitMartSocketClient(Action optionsDelegate) : this( /// Create a new instance of BitMartSocketClient /// /// The logger factory - /// Option configuration delegate - public BitMartSocketClient(Action? optionsDelegate, ILoggerFactory? loggerFactory = null) : base(loggerFactory, "BitMart") + /// Option configuration + public BitMartSocketClient(IOptions options, ILoggerFactory? loggerFactory = null) : base(loggerFactory, "BitMart") { - var options = BitMartSocketOptions.Default.Copy(); - optionsDelegate?.Invoke(options); - Initialize(options); + Initialize(options.Value); - UsdFuturesApi = AddApiClient(new BitMartSocketClientUsdFuturesApi(_logger, options)); - SpotApi = AddApiClient(new BitMartSocketClientSpotApi(_logger, options)); + UsdFuturesApi = AddApiClient(new BitMartSocketClientUsdFuturesApi(_logger, options.Value)); + SpotApi = AddApiClient(new BitMartSocketClientSpotApi(_logger, options.Value)); } #endregion @@ -66,9 +59,7 @@ public BitMartSocketClient(Action? optionsDelegate, ILogge /// Option configuration delegate public static void SetDefaultOptions(Action optionsDelegate) { - var options = BitMartSocketOptions.Default.Copy(); - optionsDelegate(options); - BitMartSocketOptions.Default = options; + BitMartSocketOptions.Default = ApplyOptionsDelegate(optionsDelegate); } /// diff --git a/BitMart.Net/ExtensionMethods/ServiceCollectionExtensions.cs b/BitMart.Net/ExtensionMethods/ServiceCollectionExtensions.cs index 5c541ab..c0056b0 100644 --- a/BitMart.Net/ExtensionMethods/ServiceCollectionExtensions.cs +++ b/BitMart.Net/ExtensionMethods/ServiceCollectionExtensions.cs @@ -10,6 +10,9 @@ using BitMart.Net.SymbolOrderBooks; using CryptoExchange.Net; using BitMart.Net; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using Microsoft.Extensions.Configuration; namespace Microsoft.Extensions.DependencyInjection { @@ -18,47 +21,114 @@ namespace Microsoft.Extensions.DependencyInjection /// public static class ServiceCollectionExtensions { + /// - /// Add the IBitMartRestClient and IBitMartSocketClient to the sevice collection so they can be injected + /// Add services such as the IBitMartRestClient and IBitMartSocketClient. Configures the services based on the provided configuration. /// /// The service collection - /// Set default options for the rest client - /// Set default options for the socket client - /// The lifetime of the IBitMartSocketClient for the service collection. Defaults to Singleton. + /// The configuration(section) containing the options /// public static IServiceCollection AddBitMart( this IServiceCollection services, - Action? defaultRestOptionsDelegate = null, - Action? defaultSocketOptionsDelegate = null, - ServiceLifetime? socketClientLifeTime = null) + IConfiguration configuration) { - var restOptions = BitMartRestOptions.Default.Copy(); + var options = new BitMartOptions(); + // Reset environment so we know if theyre overriden + options.Rest.Environment = null!; + options.Socket.Environment = null!; + configuration.Bind(options); - if (defaultRestOptionsDelegate != null) - { - defaultRestOptionsDelegate(restOptions); - BitMartRestClient.SetDefaultOptions(defaultRestOptionsDelegate); - } + if (options.Rest == null || options.Socket == null) + throw new ArgumentException("Options null"); - if (defaultSocketOptionsDelegate != null) - BitMartSocketClient.SetDefaultOptions(defaultSocketOptionsDelegate); + var restEnvName = options.Rest.Environment?.Name ?? options.Environment?.Name ?? BitMartEnvironment.Live.Name; + var socketEnvName = options.Socket.Environment?.Name ?? options.Environment?.Name ?? BitMartEnvironment.Live.Name; + options.Rest.Environment = BitMartEnvironment.GetEnvironmentByName(restEnvName) ?? options.Rest.Environment!; + options.Rest.ApiCredentials = options.Rest.ApiCredentials ?? options.ApiCredentials; + options.Socket.Environment = BitMartEnvironment.GetEnvironmentByName(socketEnvName) ?? options.Socket.Environment!; + options.Socket.ApiCredentials = options.Socket.ApiCredentials ?? options.ApiCredentials; - services.AddHttpClient(options => - { - options.Timeout = restOptions.RequestTimeout; - }).ConfigurePrimaryHttpMessageHandler(() => + + services.AddSingleton(x => Options.Options.Create(options.Rest)); + services.AddSingleton(x => Options.Options.Create(options.Socket)); + + return AddBitMartCore(services, options.SocketClientLifeTime); + } + + /// + /// Add services such as the IBitMartRestClient and IBitMartSocketClient. Services will be configured based on the provided options. + /// + /// The service collection + /// Set options for the BitMart services + /// + public static IServiceCollection AddBitMart( + this IServiceCollection services, + Action? optionsDelegate = null) + { + var options = new BitMartOptions(); + // Reset environment so we know if theyre overriden + options.Rest.Environment = null!; + options.Socket.Environment = null!; + optionsDelegate?.Invoke(options); + if (options.Rest == null || options.Socket == null) + throw new ArgumentException("Options null"); + + options.Rest.Environment = options.Rest.Environment ?? options.Environment ?? BitMartEnvironment.Live; + options.Rest.ApiCredentials = options.Rest.ApiCredentials ?? options.ApiCredentials; + options.Socket.Environment = options.Socket.Environment ?? options.Environment ?? BitMartEnvironment.Live; + options.Socket.ApiCredentials = options.Socket.ApiCredentials ?? options.ApiCredentials; + + services.AddSingleton(x => Options.Options.Create(options.Rest)); + services.AddSingleton(x => Options.Options.Create(options.Socket)); + + return AddBitMartCore(services, options.SocketClientLifeTime); + } + + /// + /// DEPRECATED; use instead + /// + public static IServiceCollection AddBitMart( + this IServiceCollection services, + Action restDelegate, + Action? socketDelegate = null, + ServiceLifetime? socketClientLifeTime = null) + { + services.Configure((x) => { restDelegate?.Invoke(x); }); + services.Configure((x) => { socketDelegate?.Invoke(x); }); + + return AddBitMartCore(services, socketClientLifeTime); + } + + private static IServiceCollection AddBitMartCore( + this IServiceCollection services, + ServiceLifetime? socketClientLifeTime = null) + { + services.AddHttpClient((client, serviceProvider) => { + var options = serviceProvider.GetRequiredService>().Value; + client.Timeout = options.RequestTimeout; + return new BitMartRestClient(client, serviceProvider.GetRequiredService(), serviceProvider.GetRequiredService>()); + }).ConfigurePrimaryHttpMessageHandler((serviceProvider) => { var handler = new HttpClientHandler(); - if (restOptions.Proxy != null) + try + { + handler.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; + } + catch (PlatformNotSupportedException) + { } + + var options = serviceProvider.GetRequiredService>().Value; + if (options.Proxy != null) { handler.Proxy = new WebProxy { - Address = new Uri($"{restOptions.Proxy.Host}:{restOptions.Proxy.Port}"), - Credentials = restOptions.Proxy.Password == null ? null : new NetworkCredential(restOptions.Proxy.Login, restOptions.Proxy.Password) + Address = new Uri($"{options.Proxy.Host}:{options.Proxy.Port}"), + Credentials = options.Proxy.Password == null ? null : new NetworkCredential(options.Proxy.Login, options.Proxy.Password) }; } return handler; }); + services.Add(new ServiceDescriptor(typeof(IBitMartSocketClient), x => { return new BitMartSocketClient(x.GetRequiredService>(), x.GetRequiredService()); }, socketClientLifeTime ?? ServiceLifetime.Singleton)); services.AddTransient(); services.AddSingleton(); @@ -70,11 +140,7 @@ public static IServiceCollection AddBitMart( services.RegisterSharedSocketInterfaces(x => x.GetRequiredService().SpotApi.SharedClient); services.RegisterSharedRestInterfaces(x => x.GetRequiredService().UsdFuturesApi.SharedClient); services.RegisterSharedSocketInterfaces(x => x.GetRequiredService().UsdFuturesApi.SharedClient); - - if (socketClientLifeTime == null) - services.AddSingleton(); - else - services.Add(new ServiceDescriptor(typeof(IBitMartSocketClient), typeof(BitMartSocketClient), socketClientLifeTime.Value)); + return services; } } diff --git a/BitMart.Net/Objects/BitMartApiCredentials.cs b/BitMart.Net/Objects/BitMartApiCredentials.cs index 945c33f..6b5bd29 100644 --- a/BitMart.Net/Objects/BitMartApiCredentials.cs +++ b/BitMart.Net/Objects/BitMartApiCredentials.cs @@ -8,19 +8,19 @@ namespace BitMart.Net.Objects public class BitMartApiCredentials : ApiCredentials { /// - /// Memo + /// API key memo/passphrase /// - public string Memo { get; set; } + public string PassPhrase { get; set; } /// /// ctor /// /// The API key /// The API secret - /// API key memo - public BitMartApiCredentials(string key, string secret, string memo) : base(key, secret) + /// API key memo/passphrase + public BitMartApiCredentials(string key, string secret, string passPhrase) : base(key, secret) { - Memo = memo; + PassPhrase = passPhrase; } /// @@ -29,7 +29,7 @@ public BitMartApiCredentials(string key, string secret, string memo) : base(key, /// public override ApiCredentials Copy() { - return new BitMartApiCredentials(Key, Secret, Memo); + return new BitMartApiCredentials(Key, Secret, PassPhrase); } } } diff --git a/BitMart.Net/Objects/Options/BitMartOptions.cs b/BitMart.Net/Objects/Options/BitMartOptions.cs new file mode 100644 index 0000000..5f3da1c --- /dev/null +++ b/BitMart.Net/Objects/Options/BitMartOptions.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BitMart.Net.Objects.Options +{ + /// + /// BitMart options + /// + public class BitMartOptions + { + /// + /// Rest client options + /// + public BitMartRestOptions Rest { get; set; } = new BitMartRestOptions(); + + /// + /// Socket client options + /// + public BitMartSocketOptions Socket { get; set; } = new BitMartSocketOptions(); + + /// + /// Trade environment. Contains info about URL's to use to connect to the API. Use `BitMartEnvironment` to swap environment, for example `Environment = BitMartEnvironment.Live` + /// + public BitMartEnvironment? Environment { get; set; } + + /// + /// The api credentials used for signing requests. + /// + public BitMartApiCredentials? ApiCredentials { get; set; } + + /// + /// The DI service lifetime for the IBitMartSocketClient + /// + public ServiceLifetime? SocketClientLifeTime { get; set; } + } +} diff --git a/BitMart.Net/Objects/Options/BitMartRestOptions.cs b/BitMart.Net/Objects/Options/BitMartRestOptions.cs index 05bcd73..952cac4 100644 --- a/BitMart.Net/Objects/Options/BitMartRestOptions.cs +++ b/BitMart.Net/Objects/Options/BitMartRestOptions.cs @@ -10,12 +10,20 @@ public class BitMartRestOptions : RestExchangeOptions /// Default options for new clients /// - public static BitMartRestOptions Default { get; set; } = new BitMartRestOptions() + internal static BitMartRestOptions Default { get; set; } = new BitMartRestOptions() { Environment = BitMartEnvironment.Live, AutoTimestamp = true }; - + + /// + /// ctor + /// + public BitMartRestOptions() + { + Default?.Set(this); + } + /// /// Set a broker id which will be send in the request headers /// @@ -24,21 +32,20 @@ public class BitMartRestOptions : RestExchangeOptions /// UsdFutures API options /// - public RestApiOptions UsdFuturesOptions { get; private set; } = new RestApiOptions(); + public RestApiOptions UsdFuturesOptions { get; private set; } = new RestApiOptions(); /// /// Spot API options /// - public RestApiOptions SpotOptions { get; private set; } = new RestApiOptions(); - + public RestApiOptions SpotOptions { get; private set; } = new RestApiOptions(); - internal BitMartRestOptions Copy() + internal BitMartRestOptions Set(BitMartRestOptions targetOptions) { - var options = Copy(); - options.BrokerId = BrokerId; - options.UsdFuturesOptions = UsdFuturesOptions.Copy(); - options.SpotOptions = SpotOptions.Copy(); - return options; + targetOptions = base.Set(targetOptions); + targetOptions.BrokerId = BrokerId; + targetOptions.UsdFuturesOptions = UsdFuturesOptions.Set(targetOptions.UsdFuturesOptions); + targetOptions.SpotOptions = SpotOptions.Set(targetOptions.SpotOptions); + return targetOptions; } } } diff --git a/BitMart.Net/Objects/Options/BitMartSocketOptions.cs b/BitMart.Net/Objects/Options/BitMartSocketOptions.cs index 3ad7232..bc9dc0f 100644 --- a/BitMart.Net/Objects/Options/BitMartSocketOptions.cs +++ b/BitMart.Net/Objects/Options/BitMartSocketOptions.cs @@ -10,29 +10,37 @@ public class BitMartSocketOptions : SocketExchangeOptions /// Default options for new clients /// - public static BitMartSocketOptions Default { get; set; } = new BitMartSocketOptions() + internal static BitMartSocketOptions Default { get; set; } = new BitMartSocketOptions() { Environment = BitMartEnvironment.Live, SocketSubscriptionsCombineTarget = 10, MaxSocketConnections = 20 }; - /// + /// + /// ctor + /// + public BitMartSocketOptions() + { + Default?.Set(this); + } + + /// /// UsdFutures API options /// - public SocketApiOptions UsdFuturesOptions { get; private set; } = new SocketApiOptions(); + public SocketApiOptions UsdFuturesOptions { get; private set; } = new SocketApiOptions(); /// /// Spot API options /// - public SocketApiOptions SpotOptions { get; private set; } = new SocketApiOptions(); + public SocketApiOptions SpotOptions { get; private set; } = new SocketApiOptions(); - internal BitMartSocketOptions Copy() + internal BitMartSocketOptions Set(BitMartSocketOptions targetOptions) { - var options = Copy(); - options.UsdFuturesOptions = UsdFuturesOptions.Copy(); - options.SpotOptions = SpotOptions.Copy(); - return options; + targetOptions = base.Set(targetOptions); + targetOptions.UsdFuturesOptions = UsdFuturesOptions.Set(targetOptions.UsdFuturesOptions); + targetOptions.SpotOptions = UsdFuturesOptions.Set(targetOptions.UsdFuturesOptions); + return targetOptions; } } } diff --git a/docs/index.html b/docs/index.html index 67f1060..c52aa89 100644 --- a/docs/index.html +++ b/docs/index.html @@ -191,8 +191,14 @@

API Access

BitMart.Net can be configured using Dotnet dependency injection, after which the clients can be injected into your services. It also correctly configures logging and HttpClient usage.

-
builder.Services.AddBitMart(options => {
-  // Options can be configured here, for example:
+		  
// Configure options from config file
+// see https://github.com/JKorf/CryptoExchange.Net/tree/master/Examples/example-config.json for an example
+builder.Services.AddBitMart(builder.Configuration.GetSection("BitMart"));
+		  
+// OR
+		  
+ builder.Services.AddBitMart(options => {
+  // Configure options in code
   options.ApiCredentials = new BitMartApiCredentials("APIKEY", "APISECRET", "MEMO");
 });