Skip to content

Commit

Permalink
feat: examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jirikostiha committed Nov 7, 2024
1 parent e174a15 commit 8c80dd9
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 29 deletions.
13 changes: 13 additions & 0 deletions src/Examples/E1.cs
Original file line number Diff line number Diff line change
@@ -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");
}
}
30 changes: 30 additions & 0 deletions src/Examples/E2.cs
Original file line number Diff line number Diff line change
@@ -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<XApiClient>();

await client.ConnectAsync();
var loginResponse = await client.LoginAsync("16697884", "xoh11724");
}
}
8 changes: 8 additions & 0 deletions src/Examples/ExampleBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Threading.Tasks;

namespace Xtb.XApi.Examples;

public abstract class ExampleBase
{
public abstract Task Run();
}
17 changes: 17 additions & 0 deletions src/Examples/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
21 changes: 21 additions & 0 deletions src/Examples/Xtb.XApi.Examples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<AnalysisLevel>latest-recommended</AnalysisLevel>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
<Version>2.5.19</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Extensions.DependencyInjection\Xtb.XApi.Extensions.DependencyInjection.csproj" />
</ItemGroup>

</Project>
17 changes: 0 additions & 17 deletions src/Extensions.DependencyInjection/XApiClientOptions.cs

This file was deleted.

27 changes: 27 additions & 0 deletions src/Extensions.DependencyInjection/XApiClientServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Xtb.XApi.Extensions.DependencyInjection;

/// <summary>
/// Options for configuring the <see cref="XApiClient" />.
/// </summary>
public record XApiClientServiceOptions
{
/// <summary>
/// The address of the X API service.
/// </summary>
public string Address { get; set; }

Check warning on line 11 in src/Extensions.DependencyInjection/XApiClientServiceOptions.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Address' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

/// <summary>
/// The port number for requests.
/// </summary>
public int MainPort { get; set; }

/// <summary>
/// The port number used for streaming connections.
/// </summary>
public int StreamingPort { get; set; }

/// <summary>
/// The streaming listener instance that will handle streaming data.
/// </summary>
public IStreamingListener? StreamingListener { get; set; }
}
18 changes: 9 additions & 9 deletions src/Extensions.DependencyInjection/XApiServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Xtb.XApi.Extensions.DependencyInjection;

/// <summary>
/// Configuration extension methods for <see cref="Xtb.XApi.XApiClient"/>.
/// Configuration extension methods for <see cref="XApiClient"/>.
/// </summary>
public static class XApiServiceExtensions
{
Expand All @@ -15,18 +15,18 @@ public static class XApiServiceExtensions
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="setupAction">
/// The <see cref="Action{AddXApiClientOptions}"/> to configure the provided <see cref="AddXApiClientOptions"/>.
/// The <see cref="Action{XApiClientOptions}"/> to configure the provided <see cref="XApiClientServiceOptions"/>.
/// </param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddXApiClient(this IServiceCollection services, Action<XApiClientOptions> setupAction)
public static IServiceCollection AddXApiClient(this IServiceCollection services, Action<XApiClientServiceOptions> setupAction)
{
#if NET7_0_OR_GREATER
ArgumentNullException.ThrowIfNull(services);
#else
_ = services ?? throw new ArgumentNullException(nameof(services));
#endif

var options = new XApiClientOptions();
var options = new XApiClientServiceOptions();
setupAction(options);
services.Configure(setupAction);

Expand All @@ -44,25 +44,25 @@ public static IServiceCollection AddXApiClient(this IServiceCollection services,
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="setupAction">
/// The <see cref="Action{AddXApiClientOptions}"/> to configure the provided <see cref="XApiClientOptions"/>.
/// The <see cref="Action{XApiClientOptions}"/> to configure the provided <see cref="XApiClientServiceOptions"/>.
/// </param>
/// <param name="key">The service key. </param>
/// <returns>The <see cref="IServiceCollection"/> so that additional calls can be chained.</returns>
public static IServiceCollection AddXApiClient(this IServiceCollection services, string key, Action<XApiClientOptions> setupAction)
public static IServiceCollection AddXApiClient(this IServiceCollection services, string key, Action<XApiClientServiceOptions> setupAction)
{
#if NET7_0_OR_GREATER
ArgumentNullException.ThrowIfNull(services);
#else
_ = 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<IXApiClientAsync>(provider => xapiClient));
services.TryAdd(ServiceDescriptor.KeyedSingleton(key, xapiClient));
services.TryAdd(ServiceDescriptor.KeyedSingleton<IXApiClient>(key, xapiClient));
services.TryAdd(ServiceDescriptor.KeyedSingleton<IXApiClientSync>(key, xapiClient));
services.TryAdd(ServiceDescriptor.KeyedSingleton<IXApiClientAsync>(key, xapiClient));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>

<Title>SyncAPIConnect - xtb XApi</Title>
<Description>todo</Description>
<Title>SyncAPIConnect dependency injection extensions</Title>
<Description></Description>
<PackageTags>xapi; xtb; trading; markets; xopenhub; csharp</PackageTags>
<PackageProjectUrl>https://github.com/jirikostiha/xtb-xApi</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<SignAssembly>False</SignAssembly>
<Version>2.5.18</Version>
<Version>2.5.19</Version>
<Authors>Jiri Kostiha</Authors>
<PackageIcon>package_icon.png</PackageIcon>
</PropertyGroup>
Expand Down
7 changes: 7 additions & 0 deletions src/xtb.xapi.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 8c80dd9

Please sign in to comment.