-
Notifications
You must be signed in to change notification settings - Fork 34
[SDK-342] - AuthRetry logic #987
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
base: autoretry-master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| import java.io.BufferedReader; | ||
| import java.io.BufferedWriter; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.InputStreamReader; | ||
| import java.io.OutputStream; | ||
| import java.io.OutputStreamWriter; | ||
|
|
@@ -153,20 +154,27 @@ static IterableApiResponse executeApiRequest(IterableApiRequest iterableApiReque | |
| // Read the response body | ||
| try { | ||
| BufferedReader in; | ||
| if (responseCode < 400) { | ||
| if (responseCode >= 0 && responseCode < 400) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a 0 code that can occur?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. In testing I received -1. Hence thought its a good idea to keep 0 - 300 range. |
||
| in = new BufferedReader( | ||
| new InputStreamReader(urlConnection.getInputStream())); | ||
| } else { | ||
| in = new BufferedReader( | ||
| new InputStreamReader(urlConnection.getErrorStream())); | ||
| InputStream errorStream = urlConnection.getErrorStream(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't it possible to extract just this flow of getting the resultResult to a helper class? this part is quite verbose |
||
| if (errorStream != null) { | ||
| in = new BufferedReader( | ||
| new InputStreamReader(errorStream)); | ||
| } else { | ||
| in = null; | ||
| } | ||
| } | ||
| String inputLine; | ||
| StringBuffer response = new StringBuffer(); | ||
| while ((inputLine = in.readLine()) != null) { | ||
| response.append(inputLine); | ||
| if (in != null) { | ||
| String inputLine; | ||
| StringBuffer response = new StringBuffer(); | ||
| while ((inputLine = in.readLine()) != null) { | ||
| response.append(inputLine); | ||
| } | ||
| in.close(); | ||
| requestResult = response.toString(); | ||
| } | ||
| in.close(); | ||
| requestResult = response.toString(); | ||
|
Comment on lines
-160
to
-169
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this change is not required at all |
||
| } catch (IOException e) { | ||
| logError(iterableApiRequest, baseUrl, e); | ||
| error = e.getMessage(); | ||
|
|
@@ -186,13 +194,36 @@ static IterableApiResponse executeApiRequest(IterableApiRequest iterableApiReque | |
| jsonError = e.getMessage(); | ||
| } | ||
|
|
||
| // If getResponseCode() returned -1 (e.g. due to network inspector | ||
| // interference) but the response body contains JWT error codes, | ||
| // we can infer the actual response was a 401. | ||
| if (responseCode == -1 && matchesJWTErrorCodes(jsonResponse)) { | ||
| responseCode = 401; | ||
| } | ||
|
Comment on lines
+197
to
+202
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -1's can be discarded. Dont have to categorize into 401 flow. |
||
|
|
||
| // Handle HTTP status codes | ||
| if (responseCode == 401) { | ||
| if (matchesJWTErrorCodes(jsonResponse)) { | ||
| apiResponse = IterableApiResponse.failure(responseCode, requestResult, jsonResponse, "JWT Authorization header error"); | ||
| IterableApi.getInstance().getAuthManager().handleAuthFailure(iterableApiRequest.authToken, getMappedErrorCodeForMessage(jsonResponse)); | ||
| // We handle the JWT Retry for both online and offline here rather than handling online request in onPostExecute | ||
| requestNewAuthTokenAndRetry(iterableApiRequest); | ||
|
|
||
| // [F] When autoRetry is enabled and this is an offline task, skip the inline | ||
| // retry. The task stays in the DB and IterableTaskRunner will retry it once | ||
| // a valid JWT is obtained via the AuthTokenReadyListener callback. | ||
| // For online requests or when autoRetry is disabled, use the existing inline retry. | ||
| boolean autoRetry = IterableApi.getInstance().isAutoRetryOnJwtFailure(); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also thinking, it should not be a public variable that should be accessible for developers like this. Can cause confusion. |
||
| if (autoRetry && iterableApiRequest.getProcessorType() == IterableApiRequest.ProcessorType.OFFLINE) { | ||
| // Schedule a delayed token refresh (respects retry policy). | ||
| // Do NOT retry the request inline -- IterableTaskRunner will handle | ||
| // the retry after the AuthTokenReadyListener callback fires. | ||
| IterableAuthManager authManager = IterableApi.getInstance().getAuthManager(); | ||
| authManager.setIsLastAuthTokenValid(false); | ||
| long retryInterval = authManager.getNextRetryInterval(); | ||
| authManager.scheduleAuthTokenRefresh(retryInterval, false, null); | ||
|
Comment on lines
+219
to
+222
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This 4 line can be combined into one if authmanager has |
||
| } else { | ||
| // Existing behavior: retry request inline after obtaining new token | ||
| requestNewAuthTokenAndRetry(iterableApiRequest); | ||
| } | ||
| } else { | ||
| apiResponse = IterableApiResponse.failure(responseCode, requestResult, jsonResponse, "Invalid API Key"); | ||
| } | ||
|
|
@@ -498,13 +529,27 @@ public JSONObject toJSONObject() throws JSONException { | |
| } | ||
|
|
||
| static IterableApiRequest fromJSON(JSONObject jsonData, @Nullable IterableHelper.SuccessHandler onSuccess, @Nullable IterableHelper.FailureHandler onFailure) { | ||
| return fromJSON(jsonData, null, onSuccess, onFailure); | ||
| } | ||
|
|
||
| /** | ||
| * Deserializes an IterableApiRequest from JSON. | ||
| * @param authTokenOverride If non-null, uses this token instead of the one stored in JSON. | ||
| * This allows offline tasks to use the latest auth token rather | ||
| * than the stale one captured at queue time. | ||
| */ | ||
| static IterableApiRequest fromJSON(JSONObject jsonData, @Nullable String authTokenOverride, @Nullable IterableHelper.SuccessHandler onSuccess, @Nullable IterableHelper.FailureHandler onFailure) { | ||
| try { | ||
| String apikey = jsonData.getString("apiKey"); | ||
| String resourcePath = jsonData.getString("resourcePath"); | ||
| String requestType = jsonData.getString("requestType"); | ||
| String authToken = ""; | ||
| if (jsonData.has("authToken")) { | ||
| String authToken; | ||
| if (authTokenOverride != null) { | ||
| authToken = authTokenOverride; | ||
| } else if (jsonData.has("authToken")) { | ||
| authToken = jsonData.getString("authToken"); | ||
| } else { | ||
| authToken = ""; | ||
|
Comment on lines
+546
to
+552
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When is authTokenOverride coming in picture? Need to revisit this. |
||
| } | ||
| JSONObject json = jsonData.getJSONObject("data"); | ||
| return new IterableApiRequest(apikey, resourcePath, json, requestType, authToken, onSuccess, onFailure); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a reason for it not to be private or public?