-
Notifications
You must be signed in to change notification settings - Fork 617
Implementing retry once for FIS Client. #895
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c8367dc
Implemneting retry for FIS Client.
ankitaj224 72e335d
Merge branch 'listeners' of github.com:firebase/firebase-android-sdk …
ankitaj224 12ae11d
Simplifying FirebaseInstallations class by adding listeners. (#847)
ankitaj224 560fba1
Addressing ciaran's comments.
ankitaj224 3ac3da2
Merge branch 'fis_sdk' of github.com:firebase/firebase-android-sdk in…
ankitaj224 d9e5c22
Merge conflicts.
ankitaj224 f57137c
Nit fixes
ankitaj224 0c0b9c2
Merge branch 'listeners' of github.com:firebase/firebase-android-sdk …
ankitaj224 0407614
Fixing verify format issue.
ankitaj224 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,9 @@ | |
import com.google.android.gms.common.util.AndroidUtilsLight; | ||
import com.google.android.gms.common.util.Hex; | ||
import com.google.android.gms.common.util.VisibleForTesting; | ||
import com.google.firebase.FirebaseException; | ||
import com.google.firebase.installations.InstallationTokenResult; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
|
@@ -53,8 +55,6 @@ public class FirebaseInstallationServiceClient { | |
private static final String CONTENT_ENCODING_HEADER_KEY = "Content-Encoding"; | ||
private static final String GZIP_CONTENT_ENCODING = "gzip"; | ||
|
||
private static final String UNAUTHORIZED_ERROR_MESSAGE = | ||
"The request did not have the required credentials."; | ||
private static final String INTERNAL_SERVER_ERROR_MESSAGE = "There was an internal server error."; | ||
private static final String NETWORK_ERROR_MESSAGE = "The server returned an unexpected error:"; | ||
|
||
|
@@ -65,6 +65,8 @@ public class FirebaseInstallationServiceClient { | |
|
||
private static final Pattern EXPIRATION_TIMESTAMP_PATTERN = Pattern.compile("[0-9]+s"); | ||
|
||
private static final int MAX_RETRIES = 1; | ||
|
||
@VisibleForTesting | ||
static final String PARSING_EXPIRATION_TIME_ERROR_MESSAGE = "Invalid Expiration Timestamp."; | ||
|
||
|
@@ -86,57 +88,58 @@ public FirebaseInstallationServiceClient(@NonNull Context context) { | |
@NonNull | ||
public InstallationResponse createFirebaseInstallation( | ||
@NonNull String apiKey, @NonNull String fid, @NonNull String projectID, @NonNull String appId) | ||
throws FirebaseInstallationServiceException { | ||
throws FirebaseException { | ||
String resourceName = String.format(CREATE_REQUEST_RESOURCE_NAME_FORMAT, projectID); | ||
try { | ||
URL url = | ||
new URL( | ||
String.format( | ||
"https://%s/%s/%s?key=%s", | ||
FIREBASE_INSTALLATIONS_API_DOMAIN, | ||
FIREBASE_INSTALLATIONS_API_VERSION, | ||
resourceName, | ||
apiKey)); | ||
int retryCount = 0; | ||
while (retryCount <= MAX_RETRIES) { | ||
URL url = | ||
new URL( | ||
String.format( | ||
"https://%s/%s/%s?key=%s", | ||
FIREBASE_INSTALLATIONS_API_DOMAIN, | ||
FIREBASE_INSTALLATIONS_API_VERSION, | ||
resourceName, | ||
apiKey)); | ||
|
||
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); | ||
httpsURLConnection.setConnectTimeout(NETWORK_TIMEOUT_MILLIS); | ||
httpsURLConnection.setReadTimeout(NETWORK_TIMEOUT_MILLIS); | ||
httpsURLConnection.setDoOutput(true); | ||
httpsURLConnection.setRequestMethod("POST"); | ||
httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE); | ||
httpsURLConnection.addRequestProperty(ACCEPT_HEADER_KEY, JSON_CONTENT_TYPE); | ||
httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING); | ||
httpsURLConnection.addRequestProperty(X_ANDROID_PACKAGE_HEADER_KEY, context.getPackageName()); | ||
httpsURLConnection.addRequestProperty( | ||
X_ANDROID_CERT_HEADER_KEY, getFingerprintHashForPackage()); | ||
|
||
GZIPOutputStream gzipOutputStream = | ||
new GZIPOutputStream(httpsURLConnection.getOutputStream()); | ||
try { | ||
gzipOutputStream.write( | ||
buildCreateFirebaseInstallationRequestBody(fid, appId).toString().getBytes("UTF-8")); | ||
} catch (JSONException e) { | ||
throw new IllegalStateException(e); | ||
} finally { | ||
gzipOutputStream.close(); | ||
} | ||
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); | ||
httpsURLConnection.setConnectTimeout(NETWORK_TIMEOUT_MILLIS); | ||
httpsURLConnection.setReadTimeout(NETWORK_TIMEOUT_MILLIS); | ||
httpsURLConnection.setDoOutput(true); | ||
httpsURLConnection.setRequestMethod("POST"); | ||
httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE); | ||
httpsURLConnection.addRequestProperty(ACCEPT_HEADER_KEY, JSON_CONTENT_TYPE); | ||
httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING); | ||
httpsURLConnection.addRequestProperty( | ||
X_ANDROID_PACKAGE_HEADER_KEY, context.getPackageName()); | ||
httpsURLConnection.addRequestProperty( | ||
X_ANDROID_CERT_HEADER_KEY, getFingerprintHashForPackage()); | ||
|
||
int httpResponseCode = httpsURLConnection.getResponseCode(); | ||
switch (httpResponseCode) { | ||
case 200: | ||
return readCreateResponse(httpsURLConnection); | ||
case 401: | ||
throw new FirebaseInstallationServiceException( | ||
UNAUTHORIZED_ERROR_MESSAGE, FirebaseInstallationServiceException.Status.UNAUTHORIZED); | ||
default: | ||
throw new FirebaseInstallationServiceException( | ||
INTERNAL_SERVER_ERROR_MESSAGE, | ||
FirebaseInstallationServiceException.Status.SERVER_ERROR); | ||
GZIPOutputStream gzipOutputStream = | ||
new GZIPOutputStream(httpsURLConnection.getOutputStream()); | ||
try { | ||
gzipOutputStream.write( | ||
buildCreateFirebaseInstallationRequestBody(fid, appId).toString().getBytes("UTF-8")); | ||
} catch (JSONException e) { | ||
throw new IllegalStateException(e); | ||
} finally { | ||
gzipOutputStream.close(); | ||
} | ||
|
||
int httpResponseCode = httpsURLConnection.getResponseCode(); | ||
switch (httpResponseCode) { | ||
case 200: | ||
return readCreateResponse(httpsURLConnection); | ||
case 500: | ||
retryCount++; | ||
break; | ||
default: | ||
throw new FirebaseException(readErrorResponse(httpsURLConnection)); | ||
} | ||
} | ||
throw new FirebaseException(INTERNAL_SERVER_ERROR_MESSAGE); | ||
} catch (IOException e) { | ||
throw new FirebaseInstallationServiceException( | ||
NETWORK_ERROR_MESSAGE + e.getMessage(), | ||
FirebaseInstallationServiceException.Status.NETWORK_ERROR); | ||
throw new FirebaseException(NETWORK_ERROR_MESSAGE + e.getMessage()); | ||
} | ||
} | ||
|
||
|
@@ -163,7 +166,7 @@ public void deleteFirebaseInstallation( | |
@NonNull String fid, | ||
@NonNull String projectID, | ||
@NonNull String refreshToken) | ||
throws FirebaseInstallationServiceException { | ||
throws FirebaseException { | ||
String resourceName = String.format(DELETE_REQUEST_RESOURCE_NAME_FORMAT, projectID, fid); | ||
try { | ||
URL url = | ||
|
@@ -188,18 +191,11 @@ public void deleteFirebaseInstallation( | |
switch (httpResponseCode) { | ||
case 200: | ||
return; | ||
case 401: | ||
throw new FirebaseInstallationServiceException( | ||
UNAUTHORIZED_ERROR_MESSAGE, FirebaseInstallationServiceException.Status.UNAUTHORIZED); | ||
default: | ||
throw new FirebaseInstallationServiceException( | ||
INTERNAL_SERVER_ERROR_MESSAGE, | ||
FirebaseInstallationServiceException.Status.SERVER_ERROR); | ||
throw new FirebaseException(readErrorResponse(httpsURLConnection)); | ||
} | ||
} catch (IOException e) { | ||
throw new FirebaseInstallationServiceException( | ||
NETWORK_ERROR_MESSAGE + e.getMessage(), | ||
FirebaseInstallationServiceException.Status.NETWORK_ERROR); | ||
throw new FirebaseException(NETWORK_ERROR_MESSAGE + e.getMessage()); | ||
} | ||
} | ||
|
||
|
@@ -218,45 +214,45 @@ public InstallationTokenResult generateAuthToken( | |
@NonNull String fid, | ||
@NonNull String projectID, | ||
@NonNull String refreshToken) | ||
throws FirebaseInstallationServiceException { | ||
throws FirebaseException { | ||
String resourceName = | ||
String.format(GENERATE_AUTH_TOKEN_REQUEST_RESOURCE_NAME_FORMAT, projectID, fid); | ||
try { | ||
URL url = | ||
new URL( | ||
String.format( | ||
"https://%s/%s/%s?key=%s", | ||
FIREBASE_INSTALLATIONS_API_DOMAIN, | ||
FIREBASE_INSTALLATIONS_API_VERSION, | ||
resourceName, | ||
apiKey)); | ||
int retryCount = 0; | ||
while (retryCount <= MAX_RETRIES) { | ||
URL url = | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new URL( | ||
String.format( | ||
"https://%s/%s/%s?key=%s", | ||
FIREBASE_INSTALLATIONS_API_DOMAIN, | ||
FIREBASE_INSTALLATIONS_API_VERSION, | ||
resourceName, | ||
apiKey)); | ||
|
||
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); | ||
httpsURLConnection.setConnectTimeout(NETWORK_TIMEOUT_MILLIS); | ||
httpsURLConnection.setReadTimeout(NETWORK_TIMEOUT_MILLIS); | ||
httpsURLConnection.setDoOutput(true); | ||
httpsURLConnection.setRequestMethod("POST"); | ||
httpsURLConnection.addRequestProperty("Authorization", "FIS_v2 " + refreshToken); | ||
httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE); | ||
httpsURLConnection.addRequestProperty(ACCEPT_HEADER_KEY, JSON_CONTENT_TYPE); | ||
httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING); | ||
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); | ||
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. there's a lot of overlap between this method and the other one that sets up a POST request. Can we maybe consolidate some of the shared logic? 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. Done. |
||
httpsURLConnection.setConnectTimeout(NETWORK_TIMEOUT_MILLIS); | ||
httpsURLConnection.setReadTimeout(NETWORK_TIMEOUT_MILLIS); | ||
httpsURLConnection.setDoOutput(true); | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
httpsURLConnection.setRequestMethod("POST"); | ||
httpsURLConnection.addRequestProperty("Authorization", "FIS_v2 " + refreshToken); | ||
httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE); | ||
httpsURLConnection.addRequestProperty(ACCEPT_HEADER_KEY, JSON_CONTENT_TYPE); | ||
httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING); | ||
|
||
int httpResponseCode = httpsURLConnection.getResponseCode(); | ||
switch (httpResponseCode) { | ||
case 200: | ||
return readGenerateAuthTokenResponse(httpsURLConnection); | ||
case 401: | ||
throw new FirebaseInstallationServiceException( | ||
UNAUTHORIZED_ERROR_MESSAGE, FirebaseInstallationServiceException.Status.UNAUTHORIZED); | ||
default: | ||
throw new FirebaseInstallationServiceException( | ||
INTERNAL_SERVER_ERROR_MESSAGE, | ||
FirebaseInstallationServiceException.Status.SERVER_ERROR); | ||
int httpResponseCode = httpsURLConnection.getResponseCode(); | ||
switch (httpResponseCode) { | ||
case 200: | ||
return readGenerateAuthTokenResponse(httpsURLConnection); | ||
case 500: | ||
retryCount++; | ||
break; | ||
default: | ||
throw new FirebaseException(readErrorResponse(httpsURLConnection)); | ||
} | ||
} | ||
throw new FirebaseException(INTERNAL_SERVER_ERROR_MESSAGE); | ||
} catch (IOException e) { | ||
throw new FirebaseInstallationServiceException( | ||
NETWORK_ERROR_MESSAGE + e.getMessage(), | ||
FirebaseInstallationServiceException.Status.NETWORK_ERROR); | ||
throw new FirebaseException(NETWORK_ERROR_MESSAGE + e.getMessage()); | ||
} | ||
} | ||
// Read the response from the createFirebaseInstallation API. | ||
|
@@ -318,6 +314,19 @@ private InstallationTokenResult readGenerateAuthTokenResponse(HttpsURLConnection | |
return builder.build(); | ||
} | ||
|
||
// Read the error message from the response. | ||
private String readErrorResponse(HttpsURLConnection conn) throws IOException { | ||
BufferedReader reader = | ||
new BufferedReader(new InputStreamReader(conn.getErrorStream(), Charset.defaultCharset())); | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
StringBuilder response = new StringBuilder(); | ||
for (String input = reader.readLine(); input != null; input = reader.readLine()) { | ||
response.append(input); | ||
} | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return String.format( | ||
"The server responded with an error. HTTP response:[%d %s %s]", | ||
ankitaj224 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
conn.getResponseCode(), conn.getResponseMessage(), response); | ||
} | ||
|
||
/** Gets the Android package's SHA-1 fingerprint. */ | ||
private String getFingerprintHashForPackage() { | ||
byte[] hash; | ||
|
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.
missing a space between the colon and the error when you concat using +. It would be slightly less error prone to use either a format string here or have a dedicated method for generating these errors (or both)
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.
Updated. PTAL.