Browse Source

Merge branch 'master' of http://git.12345lm.cn/Fengwo/hotline

Dun.Jason 1 year ago
parent
commit
c6ee35b551
1 changed files with 13 additions and 16 deletions
  1. 13 16
      src/Tr.Sdk/TrClient.cs

+ 13 - 16
src/Tr.Sdk/TrClient.cs

@@ -7,7 +7,7 @@ namespace Tr.Sdk;
 
 public class TrClient : ITrClient, IDisposable
 {
-    private readonly RestClient _client;
+    private RestClient _client;
     private readonly string _baseUrl;
     private readonly string _apiKey;
     private readonly string _apiSecret;
@@ -22,7 +22,7 @@ public class TrClient : ITrClient, IDisposable
             Authenticator = new TrAuthenticator(baseUrl, apiKey, apiSecret)
         };
         _client = new RestClient(options);
-       
+
     }
 
     /// <summary>
@@ -41,25 +41,22 @@ public class TrClient : ITrClient, IDisposable
         try
         {
             var response = await _client.ExecuteAsync<TResponse>(req, cancellationToken);
-            if (!response?.IsSuccessful ?? false)
+
+            if (response?.StatusCode == System.Net.HttpStatusCode.Unauthorized)
             {
-                if(response.StatusCode ==  System.Net.HttpStatusCode.Unauthorized)
+                _client.Dispose();
+                var options = new RestClientOptions(_baseUrl)
                 {
-                    //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}");
+                    Authenticator = new TrAuthenticator(_baseUrl, _apiKey, _apiSecret)
+                };
+                _client = new RestClient(options);
 
-                    return response.Data;
-                }
-                throw new HttpRequestException($"请求呼叫中心服务失败, HttpCode: {response.StatusCode}, Error: {response.ErrorMessage}");
+                response = await _client.ExecuteAsync<TResponse>(req, cancellationToken);
             }
 
+            if (!response?.IsSuccessful ?? false)
+                throw new HttpRequestException($"请求呼叫中心服务失败, HttpCode: {response.StatusCode}, Error: {response.ErrorMessage}");
+
             return response.Data;
         }
         catch (Exception e)