Skip to content

Commit

Permalink
fix: always use en-US as culture
Browse files Browse the repository at this point in the history
Signed-off-by: JobaDiniz <jobertodinizjunior@gmail.com>
  • Loading branch information
JobaDiniz committed Apr 15, 2024
1 parent af77634 commit 9288c9c
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 70 deletions.
148 changes: 96 additions & 52 deletions src/Joba.IBM.RPA.Cli/Client/RpaClient.cs

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions src/Joba.IBM.RPA.Cli/Client/RpaClientFactory.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
using System.Net.Http.Headers;
using System.Globalization;
using System.Net.Http.Headers;

namespace Joba.IBM.RPA.Cli
{
internal class RpaClientFactory : IRpaClientFactory
{
private readonly IConsole console;
private readonly IRpaHttpClientFactory httpFactory;
private readonly CultureInfo culture;

public RpaClientFactory(IConsole console, IRpaHttpClientFactory httpFactory)
public RpaClientFactory(IConsole console, IRpaHttpClientFactory httpFactory, CultureInfo culture)
{
this.console = console;
this.httpFactory = httpFactory;
this.culture = culture;
}

public IRpaClient CreateFromAddress(Uri address) => new RpaClient(httpFactory.Create(address));
public IRpaClient CreateFromAddress(Uri address) => new RpaClient(httpFactory.Create(address), culture);
IRpaClient IRpaClientFactory.CreateFromRegion(Region region) => ((IRpaClientFactory)this).CreateFromAddress(region.ApiAddress);

IRpaClient IRpaClientFactory.CreateFromPackageSource(PackageSource source)
Expand All @@ -22,7 +25,7 @@ IRpaClient IRpaClientFactory.CreateFromPackageSource(PackageSource source)
var sessionEnsurer = new SessionEnsurer(console, authenticatorFactory, source.Session);
var client = httpFactory.Create(source.Remote.Address, new RenewExpiredSession(sessionEnsurer));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", source.Session.Current.Token);
return new RpaClient(client);
return new RpaClient(client, culture);
}

IRpaClient IRpaClientFactory.CreateFromEnvironment(Environment environment)
Expand All @@ -31,7 +34,7 @@ IRpaClient IRpaClientFactory.CreateFromEnvironment(Environment environment)
var sessionEnsurer = new SessionEnsurer(console, authenticatorFactory, environment.Session);
var client = httpFactory.Create(environment.Remote.Address, new RenewExpiredSession(sessionEnsurer));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", environment.Session.Current.Token);
return new RpaClient(client);
return new RpaClient(client, culture);
}

class RenewExpiredSession : IRenewExpiredSession
Expand Down
6 changes: 4 additions & 2 deletions src/Joba.IBM.RPA.Cli/Environment/ServerSelector.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Joba.IBM.RPA.Cli
using System.Globalization;

namespace Joba.IBM.RPA.Cli
{
internal class ServerSelector
{
Expand All @@ -20,7 +22,7 @@ public async Task<ServerConfig> SelectAsync(ServerAddress address, CancellationT
address = address.IsDefined ? address : SelectServerAddress();
using var client = clientFactory.CreateFromAddress(address.ToUri());

var server = await client.GetConfigurationAsync(cancellation);
var server = await client.GetConfigurationAsync(CultureInfo.GetCultureInfo("en-US"), cancellation);
server.EnsureValid(supportedServerVersion);

return server;
Expand Down
3 changes: 2 additions & 1 deletion src/Joba.IBM.RPA.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using System.CommandLine.Builder;
using System.CommandLine.Parsing;
using System.Globalization;
using System.Reflection;

namespace Joba.IBM.RPA.Cli
Expand Down Expand Up @@ -87,7 +88,7 @@ private static async Task Middleware(InvocationContext context, Func<InvocationC
context.BindingContext.AddService<IAccountAuthenticatorFactory>(s => new AccountAuthenticatorFactory(
s.GetRequiredService<IRpaClientFactory>(), s.GetRequiredService<IRpaHttpClientFactory>()));
context.BindingContext.AddService<IRpaClientFactory>(s => new RpaClientFactory(
context.Console, s.GetRequiredService<IRpaHttpClientFactory>()));
context.Console, s.GetRequiredService<IRpaHttpClientFactory>(), CultureInfo.GetCultureInfo("en-US")));
context.BindingContext.AddService<IDeployService>(s => new DeployService(
loggerFactory.CreateLogger<DeployService>(),
s.GetRequiredService<IRpaClientFactory>(),
Expand Down
3 changes: 2 additions & 1 deletion src/Joba.IBM.RPA/IRpaClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Joba.IBM.RPA.Server;
using System.Globalization;

namespace Joba.IBM.RPA
{
public interface IRpaClient : IDisposable
{
Uri Address { get; }
Task<ServerConfig> GetConfigurationAsync(CancellationToken cancellation);
Task<ServerConfig> GetConfigurationAsync(CultureInfo culture, CancellationToken cancellation);
IAccountResource Account { get; }
IScriptResource Script { get; }
IParameterResource Parameter { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using Moq;
using System.CommandLine;
using System.Globalization;
using Xunit.Abstractions;
using static Joba.IBM.RPA.Cli.PackageCommand;

Expand Down Expand Up @@ -66,7 +67,7 @@ public async Task CreatePackageSourcesFile()
var credentials = new AccountCredentials(options.TenantCode!.Value, options.UserName!, options.Password!);
var console = new Mock<IConsole>();
var client = new Mock<IRpaClient>();
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CancellationToken>())).ReturnsAsync(config);
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CultureInfo>(), It.IsAny<CancellationToken>())).ReturnsAsync(config);
var clientFactory = new Mock<IRpaClientFactory>();
clientFactory.Setup(c => c.CreateFromAddress(region.ApiAddress)).Returns(client.Object);
clientFactory.Setup(c => c.CreateFromRegion(region)).Returns(client.Object);
Expand Down Expand Up @@ -109,7 +110,7 @@ public async Task UpdatePackageSourcesFile()
var credentials = new AccountCredentials(options.TenantCode!.Value, options.UserName!, options.Password!);
var console = new Mock<IConsole>();
var client = new Mock<IRpaClient>();
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CancellationToken>())).ReturnsAsync(config);
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CultureInfo>(), It.IsAny<CancellationToken>())).ReturnsAsync(config);
var clientFactory = new Mock<IRpaClientFactory>();
clientFactory.Setup(c => c.CreateFromAddress(region.ApiAddress)).Returns(client.Object);
clientFactory.Setup(c => c.CreateFromRegion(region)).Returns(client.Object);
Expand Down Expand Up @@ -150,7 +151,7 @@ public async Task UpdatePackageSourcesFile_WithCloudPakOpenShift()
var credentials = new AccountCredentials(options.TenantCode!.Value, options.UserName!, options.Password!);
var console = new Mock<IConsole>();
var client = new Mock<IRpaClient>();
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CancellationToken>())).ReturnsAsync(config);
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CultureInfo>(), It.IsAny<CancellationToken>())).ReturnsAsync(config);
var clientFactory = new Mock<IRpaClientFactory>();
clientFactory.Setup(c => c.CreateFromAddress(region.ApiAddress)).Returns(client.Object);
clientFactory.Setup(c => c.CreateFromRegion(region)).Returns(client.Object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Logging;
using Moq;
using System.CommandLine;
using System.Globalization;
using System.Net;
using Xunit.Abstractions;
using static Joba.IBM.RPA.Cli.EnvironmentCommand.NewEnvironmentCommand;
Expand Down Expand Up @@ -86,7 +87,7 @@ public async Task AddEnvironment()
var credentials = new AccountCredentials(options.TenantCode!.Value, options.UserName!, options.Password!);
var console = new Mock<IConsole>();
var client = new Mock<IRpaClient>();
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CancellationToken>())).ReturnsAsync(config);
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CultureInfo>(), It.IsAny<CancellationToken>())).ReturnsAsync(config);
var clientFactory = new Mock<IRpaClientFactory>();
clientFactory.Setup(c => c.CreateFromAddress(region.ApiAddress)).Returns(client.Object);
clientFactory.Setup(c => c.CreateFromRegion(region)).Returns(client.Object);
Expand Down Expand Up @@ -128,7 +129,7 @@ public async Task AddEnvironment_UsingSecretFromSystemVariables()
var credentials = new AccountCredentials(options.TenantCode!.Value, options.UserName!, password);
var console = new Mock<IConsole>();
var client = new Mock<IRpaClient>();
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CancellationToken>())).ReturnsAsync(config);
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CultureInfo>(), It.IsAny<CancellationToken>())).ReturnsAsync(config);
var clientFactory = new Mock<IRpaClientFactory>();
clientFactory.Setup(c => c.CreateFromAddress(region.ApiAddress)).Returns(client.Object);
clientFactory.Setup(c => c.CreateFromRegion(region)).Returns(client.Object);
Expand Down Expand Up @@ -170,7 +171,7 @@ public async Task AddCloudPakOpenShiftEnvironment()
var credentials = new AccountCredentials(options.TenantCode!.Value, options.UserName!, options.Password!);
var console = new Mock<IConsole>();
var client = new Mock<IRpaClient>();
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CancellationToken>())).ReturnsAsync(config);
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CultureInfo>(), It.IsAny<CancellationToken>())).ReturnsAsync(config);
var clientFactory = new Mock<IRpaClientFactory>();
clientFactory.Setup(c => c.CreateFromAddress(region.ApiAddress)).Returns(client.Object);
clientFactory.Setup(c => c.CreateFromRegion(region)).Returns(client.Object);
Expand Down
7 changes: 4 additions & 3 deletions src/Tests/Joba.IBM.RPA.Cli.Tests/ServerSelectorShould.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Moq;
using System.CommandLine;
using System.Globalization;

namespace Joba.IBM.RPA.Cli.Tests
{
Expand All @@ -15,7 +16,7 @@ public async Task ThrowException_WhenServerVersionIsNotSupported()
var server = new ServerConfig { Version = new Version("20.12.0"), Deployment = DeploymentOption.SaaS, AuthenticationMethod = AuthenticationMethod.WDG };
var console = new Mock<IConsole>();
var client = new Mock<IRpaClient>();
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CancellationToken>())).ReturnsAsync(server);
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CultureInfo>(), It.IsAny<CancellationToken>())).ReturnsAsync(server);
var clientFactory = new Mock<IRpaClientFactory>();
clientFactory.Setup(c => c.CreateFromAddress(It.Is<Uri>(u => u.AbsoluteUri == url))).Returns(client.Object);
var project = new Mock<IProject>();
Expand All @@ -35,7 +36,7 @@ public async Task ThrowException_WhenDeploymentOptionIsNotSupported()
var server = new ServerConfig { Version = RpaCommand.SupportedServerVersion, Deployment = new DeploymentOption("not-supported"), AuthenticationMethod = AuthenticationMethod.WDG };
var console = new Mock<IConsole>();
var client = new Mock<IRpaClient>();
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CancellationToken>())).ReturnsAsync(server);
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CultureInfo>(), It.IsAny<CancellationToken>())).ReturnsAsync(server);
var clientFactory = new Mock<IRpaClientFactory>();
clientFactory.Setup(c => c.CreateFromAddress(It.Is<Uri>(u => u.AbsoluteUri == url))).Returns(client.Object);
var project = new Mock<IProject>();
Expand All @@ -55,7 +56,7 @@ public async Task ThrowException_WhenAuthenticationMethodIsNotSupported()
var server = new ServerConfig { Version = RpaCommand.SupportedServerVersion, Deployment = DeploymentOption.SaaS, AuthenticationMethod = new AuthenticationMethod("not-supported") };
var console = new Mock<IConsole>();
var client = new Mock<IRpaClient>();
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CancellationToken>())).ReturnsAsync(server);
client.Setup(c => c.GetConfigurationAsync(It.IsAny<CultureInfo>(), It.IsAny<CancellationToken>())).ReturnsAsync(server);
var clientFactory = new Mock<IRpaClientFactory>();
clientFactory.Setup(c => c.CreateFromAddress(It.Is<Uri>(u => u.AbsoluteUri == url))).Returns(client.Object);
var project = new Mock<IProject>();
Expand Down

0 comments on commit 9288c9c

Please sign in to comment.