|
@@ -1,75 +1,67 @@
|
|
|
-//using System.Net.Http.Json;
|
|
|
-//using System.Text.Json;
|
|
|
+using System.Net.Http.Json;
|
|
|
+using System.Text.Json;
|
|
|
+using RestSharp;
|
|
|
+using RestSharp.Authenticators;
|
|
|
|
|
|
-//namespace Tr.Sdk;
|
|
|
+namespace Tr.Sdk;
|
|
|
|
|
|
-//public partial class TrClient : ITrClient
|
|
|
-//{
|
|
|
-// private readonly IHttpClientFactory _httpClientFactory;
|
|
|
+public partial class TrClient : ITrClient, IDisposable
|
|
|
+{
|
|
|
+ private readonly RestClient _client;
|
|
|
|
|
|
-// public TrClient(IHttpClientFactory httpClientFactory)
|
|
|
-// {
|
|
|
-// _httpClientFactory = httpClientFactory;
|
|
|
-// }
|
|
|
+ public TrClient(string baseUrl, string apiKey, string apiSecret)
|
|
|
+ {
|
|
|
+ var options = new RestClientOptions(baseUrl)
|
|
|
+ {
|
|
|
+ Authenticator = new TrAuthenticator()
|
|
|
+ };
|
|
|
+ _client = new RestClient(options);
|
|
|
+ }
|
|
|
|
|
|
-// /// <summary>
|
|
|
-// /// 执行操作呼叫中心请求
|
|
|
-// /// </summary>
|
|
|
-// /// <typeparam name="TRequest"></typeparam>
|
|
|
-// /// <typeparam name="TResponse"></typeparam>
|
|
|
-// /// <param name="request"></param>
|
|
|
-// /// <param name="cancellationToken"></param>
|
|
|
-// /// <returns></returns>
|
|
|
-// public async Task<TResponse?> ExecuteAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken) where TRequest : ITrRequest
|
|
|
-// {
|
|
|
-// //var httpClient = _httpClientFactory.CreateClient(Defaults.ServiceName);
|
|
|
-// //var token = await _tokenManager.GetTokenAsync(cancellationToken);
|
|
|
-// //httpClient.DefaultRequestHeaders.Add("X-Auth-Token", token);
|
|
|
-// //try
|
|
|
-// //{
|
|
|
-// // if (request.Method == HttpMethod.Get)
|
|
|
-// // {
|
|
|
-// // return await httpClient.GetFromJsonAsync<TResponse>(request.Url, cancellationToken);
|
|
|
-// // }
|
|
|
-// // else if (request.Method == HttpMethod.Post)
|
|
|
-// // {
|
|
|
-// // using var content = new FormUrlEncodedContent
|
|
|
-// // await httpClient.PostAsync(request.Url, new FormUrlEncodedContent(), cancellationToken);
|
|
|
-// // }
|
|
|
-// // else
|
|
|
-// // {
|
|
|
-// // throw new ArgumentOutOfRangeException();
|
|
|
-// // }
|
|
|
+ /// <summary>
|
|
|
+ /// 执行操作呼叫中心请求
|
|
|
+ /// </summary>
|
|
|
+ /// <typeparam name="TRequest"></typeparam>
|
|
|
+ /// <typeparam name="TResponse"></typeparam>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <param name="cancellationToken"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public async Task<TResponse?> ExecuteAsync<TRequest, TResponse>(TRequest request, CancellationToken cancellationToken)
|
|
|
+ where TRequest : TrRequest
|
|
|
+ {
|
|
|
+ var req = new RestRequest(request.Path(), request.Method())
|
|
|
+ .AddObject(request);
|
|
|
|
|
|
-// // var rsp = await httpClient.PostAsJsonAsync(request.Url, request, cancellationToken: cancellationToken);
|
|
|
-// // if (!rsp.IsSuccessStatusCode)
|
|
|
-// // throw new HttpRequestException("请求呼叫中心服务失败");
|
|
|
-// // var response = await rsp.Content.ReadFromJsonAsync<TResponse>(new JsonSerializerOptions
|
|
|
-// // {
|
|
|
-// // PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
|
-// // }, cancellationToken);
|
|
|
-// // if (response?.Code == 401)
|
|
|
-// // {
|
|
|
-// // await _tokenManager.RefreshTokenAsync(cancellationToken);
|
|
|
-// // response = await rsp.Content.ReadFromJsonAsync<TResponse>(new JsonSerializerOptions
|
|
|
-// // {
|
|
|
-// // PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
|
|
-// // }, cancellationToken);
|
|
|
-// // }
|
|
|
-// // if (response?.Code != 200)
|
|
|
-// // throw new HttpRequestException(response?.Msg);
|
|
|
-// // return response;
|
|
|
-// //}
|
|
|
-// //catch (Exception)
|
|
|
-// //{
|
|
|
-// // throw;
|
|
|
-// //}
|
|
|
+ var response = await _client.ExecuteAsync<TResponse>(req, cancellationToken);
|
|
|
+ if (!response.IsSuccessful)
|
|
|
+ {
|
|
|
+ throw new HttpRequestException($"请求呼叫中心服务失败, HttpCode: {response.StatusCode}, Error: {response.ErrorMessage}");
|
|
|
+ }
|
|
|
+ return response.Data;
|
|
|
+ }
|
|
|
|
|
|
-// throw new NotImplementedException();
|
|
|
-// }
|
|
|
+ public async Task<string> GetTokenAsync(TokenRequest request, CancellationToken cancellationToken)
|
|
|
+ {
|
|
|
+ throw new NotImplementedException();
|
|
|
+ }
|
|
|
|
|
|
-// public async Task<string> GetTokenAsync(TokenRequest request, CancellationToken cancellationToken)
|
|
|
-// {
|
|
|
-// throw new NotImplementedException();
|
|
|
-// }
|
|
|
-//}
|
|
|
+ /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
|
|
|
+ public void Dispose()
|
|
|
+ {
|
|
|
+ _client?.Dispose();
|
|
|
+ GC.SuppressFinalize(this);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+public class TrAuthenticator : AuthenticatorBase
|
|
|
+{
|
|
|
+ public TrAuthenticator() : base("")
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ protected override async ValueTask<Parameter> GetAuthenticationParameter(string accessToken)
|
|
|
+ {
|
|
|
+ Token = string.IsNullOrEmpty(Token) ? "8352d42165d7cf6821e53e926d66adba54c309e6" : Token;
|
|
|
+ return new HeaderParameter("X-Auth-Token", Token);
|
|
|
+ }
|
|
|
+}
|