-
Notifications
You must be signed in to change notification settings - Fork 33
[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
Draft
Ayyanchira
wants to merge
1
commit into
master
Choose a base branch
from
feature/auto-retry
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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? |
||
| 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(); | ||
| } 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; | ||
| } | ||
|
|
||
| // 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(); | ||
| 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); | ||
| } 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 = ""; | ||
| } | ||
| JSONObject json = jsonData.getJSONObject("data"); | ||
| return new IterableApiRequest(apikey, resourcePath, json, requestType, authToken, onSuccess, onFailure); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?