diff --git a/WhiteBit.Net.UnitTests/WhiteBitRestClientTests.cs b/WhiteBit.Net.UnitTests/WhiteBitRestClientTests.cs index 3e50c3d..33e209a 100644 --- a/WhiteBit.Net.UnitTests/WhiteBitRestClientTests.cs +++ b/WhiteBit.Net.UnitTests/WhiteBitRestClientTests.cs @@ -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(); CryptoExchange.Net.Testing.TestHelpers.CheckForMissingSocketInterfaces(); } + + [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 + { + { "WhiteBit:Environment:Name", environmentName }, + }).Build(); + + var collection = new ServiceCollection(); + collection.AddWhiteBit(configuration.GetSection("WhiteBit")); + var provider = collection.BuildServiceProvider(); + + var client = provider.GetRequiredService(); + + var address = client.V4Api.BaseAddress; + + Assert.That(address, Is.EqualTo(expected)); + } + + [Test] + public void TestConstructorNullEnvironment() + { + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + { "WhiteBit", null }, + }).Build(); + + var collection = new ServiceCollection(); + collection.AddWhiteBit(configuration.GetSection("WhiteBit")); + var provider = collection.BuildServiceProvider(); + + var client = provider.GetRequiredService(); + + var address = client.V4Api.BaseAddress; + + Assert.That(address, Is.EqualTo("https://whitebit.com")); + } + + [Test] + public void TestConstructorApiOverwriteEnvironment() + { + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + { "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(); + + var address = client.V4Api.BaseAddress; + + Assert.That(address, Is.EqualTo("https://whitebit.com")); + } + + [Test] + public void TestConstructorConfiguration() + { + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(new Dictionary + { + { "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(); + var socketClient = provider.GetRequiredService(); + + 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)); + } } } diff --git a/WhiteBit.Net.UnitTests/WhiteBitRestIntegrationTests.cs b/WhiteBit.Net.UnitTests/WhiteBitRestIntegrationTests.cs index 243420a..bd3c72b 100644 --- a/WhiteBit.Net.UnitTests/WhiteBitRestIntegrationTests.cs +++ b/WhiteBit.Net.UnitTests/WhiteBitRestIntegrationTests.cs @@ -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] diff --git a/WhiteBit.Net/Clients/WhiteBitRestClient.cs b/WhiteBit.Net/Clients/WhiteBitRestClient.cs index 7721dff..c2726b5 100644 --- a/WhiteBit.Net/Clients/WhiteBitRestClient.cs +++ b/WhiteBit.Net/Clients/WhiteBitRestClient.cs @@ -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 /// /// Option configuration delegate - public WhiteBitRestClient(Action? optionsDelegate = null) : this(null, null, optionsDelegate) + public WhiteBitRestClient(Action? optionsDelegate = null) + : this(null, null, Options.Create(ApplyOptionsDelegate(optionsDelegate))) { } /// /// Create a new instance of the WhiteBitRestClient using provided options /// - /// Option configuration delegate + /// Option configuration /// The logger factory /// Http client for this client - public WhiteBitRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, Action? optionsDelegate = null) : base(loggerFactory, "WhiteBit") + public WhiteBitRestClient(HttpClient? httpClient, ILoggerFactory? loggerFactory, IOptions 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, /// Option configuration delegate public static void SetDefaultOptions(Action optionsDelegate) { - var options = WhiteBitRestOptions.Default.Copy(); - optionsDelegate(options); - WhiteBitRestOptions.Default = options; + WhiteBitRestOptions.Default = ApplyOptionsDelegate(optionsDelegate); } /// diff --git a/WhiteBit.Net/Clients/WhiteBitSocketClient.cs b/WhiteBit.Net/Clients/WhiteBitSocketClient.cs index 2420b14..8277698 100644 --- a/WhiteBit.Net/Clients/WhiteBitSocketClient.cs +++ b/WhiteBit.Net/Clients/WhiteBitSocketClient.cs @@ -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,19 +24,12 @@ public class WhiteBitSocketClient : BaseSocketClient, IWhiteBitSocketClient #endregion #region constructor/destructor - /// - /// Create a new instance of WhiteBitSocketClient - /// - /// The logger factory - public WhiteBitSocketClient(ILoggerFactory? loggerFactory = null) : this((x) => { }, loggerFactory) - { - } - /// /// Create a new instance of WhiteBitSocketClient /// /// Option configuration delegate - public WhiteBitSocketClient(Action optionsDelegate) : this(optionsDelegate, null) + public WhiteBitSocketClient(Action? optionsDelegate = null) + : this(Options.Create(ApplyOptionsDelegate(optionsDelegate)), null) { } @@ -43,14 +37,12 @@ public WhiteBitSocketClient(Action optionsDelegate) : thi /// Create a new instance of WhiteBitSocketClient /// /// The logger factory - /// Option configuration delegate - public WhiteBitSocketClient(Action? optionsDelegate, ILoggerFactory? loggerFactory = null) : base(loggerFactory, "WhiteBit") + /// Option configuration + public WhiteBitSocketClient(IOptions 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? optionsDelegate, ILog /// Option configuration delegate public static void SetDefaultOptions(Action optionsDelegate) { - var options = WhiteBitSocketOptions.Default.Copy(); - optionsDelegate(options); - WhiteBitSocketOptions.Default = options; + WhiteBitSocketOptions.Default = ApplyOptionsDelegate(optionsDelegate); } /// diff --git a/WhiteBit.Net/ExtensionMethods/ServiceCollectionExtensions.cs b/WhiteBit.Net/ExtensionMethods/ServiceCollectionExtensions.cs index ec5ac30..b6d7557 100644 --- a/WhiteBit.Net/ExtensionMethods/ServiceCollectionExtensions.cs +++ b/WhiteBit.Net/ExtensionMethods/ServiceCollectionExtensions.cs @@ -1,6 +1,9 @@ using CryptoExchange.Net; using CryptoExchange.Net.Clients; using CryptoExchange.Net.Interfaces; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using System; using System.Net; using System.Net.Http; @@ -18,47 +21,115 @@ namespace Microsoft.Extensions.DependencyInjection /// public static class ServiceCollectionExtensions { + + /// + /// Add services such as the IWhiteBitRestClient and IWhiteBitSocketClient. Configures the services based on the provided configuration. + /// + /// The service collection + /// The configuration(section) containing the options + /// + public static IServiceCollection AddWhiteBit( + this IServiceCollection services, + IConfiguration configuration) + { + var options = new WhiteBitOptions(); + // Reset environment so we know if theyre overriden + options.Rest.Environment = null!; + options.Socket.Environment = null!; + configuration.Bind(options); + + if (options.Rest == null || options.Socket == null) + throw new ArgumentException("Options null"); + + var restEnvName = options.Rest.Environment?.Name ?? options.Environment?.Name ?? WhiteBitEnvironment.Live.Name; + var socketEnvName = options.Socket.Environment?.Name ?? options.Environment?.Name ?? WhiteBitEnvironment.Live.Name; + options.Rest.Environment = WhiteBitEnvironment.GetEnvironmentByName(restEnvName) ?? options.Rest.Environment!; + options.Rest.ApiCredentials = options.Rest.ApiCredentials ?? options.ApiCredentials; + options.Socket.Environment = WhiteBitEnvironment.GetEnvironmentByName(socketEnvName) ?? options.Socket.Environment!; + 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 AddWhiteBitCore(services, options.SocketClientLifeTime); + } + /// - /// Add the IWhiteBitRestClient and IWhiteBitSocketClient to the sevice collection so they can be injected + /// Add services such as the IWhiteBitRestClient and IWhiteBitSocketClient. 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 IWhiteBitSocketClient for the service collection. Defaults to Singleton. + /// Set options for the WhiteBit services /// public static IServiceCollection AddWhiteBit( this IServiceCollection services, - Action? defaultRestOptionsDelegate = null, - Action? defaultSocketOptionsDelegate = null, + Action? optionsDelegate = null) + { + var options = new WhiteBitOptions(); + // 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 ?? WhiteBitEnvironment.Live; + options.Rest.ApiCredentials = options.Rest.ApiCredentials ?? options.ApiCredentials; + options.Socket.Environment = options.Socket.Environment ?? options.Environment ?? WhiteBitEnvironment.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 AddWhiteBitCore(services, options.SocketClientLifeTime); + } + + /// + /// DEPRECATED; use instead + /// + public static IServiceCollection AddWhiteBit( + this IServiceCollection services, + Action restDelegate, + Action? socketDelegate = null, ServiceLifetime? socketClientLifeTime = null) { - var restOptions = WhiteBitRestOptions.Default.Copy(); + services.Configure((x) => { restDelegate?.Invoke(x); }); + services.Configure((x) => { socketDelegate?.Invoke(x); }); - if (defaultRestOptionsDelegate != null) - { - defaultRestOptionsDelegate(restOptions); - WhiteBitRestClient.SetDefaultOptions(defaultRestOptionsDelegate); - } + return AddWhiteBitCore(services, socketClientLifeTime); + } - if (defaultSocketOptionsDelegate != null) - WhiteBitSocketClient.SetDefaultOptions(defaultSocketOptionsDelegate); + private static IServiceCollection AddWhiteBitCore( + this IServiceCollection services, + ServiceLifetime? socketClientLifeTime = null) + { - services.AddHttpClient(options => - { - options.Timeout = restOptions.RequestTimeout; - }).ConfigurePrimaryHttpMessageHandler(() => + services.AddHttpClient((client, serviceProvider) => { + var options = serviceProvider.GetRequiredService>().Value; + client.Timeout = options.RequestTimeout; + return new WhiteBitRestClient(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(IWhiteBitSocketClient), x => { return new WhiteBitSocketClient(x.GetRequiredService>(), x.GetRequiredService()); }, socketClientLifeTime ?? ServiceLifetime.Singleton)); services.AddTransient(); services.AddSingleton(); @@ -67,11 +138,6 @@ public static IServiceCollection AddWhiteBit( services.RegisterSharedRestInterfaces(x => x.GetRequiredService().V4Api.SharedClient); services.RegisterSharedSocketInterfaces(x => x.GetRequiredService().V4Api.SharedClient); - - if (socketClientLifeTime == null) - services.AddSingleton(); - else - services.Add(new ServiceDescriptor(typeof(IWhiteBitSocketClient), typeof(WhiteBitSocketClient), socketClientLifeTime.Value)); return services; } } diff --git a/WhiteBit.Net/Objects/Options/WhiteBitOptions.cs b/WhiteBit.Net/Objects/Options/WhiteBitOptions.cs new file mode 100644 index 0000000..7d5f67a --- /dev/null +++ b/WhiteBit.Net/Objects/Options/WhiteBitOptions.cs @@ -0,0 +1,39 @@ +using CryptoExchange.Net.Authentication; +using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using System.Text; + +namespace WhiteBit.Net.Objects.Options +{ + /// + /// WhiteBit options + /// + public class WhiteBitOptions + { + /// + /// Rest client options + /// + public WhiteBitRestOptions Rest { get; set; } = new WhiteBitRestOptions(); + + /// + /// Socket client options + /// + public WhiteBitSocketOptions Socket { get; set; } = new WhiteBitSocketOptions(); + + /// + /// Trade environment. Contains info about URL's to use to connect to the API. Use `WhiteBitEnvironment` to swap environment, for example `Environment = WhiteBitEnvironment.Live` + /// + public WhiteBitEnvironment? Environment { get; set; } + + /// + /// The api credentials used for signing requests. + /// + public ApiCredentials? ApiCredentials { get; set; } + + /// + /// The DI service lifetime for the IWhiteBitSocketClient + /// + public ServiceLifetime? SocketClientLifeTime { get; set; } + } +} diff --git a/WhiteBit.Net/Objects/Options/WhiteBitRestOptions.cs b/WhiteBit.Net/Objects/Options/WhiteBitRestOptions.cs index 85b5fce..0a749e7 100644 --- a/WhiteBit.Net/Objects/Options/WhiteBitRestOptions.cs +++ b/WhiteBit.Net/Objects/Options/WhiteBitRestOptions.cs @@ -10,14 +10,21 @@ public class WhiteBitRestOptions : RestExchangeOptions /// /// Default options for new clients /// - public static WhiteBitRestOptions Default { get; set; } = new WhiteBitRestOptions() + internal static WhiteBitRestOptions Default { get; set; } = new WhiteBitRestOptions() { Environment = WhiteBitEnvironment.Live, AutoTimestamp = true }; - - /// + /// + /// ctor + /// + public WhiteBitRestOptions() + { + Default?.Set(this); + } + + /// /// V4 API options /// public RestApiOptions V4Options { get; private set; } = new RestApiOptions(); @@ -27,13 +34,12 @@ public class WhiteBitRestOptions : RestExchangeOptions /// public bool EnableNonceWindow { get; set; } = false; - internal WhiteBitRestOptions Copy() + internal WhiteBitRestOptions Set(WhiteBitRestOptions targetOptions) { - var options = Copy(); - options.EnableNonceWindow = EnableNonceWindow; - options.V4Options = V4Options.Copy(); - - return options; + targetOptions = base.Set(targetOptions); + targetOptions.EnableNonceWindow = EnableNonceWindow; + targetOptions.V4Options = V4Options.Set(targetOptions.V4Options); + return targetOptions; } } } diff --git a/WhiteBit.Net/Objects/Options/WhiteBitSocketOptions.cs b/WhiteBit.Net/Objects/Options/WhiteBitSocketOptions.cs index 498ad9e..b2ae80d 100644 --- a/WhiteBit.Net/Objects/Options/WhiteBitSocketOptions.cs +++ b/WhiteBit.Net/Objects/Options/WhiteBitSocketOptions.cs @@ -10,26 +10,31 @@ public class WhiteBitSocketOptions : SocketExchangeOptions /// /// Default options for new clients /// - public static WhiteBitSocketOptions Default { get; set; } = new WhiteBitSocketOptions() + internal static WhiteBitSocketOptions Default { get; set; } = new WhiteBitSocketOptions() { Environment = WhiteBitEnvironment.Live, SocketSubscriptionsCombineTarget = 10 }; - - /// + /// + /// ctor + /// + public WhiteBitSocketOptions() + { + Default?.Set(this); + } + + /// /// V4 API options /// public SocketApiOptions V4Options { get; private set; } = new SocketApiOptions(); - internal WhiteBitSocketOptions Copy() + internal WhiteBitSocketOptions Set(WhiteBitSocketOptions targetOptions) { - var options = Copy(); - - options.V4Options = V4Options.Copy(); - - return options; + targetOptions = base.Set(targetOptions); + targetOptions.V4Options = V4Options.Set(targetOptions.V4Options); + return targetOptions; } } } diff --git a/WhiteBit.Net/WhiteBit.Net.csproj b/WhiteBit.Net/WhiteBit.Net.csproj index da33a03..486dce6 100644 --- a/WhiteBit.Net/WhiteBit.Net.csproj +++ b/WhiteBit.Net/WhiteBit.Net.csproj @@ -1,4 +1,4 @@ - + netstandard2.0;netstandard2.1 12.0 @@ -48,7 +48,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/WhiteBit.Net/WhiteBit.Net.xml b/WhiteBit.Net/WhiteBit.Net.xml index fcb4319..1c02508 100644 --- a/WhiteBit.Net/WhiteBit.Net.xml +++ b/WhiteBit.Net/WhiteBit.Net.xml @@ -377,11 +377,11 @@ Option configuration delegate - + Create a new instance of the WhiteBitRestClient using provided options - Option configuration delegate + Option configuration The logger factory Http client for this client @@ -400,24 +400,18 @@ - - - Create a new instance of WhiteBitSocketClient - - The logger factory - Create a new instance of WhiteBitSocketClient Option configuration delegate - + Create a new instance of WhiteBitSocketClient The logger factory - Option configuration delegate + Option configuration @@ -4543,6 +4537,36 @@ Side + + + WhiteBit options + + + + + Rest client options + + + + + Socket client options + + + + + Trade environment. Contains info about URL's to use to connect to the API. Use `WhiteBitEnvironment` to swap environment, for example `Environment = WhiteBitEnvironment.Live` + + + + + The api credentials used for signing requests. + + + + + The DI service lifetime for the IWhiteBitSocketClient + + Options for the WhiteBit SymbolOrderBook @@ -4573,6 +4597,11 @@ Default options for new clients + + + ctor + + V4 API options @@ -4593,6 +4622,11 @@ Default options for new clients + + + ctor + + V4 API options @@ -4849,6 +4883,16 @@ Socket API address + + + ctor for DI, use for creating a custom environment + + + + + Get the WhiteBit environment by name + + Live environment @@ -4873,6 +4917,16 @@ Exchange name + + + Exchange name + + + + + Url to exchange image + + Url to the main website @@ -4949,15 +5003,26 @@ Extensions for DI - + + + Add services such as the IWhiteBitRestClient and IWhiteBitSocketClient. Configures the services based on the provided configuration. + + The service collection + The configuration(section) containing the options + + + - Add the IWhiteBitRestClient and IWhiteBitSocketClient to the sevice collection so they can be injected + Add services such as the IWhiteBitRestClient and IWhiteBitSocketClient. 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 IWhiteBitSocketClient for the service collection. Defaults to Singleton. + Set options for the WhiteBit services + + + DEPRECATED; use instead + + diff --git a/WhiteBit.Net/WhiteBitEnvironment.cs b/WhiteBit.Net/WhiteBitEnvironment.cs index 2f49ac8..84f53bd 100644 --- a/WhiteBit.Net/WhiteBitEnvironment.cs +++ b/WhiteBit.Net/WhiteBitEnvironment.cs @@ -28,6 +28,26 @@ internal WhiteBitEnvironment( SocketClientAddress = streamAddress; } + /// + /// 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 WhiteBitEnvironment() : 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 WhiteBit environment by name + /// + public static WhiteBitEnvironment? GetEnvironmentByName(string? name) + => name switch + { + TradeEnvironmentNames.Live => Live, + "" => Live, + null => Live, + _ => default + }; + /// /// Live environment /// diff --git a/WhiteBit.Net/WhiteBitExchange.cs b/WhiteBit.Net/WhiteBitExchange.cs index 3ca1c78..84e8d92 100644 --- a/WhiteBit.Net/WhiteBitExchange.cs +++ b/WhiteBit.Net/WhiteBitExchange.cs @@ -20,6 +20,16 @@ public static class WhiteBitExchange /// public static string ExchangeName => "WhiteBit"; + /// + /// Exchange name + /// + public static string DisplayName => "WhiteBit"; + + /// + /// Url to exchange image + /// + public static string ImageUrl { get; } = "https://raw.githubusercontent.com/JKorf/WhiteBit.Net/master/WhiteBit.Net/Icon/icon.png"; + /// /// Url to the main website /// diff --git a/docs/index.html b/docs/index.html index d188328..99e3144 100644 --- a/docs/index.html +++ b/docs/index.html @@ -189,8 +189,14 @@

API Access

WhiteBit.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.AddWhiteBit(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.AddWhiteBit(builder.Configuration.GetSection("WhiteBit"));
+		  
+// OR
+		  
+ builder.Services.AddWhiteBit(options => {
+  // Configure options in code
   options.ApiCredentials = new ApiCredentials("APIKEY", "APISECRET");
 });