|
@@ -8,9 +8,15 @@ namespace Tr.Sdk;
|
|
|
public class TrClient : ITrClient, IDisposable
|
|
|
{
|
|
|
private readonly RestClient _client;
|
|
|
+ private readonly string _baseUrl;
|
|
|
+ private readonly string _apiKey;
|
|
|
+ private readonly string _apiSecret;
|
|
|
|
|
|
public TrClient(string baseUrl, string apiKey, string apiSecret)
|
|
|
{
|
|
|
+ _baseUrl = baseUrl;
|
|
|
+ _apiKey = apiKey;
|
|
|
+ _apiSecret = apiSecret;
|
|
|
var options = new RestClientOptions(baseUrl)
|
|
|
{
|
|
|
Authenticator = new TrAuthenticator(baseUrl, apiKey, apiSecret)
|
|
@@ -36,7 +42,23 @@ public class TrClient : ITrClient, IDisposable
|
|
|
{
|
|
|
var response = await _client.ExecuteAsync<TResponse>(req, cancellationToken);
|
|
|
if (!response?.IsSuccessful ?? false)
|
|
|
+ {
|
|
|
+ if(response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
|
|
|
+ {
|
|
|
+ //TODO 清除Token
|
|
|
+ var options = new RestClientOptions(_baseUrl)
|
|
|
+ {
|
|
|
+ Authenticator = new TrAuthenticator(_baseUrl, _apiKey, _apiSecret)
|
|
|
+ };
|
|
|
+ RestClient _newClient = new RestClient(options);
|
|
|
+ response = await _newClient.ExecuteAsync<TResponse>(req, cancellationToken);
|
|
|
+ if (!response?.IsSuccessful ?? false)
|
|
|
+ throw new HttpRequestException($"请求呼叫中心服务失败, HttpCode: {response.StatusCode}, Error: {response.ErrorMessage}");
|
|
|
+
|
|
|
+ return response.Data;
|
|
|
+ }
|
|
|
throw new HttpRequestException($"请求呼叫中心服务失败, HttpCode: {response.StatusCode}, Error: {response.ErrorMessage}");
|
|
|
+ }
|
|
|
|
|
|
return response.Data;
|
|
|
}
|