Skip to content

Commit

Permalink
add Client
Browse files Browse the repository at this point in the history
  • Loading branch information
forsthug committed Mar 20, 2024
1 parent d3ccba1 commit 1050fe4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/fiskaltrust.Launcher/Clients/ATSSCDClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using fiskaltrust.ifPOS.v1.at;
using fiskaltrust.Launcher.Common.Configuration;
using fiskaltrust.Middleware.Abstractions;
using fiskaltrust.Middleware.Interface.Client;
using fiskaltrust.Middleware.Interface.Client.Grpc;
using fiskaltrust.Middleware.Interface.Client.Http;
using fiskaltrust.Middleware.Interface.Client.Http.ATSSCD;
using fiskaltrust.Middleware.Interface.Client.Soap;
using Grpc.Core;
using Grpc.Net.Client;

namespace fiskaltrust.Launcher.Clients
{
public class ATSSCDClientFactory : IClientFactory<IATSSCD>
{
private readonly LauncherConfiguration _launcherConfiguration;

public ATSSCDClientFactory(LauncherConfiguration launcherConfiguration) => _launcherConfiguration = launcherConfiguration;

public IATSSCD CreateClient(ClientConfiguration configuration)
{
if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}

var retryPolicyoptions = new RetryPolicyOptions
{
DelayBetweenRetries = configuration.DelayBetweenRetries != default ? configuration.DelayBetweenRetries : RetryPolicyOptions.Default.DelayBetweenRetries,
Retries = configuration.RetryCount ?? RetryPolicyOptions.Default.Retries,
ClientTimeout = configuration.Timeout != default ? configuration.Timeout : RetryPolicyOptions.Default.ClientTimeout
};

var isHttps = !string.IsNullOrEmpty(_launcherConfiguration?.TlsCertificatePath) || !string.IsNullOrEmpty(_launcherConfiguration?.TlsCertificateBase64);
var sslValidationDisabled = _launcherConfiguration != null && _launcherConfiguration.SslValidation.HasValue && !_launcherConfiguration.SslValidation.Value;

return configuration.UrlType switch
{
"grpc" => GrpcATSSCDFactory.CreateSSCDAsync(new GrpcClientOptions
{
Url = new Uri(configuration.Url.Replace("grpc://", isHttps ? "https://" : "http://")),
RetryPolicyOptions = retryPolicyoptions,
ChannelOptions = new GrpcChannelOptions
{
Credentials = isHttps ? ChannelCredentials.SecureSsl : ChannelCredentials.Insecure,
HttpHandler = isHttps && sslValidationDisabled ? new HttpClientHandler { ServerCertificateCustomValidationCallback = (httpRequestMessage, cert, cetChain, policyErrors) => true } : null
}
}).Result,
"rest" => HttpATSSCDFactory.CreateSSCDAsync(new HttpATSSCDClientOptions
{
Url = new Uri(configuration.Url.Replace("rest://", isHttps ? "https://" : "http://")),
RetryPolicyOptions = retryPolicyoptions,
DisableSslValidation = sslValidationDisabled
}).Result,
"http" or "https" or "net.tcp" => SoapATSSCDFactory.CreateSSCDAsync(new ClientOptions
{
Url = new Uri(configuration.Url),
RetryPolicyOptions = retryPolicyoptions
}).Result,
_ => throw new ArgumentException("This version of the fiskaltrust Launcher currently only supports gRPC, REST and SOAP communication."),
};
}
}
}
3 changes: 3 additions & 0 deletions src/fiskaltrust.Launcher/Commands/HostCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using fiskaltrust.Launcher.Helpers;
using ILogger = Microsoft.Extensions.Logging.ILogger;
using fiskaltrust.Launcher.Factories;
using fiskaltrust.ifPOS.v1.at;

namespace fiskaltrust.Launcher.Commands
{
Expand Down Expand Up @@ -133,6 +134,7 @@ public static async Task<int> HandleAsync(HostOptions hostOptions, HostServices

services.AddSingleton<IClientFactory<IDESSCD>, DESSCDClientFactory>();
services.AddSingleton<IClientFactory<IITSSCD>, ITSSCDClientFactory>();
services.AddSingleton<IClientFactory<IATSSCD>, ATSSCDClientFactory>();
services.AddSingleton<IClientFactory<IPOS>, POSClientFactory>();

using var downloader = new PackageDownloader(services.BuildServiceProvider().GetRequiredService<ILogger<PackageDownloader>>(), launcherConfiguration, hostServices.LauncherExecutablePath);
Expand All @@ -150,6 +152,7 @@ public static async Task<int> HandleAsync(HostOptions hostOptions, HostServices
typeof(IClientFactory<IPOS>),
typeof(IClientFactory<IDESSCD>),
typeof(IClientFactory<IITSSCD>),
typeof(IClientFactory<IATSSCD>),
typeof(JournalRequest),
typeof(JournalResponse),
typeof(IHelper),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public static WebApplication AddScuAtEndpoints(this WebApplication app, IATSSCD
{
app.MapMultiplePrefixed(_prefixesV1, "Certificate", EndpointRouteBuilderExtensions.MapGet, async () => await ssat.CertificateAsync());
app.MapMultiplePrefixed(_prefixesV1, "ZDA", EndpointRouteBuilderExtensions.MapGet, async () => await ssat.ZdaAsync());
app.MapMultiplePrefixed(_prefixesV1, "Sign", EndpointRouteBuilderExtensions.MapGet, async (SignRequest req) => await ssat.SignAsync(req));
app.MapMultiplePrefixed(_prefixesV1, "Echo", EndpointRouteBuilderExtensions.MapGet, async (ifPOS.v1.at.EchoRequest req) => await ssat.EchoAsync(req));
return app;
}
}
Expand Down

0 comments on commit 1050fe4

Please sign in to comment.