From 8c80dd9235e2634f6c992ce6a1558d7bc705c1a8 Mon Sep 17 00:00:00 2001 From: Jiri Kostiha Date: Thu, 17 Oct 2024 23:52:52 +0200 Subject: [PATCH] feat: examples --- src/Examples/E1.cs | 13 ++++++++ src/Examples/E2.cs | 30 +++++++++++++++++++ src/Examples/ExampleBase.cs | 8 +++++ src/Examples/Program.cs | 17 +++++++++++ src/Examples/Xtb.XApi.Examples.csproj | 21 +++++++++++++ .../XApiClientOptions.cs | 17 ----------- .../XApiClientServiceOptions.cs | 27 +++++++++++++++++ .../XApiServiceExtensions.cs | 18 +++++------ ...XApi.Extensions.DependencyInjection.csproj | 6 ++-- src/xtb.xapi.sln | 7 +++++ 10 files changed, 135 insertions(+), 29 deletions(-) create mode 100644 src/Examples/E1.cs create mode 100644 src/Examples/E2.cs create mode 100644 src/Examples/ExampleBase.cs create mode 100644 src/Examples/Program.cs create mode 100644 src/Examples/Xtb.XApi.Examples.csproj delete mode 100644 src/Extensions.DependencyInjection/XApiClientOptions.cs create mode 100644 src/Extensions.DependencyInjection/XApiClientServiceOptions.cs diff --git a/src/Examples/E1.cs b/src/Examples/E1.cs new file mode 100644 index 0000000..39c9440 --- /dev/null +++ b/src/Examples/E1.cs @@ -0,0 +1,13 @@ +using System.Threading.Tasks; + +namespace Xtb.XApi.Examples; + +public class E1 : ExampleBase +{ + public override async Task Run() + { + var client = XApiClient.Create("81.2.190.163", 5124, 5125); + await client.ConnectAsync(); + var loginResponse = await client.LoginAsync("16697884", "xoh11724"); + } +} \ No newline at end of file diff --git a/src/Examples/E2.cs b/src/Examples/E2.cs new file mode 100644 index 0000000..dcf8e48 --- /dev/null +++ b/src/Examples/E2.cs @@ -0,0 +1,30 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using System.Threading.Tasks; +using Xtb.XApi.Extensions.DependencyInjection; + +namespace Xtb.XApi.Examples; + +public class E2 : ExampleBase +{ + public override async Task Run() + { + var hostBuilder = Host.CreateDefaultBuilder() + .ConfigureServices((context, services) => + { + services.AddXApiClient(options => + { + options.Address = "81.2.190.163"; + options.MainPort = 5124; + options.StreamingPort = 5125; + }); + }); + + var host = hostBuilder.Build(); + + var client = host.Services.GetRequiredService(); + + await client.ConnectAsync(); + var loginResponse = await client.LoginAsync("16697884", "xoh11724"); + } +} \ No newline at end of file diff --git a/src/Examples/ExampleBase.cs b/src/Examples/ExampleBase.cs new file mode 100644 index 0000000..a9b9a33 --- /dev/null +++ b/src/Examples/ExampleBase.cs @@ -0,0 +1,8 @@ +using System.Threading.Tasks; + +namespace Xtb.XApi.Examples; + +public abstract class ExampleBase +{ + public abstract Task Run(); +} \ No newline at end of file diff --git a/src/Examples/Program.cs b/src/Examples/Program.cs new file mode 100644 index 0000000..2419536 --- /dev/null +++ b/src/Examples/Program.cs @@ -0,0 +1,17 @@ +using System; +using System.Threading.Tasks; +using Xtb.XApi.Examples; + +namespace Xtb.XApi.SystemTests; + +internal static class Program +{ + private static async Task Main(string[] args) + { + //await new E1().Run(); + await new E2().Run(); + + Console.WriteLine("Done."); + Console.Read(); + } +} \ No newline at end of file diff --git a/src/Examples/Xtb.XApi.Examples.csproj b/src/Examples/Xtb.XApi.Examples.csproj new file mode 100644 index 0000000..81493ab --- /dev/null +++ b/src/Examples/Xtb.XApi.Examples.csproj @@ -0,0 +1,21 @@ + + + + net8 + latest + enable + latest-recommended + Exe + false + 2.5.19 + + + + + + + + + + + diff --git a/src/Extensions.DependencyInjection/XApiClientOptions.cs b/src/Extensions.DependencyInjection/XApiClientOptions.cs deleted file mode 100644 index bc05089..0000000 --- a/src/Extensions.DependencyInjection/XApiClientOptions.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.Extensions.Options; - -namespace Xtb.XApi.Extensions.DependencyInjection; - -public record XApiClientOptions //: IOptions NO -{ - public string Address { get; set; } - - public int MainPort { get; set; } - - public int StreamingPort { get; set; } - - public IStreamingListener StreamingListener { get; set; } - - /// - //public AddXApiClientOptions Value => this; -} \ No newline at end of file diff --git a/src/Extensions.DependencyInjection/XApiClientServiceOptions.cs b/src/Extensions.DependencyInjection/XApiClientServiceOptions.cs new file mode 100644 index 0000000..4a99d5b --- /dev/null +++ b/src/Extensions.DependencyInjection/XApiClientServiceOptions.cs @@ -0,0 +1,27 @@ +namespace Xtb.XApi.Extensions.DependencyInjection; + +/// +/// Options for configuring the . +/// +public record XApiClientServiceOptions +{ + /// + /// The address of the X API service. + /// + public string Address { get; set; } + + /// + /// The port number for requests. + /// + public int MainPort { get; set; } + + /// + /// The port number used for streaming connections. + /// + public int StreamingPort { get; set; } + + /// + /// The streaming listener instance that will handle streaming data. + /// + public IStreamingListener? StreamingListener { get; set; } +} diff --git a/src/Extensions.DependencyInjection/XApiServiceExtensions.cs b/src/Extensions.DependencyInjection/XApiServiceExtensions.cs index c7a5309..209564b 100644 --- a/src/Extensions.DependencyInjection/XApiServiceExtensions.cs +++ b/src/Extensions.DependencyInjection/XApiServiceExtensions.cs @@ -6,7 +6,7 @@ namespace Xtb.XApi.Extensions.DependencyInjection; /// -/// Configuration extension methods for . +/// Configuration extension methods for . /// public static class XApiServiceExtensions { @@ -15,10 +15,10 @@ public static class XApiServiceExtensions /// /// The to add services to. /// - /// The to configure the provided . + /// The to configure the provided . /// /// The so that additional calls can be chained. - public static IServiceCollection AddXApiClient(this IServiceCollection services, Action setupAction) + public static IServiceCollection AddXApiClient(this IServiceCollection services, Action setupAction) { #if NET7_0_OR_GREATER ArgumentNullException.ThrowIfNull(services); @@ -26,7 +26,7 @@ public static IServiceCollection AddXApiClient(this IServiceCollection services, _ = services ?? throw new ArgumentNullException(nameof(services)); #endif - var options = new XApiClientOptions(); + var options = new XApiClientServiceOptions(); setupAction(options); services.Configure(setupAction); @@ -44,11 +44,11 @@ public static IServiceCollection AddXApiClient(this IServiceCollection services, /// /// The to add services to. /// - /// The to configure the provided . + /// The to configure the provided . /// /// The service key. /// The so that additional calls can be chained. - public static IServiceCollection AddXApiClient(this IServiceCollection services, string key, Action setupAction) + public static IServiceCollection AddXApiClient(this IServiceCollection services, string key, Action setupAction) { #if NET7_0_OR_GREATER ArgumentNullException.ThrowIfNull(services); @@ -56,13 +56,13 @@ public static IServiceCollection AddXApiClient(this IServiceCollection services, _ = services ?? throw new ArgumentNullException(nameof(services)); #endif - var options = new XApiClientOptions(); + var options = new XApiClientServiceOptions(); setupAction(options); services.Configure(setupAction); var xapiClient = XApiClient.Create(options.Address, options.MainPort, options.StreamingPort, options.StreamingListener); - services.TryAddKeyedSingleton(key, ServiceDescriptor.Singleton(xapiClient)); - services.TryAddKeyedSingleton(key, ServiceDescriptor.Singleton(provider => xapiClient)); + services.TryAdd(ServiceDescriptor.KeyedSingleton(key, xapiClient)); + services.TryAdd(ServiceDescriptor.KeyedSingleton(key, xapiClient)); services.TryAdd(ServiceDescriptor.KeyedSingleton(key, xapiClient)); services.TryAdd(ServiceDescriptor.KeyedSingleton(key, xapiClient)); diff --git a/src/Extensions.DependencyInjection/Xtb.XApi.Extensions.DependencyInjection.csproj b/src/Extensions.DependencyInjection/Xtb.XApi.Extensions.DependencyInjection.csproj index e9dc8e8..ebb6cbe 100644 --- a/src/Extensions.DependencyInjection/Xtb.XApi.Extensions.DependencyInjection.csproj +++ b/src/Extensions.DependencyInjection/Xtb.XApi.Extensions.DependencyInjection.csproj @@ -14,13 +14,13 @@ git true - SyncAPIConnect - xtb XApi - todo + SyncAPIConnect dependency injection extensions + xapi; xtb; trading; markets; xopenhub; csharp https://github.com/jirikostiha/xtb-xApi MIT False - 2.5.18 + 2.5.19 Jiri Kostiha package_icon.png diff --git a/src/xtb.xapi.sln b/src/xtb.xapi.sln index 94dc658..67c316d 100644 --- a/src/xtb.xapi.sln +++ b/src/xtb.xapi.sln @@ -26,6 +26,7 @@ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtb.XApi.SystemTests", "SystemTests\Xtb.XApi.SystemTests.csproj", "{82751AFF-1738-409A-A6EF-BFE2E3ABDA9E}" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtb.XApi.UnitTests", "UnitTests\Xtb.XApi.UnitTests.csproj", "{84A4454D-B5D9-47E3-A373-6AE80A085FEB}" +EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtb.XApi.Extensions.DependencyInjection", "Extensions.DependencyInjection\Xtb.XApi.Extensions.DependencyInjection.csproj", "{9C29601D-CF3D-4850-9349-BD975FE99667}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "templates", "templates", "{12D2B3B2-9C30-4008-B796-830E969219BE}" @@ -34,6 +35,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "templates", "templates", "{ ..\.github\ISSUE_TEMPLATE\feature_request.md = ..\.github\ISSUE_TEMPLATE\feature_request.md EndProjectSection EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Xtb.XApi.Examples", "Examples\Xtb.XApi.Examples.csproj", "{7D1382E8-AA25-4868-928A-8623515D93BD}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -56,6 +59,10 @@ Global {9C29601D-CF3D-4850-9349-BD975FE99667}.Debug|Any CPU.Build.0 = Debug|Any CPU {9C29601D-CF3D-4850-9349-BD975FE99667}.Release|Any CPU.ActiveCfg = Release|Any CPU {9C29601D-CF3D-4850-9349-BD975FE99667}.Release|Any CPU.Build.0 = Release|Any CPU + {7D1382E8-AA25-4868-928A-8623515D93BD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7D1382E8-AA25-4868-928A-8623515D93BD}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7D1382E8-AA25-4868-928A-8623515D93BD}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7D1382E8-AA25-4868-928A-8623515D93BD}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE