|
@@ -14,16 +14,16 @@ using XF.Domain.Dependency;
|
|
|
|
|
|
namespace Hotline.YbEnterprise.Sdk
|
|
|
{
|
|
|
- public class EnterpriseService : IEnterpriseService, ITransientDependency
|
|
|
+ public class EnterpriseService : IEnterpriseService, IScopeDependency
|
|
|
{
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
private readonly IOptionsSnapshot<EnterpriseConfig> _enterpriseOptions;
|
|
|
- private readonly ITypedCache<EnterpriseToken> _tokenCache;
|
|
|
+ private readonly ITypedCache<YbEnterpriseToken> _tokenCache;
|
|
|
private readonly ILogger<EnterpriseService> _logger;
|
|
|
public EnterpriseService(
|
|
|
IHttpClientFactory httpClientFactory,
|
|
|
IOptionsSnapshot<EnterpriseConfig> enterpriseOptions,
|
|
|
- ITypedCache<EnterpriseToken> tokenCache,
|
|
|
+ ITypedCache<YbEnterpriseToken> tokenCache,
|
|
|
ILogger<EnterpriseService> logger)
|
|
|
{
|
|
|
_httpClientFactory = httpClientFactory;
|
|
@@ -40,10 +40,10 @@ namespace Hotline.YbEnterprise.Sdk
|
|
|
/// <param name="size"></param>
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
/// <returns></returns>
|
|
|
- public async Task<EnterpriseListData> GetEnterprisesAsync(
|
|
|
+ public async Task<YbEnterprisePaged> QueryYbEnterprisesAsync(
|
|
|
string name, int current, int size, CancellationToken cancellationToken)
|
|
|
{
|
|
|
- var request = new EnterpriseListRequest
|
|
|
+ var request = new YbEnterprisesRequest()
|
|
|
{
|
|
|
EnterpriseName = name,
|
|
|
Current = current,
|
|
@@ -51,17 +51,17 @@ namespace Hotline.YbEnterprise.Sdk
|
|
|
};
|
|
|
|
|
|
var token = await GetTokenAsync(YbEnterpriseDefaults.KeyOfToken, cancellationToken);
|
|
|
- var result = await ExecuteAsync<EnterpriseListRequest, EnterpriseListResponse>(
|
|
|
+ var result = await ExecuteAsync<YbEnterprisesRequest, YbEnterpriseResponse<YbEnterprisePaged>>(
|
|
|
YbEnterpriseDefaults.PathEnterprises,
|
|
|
request,
|
|
|
d => d.DefaultRequestHeaders.Add("Blade-Auth", $"{token.TokenType} {token.AccessToken}"),
|
|
|
cancellationToken);
|
|
|
- if (result is null || !result.success)
|
|
|
+ if (result is null || !result.Success)
|
|
|
throw new UserFriendlyException("未获取到企业专班数据");
|
|
|
- return result.data;
|
|
|
+ return result.Data;
|
|
|
}
|
|
|
|
|
|
- public async Task<EnterpriseToken> GetTokenAsync(string key, CancellationToken cancellationToken)
|
|
|
+ private async Task<YbEnterpriseToken> GetTokenAsync(string key, CancellationToken cancellationToken)
|
|
|
{
|
|
|
var token = await _tokenCache.GetAsync(key, cancellationToken);
|
|
|
if (token is not null) return token;
|
|
@@ -70,13 +70,12 @@ namespace Hotline.YbEnterprise.Sdk
|
|
|
return token;
|
|
|
}
|
|
|
|
|
|
- public async Task<EnterpriseToken> GetTokenAsync(CancellationToken cancellationToken)
|
|
|
+ private async Task<YbEnterpriseToken> GetTokenAsync(CancellationToken cancellationToken)
|
|
|
{
|
|
|
- //var path = "blade-auth/oauth/getAccessToken";
|
|
|
var config = _enterpriseOptions.Value;
|
|
|
string authorization = config.ClientId + ":" + config.ClientSecret;
|
|
|
authorization = Convert.ToBase64String(Encoding.UTF8.GetBytes(authorization));
|
|
|
- var result = await ExecuteAsync<object, EnterpriseTokenResponse>(
|
|
|
+ var result = await ExecuteAsync<object, YbEnterpriseResponse<YbEnterpriseToken>>(
|
|
|
YbEnterpriseDefaults.PathToken,
|
|
|
new(),
|
|
|
d =>
|
|
@@ -85,12 +84,12 @@ namespace Hotline.YbEnterprise.Sdk
|
|
|
d.DefaultRequestHeaders.Add("Tenant-Id", config.TenantId);
|
|
|
}, cancellationToken);
|
|
|
|
|
|
- if (result is null || !result.success)
|
|
|
+ if (result is null || !result.Success)
|
|
|
throw new UserFriendlyException("获取enterprise token失败");
|
|
|
- return result.data;
|
|
|
+ return result.Data;
|
|
|
}
|
|
|
|
|
|
- public async Task<TResponse?> ExecuteAsync<TRequest, TResponse>(string path, TRequest request,
|
|
|
+ private async Task<TResponse?> ExecuteAsync<TRequest, TResponse>(string path, TRequest request,
|
|
|
Action<HttpClient>? clientInitialize = null, CancellationToken cancellationToken = default)
|
|
|
{
|
|
|
var client = _httpClientFactory.CreateClient(YbEnterpriseDefaults.HttpName);
|
|
@@ -98,8 +97,6 @@ namespace Hotline.YbEnterprise.Sdk
|
|
|
|
|
|
using var responseMessage = await client.PostAsJsonAsync(path, request, cancellationToken);
|
|
|
responseMessage.EnsureSuccessStatusCode();
|
|
|
- var s = await responseMessage.Content.ReadAsStringAsync(cancellationToken);
|
|
|
- _logger.LogInformation(s);
|
|
|
var result = await responseMessage.Content.ReadFromJsonAsync<TResponse>(cancellationToken: cancellationToken);
|
|
|
return result;
|
|
|
}
|