-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from Tynab/develop
Develop
- Loading branch information
Showing
24 changed files
with
299 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
src/YANLib.Application.Contracts/RemoteService/IRemoteService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using RestSharp; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Volo.Abp.Application.Services; | ||
|
||
namespace YANLib.RemoteService; | ||
|
||
public interface IRemoteService : IApplicationService | ||
{ | ||
public ValueTask<T> InvokeApi<T>(string remoteRoot, string path, Method method, Dictionary<string, string> headers = null, string jsonInput = null, Dictionary<string, object> queryParams = null); | ||
} |
12 changes: 12 additions & 0 deletions
12
src/YANLib.Application.Contracts/Requests/EcommerceLoginRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.ComponentModel; | ||
|
||
namespace YANLib.Requests; | ||
|
||
public sealed class EcommerceLoginRequest | ||
{ | ||
[DefaultValue("[email protected]")] | ||
public required string Username { get; set; } | ||
|
||
[DefaultValue("nguyenvana")] | ||
public required string Password { get; set; } | ||
} |
12 changes: 12 additions & 0 deletions
12
src/YANLib.Application.Contracts/Services/IEcommerceService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Threading.Tasks; | ||
using Volo.Abp.Application.Services; | ||
using YANLib.Requests; | ||
|
||
namespace YANLib.Services; | ||
|
||
public interface IEcommerceService : IApplicationService | ||
{ | ||
public ValueTask<string> GetAccessToken(EcommerceLoginRequest request); | ||
|
||
public ValueTask<string> GetRefreshToken(string accessToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using NUglify.Helpers; | ||
using RestSharp; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Volo.Abp.Http; | ||
using Volo.Abp.Http.Client; | ||
using YANLib.Core; | ||
using static Newtonsoft.Json.Linq.JObject; | ||
using static RestSharp.ParameterType; | ||
using static System.Net.HttpStatusCode; | ||
using static YANLib.YANLibDomainErrorCodes; | ||
|
||
namespace YANLib.RemoteService; | ||
|
||
public class RemoteService( | ||
ILogger<RemoteService> logger, | ||
IOptionsSnapshot<AbpRemoteServiceOptions> remoteServiceOptions | ||
) : YANLibAppService, IRemoteService | ||
{ | ||
private readonly ILogger<RemoteService> _logger = logger; | ||
private readonly AbpRemoteServiceOptions _remoteServiceOptions = remoteServiceOptions.Value; | ||
|
||
public async ValueTask<T> InvokeApi<T>(string remoteRoot, string path, Method method, Dictionary<string, string> headers = null, string jsonInput = null, Dictionary<string, object> queryParams = null) | ||
{ | ||
try | ||
{ | ||
var req = new RestRequest(path, method); | ||
|
||
if (headers.IsNotEmptyAndNull()) | ||
{ | ||
headers.ForEach(x => req.AddHeader(x.Key, x.Value)); | ||
} | ||
else | ||
{ | ||
_ = req.AddHeader("Accept", "*/*"); | ||
_ = req.AddHeader("Content-Type", "application/json"); | ||
} | ||
|
||
if (jsonInput.IsNotWhiteSpaceAndNull()) | ||
{ | ||
_ = req.AddParameter("application/json", jsonInput, RequestBody); | ||
} | ||
|
||
if (queryParams.IsNotEmptyAndNull()) | ||
{ | ||
queryParams.ForEach(x => req.AddParameter(x.Key, x.Value, QueryString)); | ||
} | ||
|
||
var res = await new RestClient(_remoteServiceOptions.RemoteServices.GetConfigurationOrDefaultOrNull(remoteRoot)?.BaseUrl)?.ExecuteAsync(req); | ||
|
||
if (res.StatusCode is OK) | ||
{ | ||
return res.Content.Deserialize<T>(); | ||
} | ||
else | ||
{ | ||
_logger.LogError("Invoke API: {PathRoot} - {Code} - {Error} - {Content}", $"{remoteRoot}{path}", res.StatusCode, res.ErrorMessage, res.Content); | ||
|
||
if (res.Content.IsWhiteSpaceOrNull()) | ||
{ | ||
throw new AbpRemoteCallException(new RemoteServiceErrorInfo | ||
{ | ||
Code = NOT_FOUND, | ||
Message = res.ErrorException.Message | ||
}) | ||
{ | ||
HttpStatusCode = res.StatusCode.ToInt() | ||
}; | ||
} | ||
else | ||
{ | ||
var jtoken = Parse(res.Content)["error"]?.ToString(); | ||
|
||
throw new AbpRemoteCallException(jtoken?.Deserialize<RemoteServiceErrorInfo>() ?? new RemoteServiceErrorInfo | ||
{ | ||
Message = jtoken | ||
}) | ||
{ | ||
HttpStatusCode = res.StatusCode.ToInt() | ||
}; | ||
} | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "InvokeApiRemoteService-Exception: {PathRoot} - {Method} - {JsonInput} - {QueryParams}", $"{remoteRoot}{path}", method.ToString(), jsonInput, queryParams.Serialize()); | ||
|
||
throw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using YANLib.Core; | ||
using YANLib.RemoteService; | ||
using YANLib.Requests; | ||
using static RestSharp.Method; | ||
using static YANLib.YANLibConsts.RemoteService; | ||
using static YANLib.YANLibConsts.RemoteService.Path; | ||
|
||
namespace YANLib.Services; | ||
|
||
public class EcommerceService( | ||
ILogger<EcommerceService> logger, | ||
IRemoteService remoteService | ||
) : YANLibAppService, IEcommerceService | ||
{ | ||
private readonly ILogger<EcommerceService> _logger = logger; | ||
private readonly IRemoteService _remoteService = remoteService; | ||
|
||
public async ValueTask<string> GetAccessToken(EcommerceLoginRequest request) | ||
{ | ||
var json = request.Serialize(); | ||
|
||
try | ||
{ | ||
var hdrs = new Dictionary<string, string> | ||
{ | ||
{ "Accept", "*/*" }, | ||
{ "Content-Type", "application/x-www-form-urlencoded" } | ||
}; | ||
return await _remoteService.InvokeApi<string>(EcommerceApi, Login, Post, jsonInput: json); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "GetAccessTokenEcommerceService-Exception: {Request}", json); | ||
|
||
throw; | ||
} | ||
} | ||
|
||
public async ValueTask<string> GetRefreshToken(string accessToken) | ||
{ | ||
try | ||
{ | ||
return await _remoteService.InvokeApi<string>(EcommerceApi, Refresh, Get, new Dictionary<string, string> | ||
{ | ||
{ "Authorization", $"Bearer {accessToken}" } | ||
}); | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogError(ex, "GetRefreshTokenEcommerceService-Exception: {AccessToken}", accessToken); | ||
|
||
throw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using FluentValidation; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using YANLib.Core; | ||
using YANLib.Requests; | ||
using static YANLib.YANLibDomainErrorCodes; | ||
|
||
namespace YANLib.Validations; | ||
|
||
public sealed class EcommerceValidator : AbstractValidator<EcommerceLoginRequest> | ||
{ | ||
public EcommerceValidator() | ||
{ | ||
_ = RuleFor(x => x.Username).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST_USER_NAME).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_USER_NAME); | ||
_ = RuleFor(x => x.Password).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST_PWD).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_PWD); | ||
} | ||
} | ||
|
||
public sealed class EcommerceValidators : AbstractValidator<List<EcommerceLoginRequest>> | ||
{ | ||
public EcommerceValidators() | ||
{ | ||
_ = RuleFor(x => x).NotNull().NotEmpty().WithErrorCode(BAD_REQUEST).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST); | ||
_ = RuleForEach(s => s).SetValidator(new EcommerceValidator()); | ||
_ = RuleFor(x => x).Must(IsNotEmptyAndNull).WithErrorCode(BAD_REQUEST).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST); | ||
_ = RuleFor(x => x).Must(UsernameIsNotWhiteSpace).WithErrorCode(BAD_REQUEST_USER_NAME).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_USER_NAME); | ||
_ = RuleFor(x => x).Must(PasswordIsNotWhiteSpace).WithErrorCode(BAD_REQUEST_PWD).WithMessage(YANLibDomainErrorMessages.BAD_REQUEST_PWD); | ||
} | ||
|
||
private bool IsNotEmptyAndNull(List<EcommerceLoginRequest> requests) => requests.IsNotEmptyAndNull(); | ||
|
||
private bool UsernameIsNotWhiteSpace(List<EcommerceLoginRequest> requests) => requests.Select(x => x.Username).AllNotWhiteSpaceAndNull(); | ||
|
||
private bool PasswordIsNotWhiteSpace(List<EcommerceLoginRequest> requests) => requests.Select(x => x.Password).AllNotWhiteSpaceAndNull(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.