Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New] Token Refresh Logic #110

Merged
merged 6 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 27 additions & 19 deletions Conductor/Client/ApiClient.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Conductor.Client.Models;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
Expand All @@ -6,6 +8,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
Expand Down Expand Up @@ -165,44 +168,49 @@ public Object CallApi(
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType, Configuration configuration = null)
{
int retryCount = 0;
RestResponse response = RetryRestClientCallApi(path, method, queryParams, postBody, headerParams,
formParams, fileParams, pathParams, contentType, configuration, ref retryCount);

return (Object)response;
}

private RestResponse RetryRestClientCallApi(String path, Method method, List<KeyValuePair<String, String>> queryParams, Object postBody,
Dictionary<String, String> headerParams, Dictionary<String, String> formParams,
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType, Configuration configuration, ref int retryCount)
{
RestResponse response = null;
bool isTokenRefreshed = false;
do
while (retryCount < Constants.MAX_TOKEN_REFRESH_RETRY_COUNT)
{
bool isTokenRefreshed = false; // reset refreshStatusflag

var request = PrepareRequest(
path, method, queryParams, postBody, headerParams, formParams, fileParams,
pathParams, contentType);

response = ExecuteRestClientAPI(request, method);
InterceptRequest(request);
response = RestClient.Execute(request, method);
InterceptResponse(request, response);
FormatHeaders(response);

if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
{
var JsonContent = JsonConvert.DeserializeObject<JObject>(response.Content);

if (JsonContent["error"].ToString() == "EXPIRED_TOKEN" && !isTokenRefreshed)
if (JsonContent["error"].ToString() == "EXPIRED_TOKEN" && retryCount < Constants.MAX_TOKEN_REFRESH_RETRY_COUNT)
{
string refreshToken = configuration.GetRefreshToken();
headerParams["X-Authorization"] = refreshToken;
isTokenRefreshed = true;
retryCount++;
}
else
isTokenRefreshed = false;
}
else
isTokenRefreshed = false;
} while (isTokenRefreshed);

return (Object)response;
}

private RestResponse ExecuteRestClientAPI(RestRequest request, Method method)
{
InterceptRequest(request);
var response = RestClient.Execute(request, method);
InterceptResponse(request, response);
FormatHeaders(response);

if (!isTokenRefreshed)
Jithesh-poojary marked this conversation as resolved.
Show resolved Hide resolved
break;
}
return response;
}

Expand Down
7 changes: 4 additions & 3 deletions Conductor/Client/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
/// </summary>
public static class Constants
{
public const string KEY_ID = "<REPLACE_WITH_KEY_ID>";
public const string KEY_SECRET = "<REPLACE_WITH_KEY_SECRET>";
public const string OWNER_EMAIL = "<REPLACE_WITH_OWNER_EMAIL>";
public const string KEY_ID = "2aa73e29-de29-479c-8b6d-5b45aa48aa4a";
public const string KEY_SECRET = "wygpIUK34iroLawafQzH3jgrbbMGUQvdeh46xicPvz9f6Kiq";
public const string OWNER_EMAIL = "[email protected]";
public const int REST_CLIENT_REQUEST_TIME_OUT = 20000;
public const int MAX_TOKEN_REFRESH_RETRY_COUNT = 3;

}
}
Loading