Skip to content

Commit

Permalink
feat: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclair-aefinder committed Aug 2, 2024
1 parent a446348 commit 35fec97
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
namespace AeFinder.Cli.Auth;

[Dependency(ReplaceServices = true)]
public class AeFinderIdentityModelAuthenticationService : IdentityModelAuthenticationService,
IIdentityModelAuthenticationService, ITransientDependency
public class AeFinderIdentityModelAuthenticationService : IdentityModelAuthenticationService
{
public AeFinderIdentityModelAuthenticationService(IOptions<AbpIdentityClientOptions> options,
ICancellationTokenProvider cancellationTokenProvider, IHttpClientFactory httpClientFactory,
Expand Down Expand Up @@ -51,7 +50,7 @@ await CreatePasswordTokenRequestAsync(configuration),
}
}

private async Task<TokenResponse> RequestClientCredentialsTokenAsync(HttpMessageInvoker client,
private static async Task<TokenResponse> RequestClientCredentialsTokenAsync(HttpMessageInvoker client,
ClientCredentialsTokenRequest request, CancellationToken cancellationToken = default)
{
var clone = request.Clone();
Expand Down
4 changes: 1 addition & 3 deletions src/AeFinder.Cli.Core/Auth/AuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ namespace AeFinder.Cli.Auth;
public class AuthService : IAuthService, ITransientDependency
{
private readonly IIdentityModelAuthenticationService _authenticationService;

private string _accessToken = string.Empty;


public AuthService(IIdentityModelAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
Expand Down
2 changes: 1 addition & 1 deletion src/AeFinder.Cli.Core/CliConsts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace AeFinder.Cli;

public static class CliConsts
{
public static readonly Dictionary<AeFinderNetwork, AeFinderEndpoint> AeFinderEndpoints =
public static Dictionary<AeFinderNetwork, AeFinderEndpoint> AeFinderEndpoints =
new()
{
{
Expand Down
4 changes: 2 additions & 2 deletions src/AeFinder.Cli.Core/CliService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ private static Type[] LoadVerbs()
private async Task RunAsync(object obj)
{
var commonOptions = obj as CommonOptions;
_logger.LogInformation("Network : {network}", commonOptions.Network);
_logger.LogInformation("AppId : {appid}", commonOptions.AppId);
_logger.LogInformation("Network : {Network}", commonOptions.Network);
_logger.LogInformation("AppId : {AppId}", commonOptions.AppId);

switch (obj)
{
Expand Down
5 changes: 1 addition & 4 deletions src/AeFinder.Cli.Core/Http/CliHttpClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ public class CliHttpClientFactory : ISingletonDependency
public static readonly TimeSpan DefaultTimeout = TimeSpan.FromMinutes(2);

private readonly IHttpClientFactory _clientFactory;
private readonly ICancellationTokenProvider _cancellationTokenProvider;

public CliHttpClientFactory(IHttpClientFactory clientFactory,
ICancellationTokenProvider cancellationTokenProvider)
public CliHttpClientFactory(IHttpClientFactory clientFactory)
{
_clientFactory = clientFactory;
_cancellationTokenProvider = cancellationTokenProvider;
}

public HttpClient CreateClient(string accessToken = null, TimeSpan? timeout = null)
Expand Down
5 changes: 3 additions & 2 deletions src/AeFinder.Cli.Core/Http/RemoteServiceExceptionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Volo.Abp.Http;
using Volo.Abp.Json;
using System.Text.Json;
using Volo.Abp;

namespace AeFinder.Cli;

Expand Down Expand Up @@ -36,7 +37,7 @@ public async Task EnsureSuccessfulHttpResponseAsync(HttpResponseMessage response
exceptionMessage += remoteServiceErrorMessage;
}

throw new Exception(exceptionMessage);
throw new UserFriendlyException(exceptionMessage);
}

public async Task<string> GetAbpRemoteServiceErrorAsync(HttpResponseMessage responseMessage)
Expand Down Expand Up @@ -75,7 +76,7 @@ await responseMessage.Content.ReadAsStringAsync()
sbError.Append("Message: " + errorResult.Error.Message);
}

if (errorResult.Error.ValidationErrors != null && errorResult.Error.ValidationErrors.Any())
if (errorResult.Error.ValidationErrors != null && errorResult.Error.ValidationErrors.Length > 0)
{
if (sbError.Length > 0)
{
Expand Down
10 changes: 5 additions & 5 deletions src/AeFinder.Cli.Core/Services/AppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public async Task DeployAsync(DeployAppOptions options)
var client = _cliHttpClientFactory.CreateClient(token);

var formDataContent = new MultipartFormDataContent();
formDataContent.Add(new StringContent(File.ReadAllText(options.Manifest)), "Manifest");
formDataContent.Add(new StreamContent(new MemoryStream(File.ReadAllBytes(options.Code))), "Code", "code.dll");
formDataContent.Add(new StringContent(await File.ReadAllTextAsync(options.Manifest)), "Manifest");
formDataContent.Add(new StreamContent(new MemoryStream(await File.ReadAllBytesAsync(options.Code))), "Code", "code.dll");

using var response = await client.PostAsync(
url,
Expand All @@ -49,7 +49,7 @@ public async Task DeployAsync(DeployAppOptions options)
await _remoteServiceExceptionHandler.EnsureSuccessfulHttpResponseAsync(response);

var responseContent = await response.Content.ReadAsStringAsync();
_logger.LogInformation("Deploy app successfully. Version: {version}", responseContent);
_logger.LogInformation("Deploy app successfully. Version: {Version}", responseContent);
}

public async Task UpdateAsync(UpdateAppOptions options)
Expand Down Expand Up @@ -77,7 +77,7 @@ private async Task UpdateCodeAsync(UpdateAppOptions options, string token)
var client = _cliHttpClientFactory.CreateClient(token);

var formDataContent = new MultipartFormDataContent();
formDataContent.Add(new StreamContent(new MemoryStream(File.ReadAllBytes(options.Code))), "Code", "code.dll");
formDataContent.Add(new StreamContent(new MemoryStream(await File.ReadAllBytesAsync(options.Code))), "Code", "code.dll");

using var response = await client.PutAsync(
url,
Expand All @@ -101,7 +101,7 @@ private async Task UpdateManifestAsync(UpdateAppOptions options, string token)

using var response = await client.PutAsync(
url,
new StringContent(File.ReadAllText(options.Manifest), Encoding.UTF8, MimeTypes.Application.Json),
new StringContent(await File.ReadAllTextAsync(options.Manifest), Encoding.UTF8, MimeTypes.Application.Json),
_cancellationTokenProvider.Token
);

Expand Down
14 changes: 10 additions & 4 deletions src/AeFinder.Cli.Core/Services/DevelopmentTemplateAppService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class DevelopmentTemplateAppService : IDevelopmentTemplateAppService, ITr
private readonly ICancellationTokenProvider _cancellationTokenProvider;
private readonly IRemoteServiceExceptionHandler _remoteServiceExceptionHandler;
private readonly ILogger<DevelopmentTemplateAppService> _logger;

public DevelopmentTemplateAppService(IAuthService authService, IJsonSerializer jsonSerializer,
CliHttpClientFactory cliHttpClientFactory, ICancellationTokenProvider cancellationTokenProvider,
ILogger<DevelopmentTemplateAppService> logger, IRemoteServiceExceptionHandler remoteServiceExceptionHandler)
Expand Down Expand Up @@ -66,19 +66,25 @@ public async Task CreateProjectAsync(InitAppOptions options)
var result = await response.Content.ReadAsStreamAsync();
ZipHelper.UnZip(result, options.Directory);

_logger.LogInformation("The AeFinder App: {app} is initialized successfully. Directory: {directory}", options.Name, options.Directory);
_logger.LogInformation("The AeFinder App: {App} is initialized successfully. Directory: {Directory}", options.Name, options.Directory);
}

private void CheckOptions(InitAppOptions options)
private static void CheckOptions(InitAppOptions options)
{
if (options.Name.Length is < 2 or > 20)
{
throw new UserFriendlyException("The name should be between 2 and 20 in length.");
}

if (!Regex.IsMatch(options.Name, "[A-Za-z][A-Za-z0-9.]+"))
if (!ProjectNameRegex.IsValid(options.Name))
{
throw new UserFriendlyException("The Name must begin with a letter and can only contain letters('A'-'Z', 'a'-'z'), numbers(0-9), and dots('.').");
}
}
}

public static partial class ProjectNameRegex {
[GeneratedRegex("^[A-Za-z][A-Za-z0-9.]+")]
public static partial Regex Regex();
public static bool IsValid(string name) => Regex().IsMatch(name);
}
2 changes: 1 addition & 1 deletion src/AeFinder.Cli.Core/Utils/ZipHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace AeFinder.Cli.Utils;

public class ZipHelper
public static class ZipHelper
{
public static void ZipDirectory(string zipFileName, string sourceDirectory)
{
Expand Down
4 changes: 2 additions & 2 deletions src/AeFinder.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace AeFinder.Cli;

public class Program
public static class Program
{
private static async Task Main(string[] args)
{
Expand All @@ -27,7 +27,7 @@ private static async Task Main(string[] args)
.WriteTo.Console(theme: AnsiConsoleTheme.Sixteen)
.CreateLogger();

using var application = AbpApplicationFactory.Create<AeFinderCliModule>(
using var application = await AbpApplicationFactory.CreateAsync<AeFinderCliModule>(
options =>
{
options.UseAutofac();
Expand Down

0 comments on commit 35fec97

Please sign in to comment.