-
Notifications
You must be signed in to change notification settings - Fork 38
[SDK-568] Add stable cross-SDK identifiers to IterableDataRegion #1081
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
Open
franco-zalamena-iterable
wants to merge
5
commits into
master
Choose a base branch
from
feature/SDK-568-data-region-parity
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.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c3652f2
[SDK-568] Add stable cross-SDK identifiers to IterableDataRegion
franco-zalamena-iterable d4c9288
[SDK-568] Name 3.12.0 as the removal version for the deprecated BASE_β¦
franco-zalamena-iterable fb4efa7
Merge remote-tracking branch 'origin/master' into feature/SDK-568-datβ¦
franco-zalamena-iterable 0de07ac
[SDK-568] Document null data region fallback and why regionCode is stβ¦
franco-zalamena-iterable 34475b2
Merge remote-tracking branch 'origin/master' into feature/SDK-568-datβ¦
franco-zalamena-iterable 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
101 changes: 98 additions & 3 deletions
101
iterableapi/src/main/java/com/iterable/iterableapi/IterableDataRegion.java
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 |
|---|---|---|
| @@ -1,16 +1,111 @@ | ||
| package com.iterable.iterableapi; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
| import androidx.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Data region determining which data center and endpoints the SDK sends data to. | ||
| * Defaults to {@link #US}; EU-hosted projects must select {@link #EU}. | ||
| */ | ||
| public enum IterableDataRegion { | ||
| US("https://api.iterable.com/api/"), | ||
| EU("https://api.eu.iterable.com/api/"); | ||
| US(0, "US", "https://api.iterable.com/api/"), | ||
| EU(1, "EU", "https://api.eu.iterable.com/api/"); | ||
|
|
||
| private static final String TAG = "IterableDataRegion"; | ||
|
|
||
| private final int code; | ||
| private final String regionCode; | ||
| private final String endpoint; | ||
|
|
||
| IterableDataRegion(String endpoint) { | ||
| IterableDataRegion(int code, String regionCode, String endpoint) { | ||
| this.code = code; | ||
| this.regionCode = regionCode; | ||
| this.endpoint = endpoint; | ||
| } | ||
|
|
||
| public String getEndpoint() { | ||
| return this.endpoint; | ||
| } | ||
|
|
||
| /** | ||
| * Stable numeric identifier for this region, matching the values used by the React Native and | ||
| * Flutter SDKs. Unlike {@link #ordinal()} this is guaranteed not to shift if regions are added. | ||
| */ | ||
| public int getCode() { | ||
| return this.code; | ||
| } | ||
|
|
||
| /** | ||
| * Stable short identifier for this region (for example {@code "EU"}), matching the values used | ||
| * by Iterable's other SDKs. It currently matches {@link #name()}, but is stored separately for | ||
| * the same reason {@link #getCode()} does not use {@link #ordinal()}: renaming an enum constant | ||
| * must not change an identifier that wrappers and persisted configuration already rely on. | ||
| */ | ||
| @NonNull | ||
| public String getRegionCode() { | ||
| return this.regionCode; | ||
| } | ||
|
rtlsilva marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Resolves a region from its short identifier (e.g. {@code "EU"}), accepting either case. Also | ||
| * accepts a full API endpoint URL, so values from the iOS SDK's string-based data region can be | ||
| * passed through unchanged. | ||
| * <p> | ||
| * Unrecognised or null values fall back to {@link #US} and are logged, matching the behaviour of | ||
| * Iterable's other SDKs. | ||
| * | ||
| * @param value region identifier, or {@code null} | ||
| * @return the matching region, or {@link #US} if the value is not recognised | ||
| */ | ||
| @NonNull | ||
| public static IterableDataRegion from(@Nullable String value) { | ||
| if (value == null || value.trim().isEmpty()) { | ||
| IterableLogger.w(TAG, "No data region specified, defaulting to " + US.regionCode); | ||
| return US; | ||
| } | ||
|
|
||
| String normalized = value.trim(); | ||
| for (IterableDataRegion region : values()) { | ||
| if (normalized.equalsIgnoreCase(region.regionCode) || normalized.equalsIgnoreCase(region.endpoint)) { | ||
| return region; | ||
| } | ||
| } | ||
|
|
||
| IterableLogger.w(TAG, "Unsupported data region \"" + value + "\", defaulting to " + US.regionCode | ||
| + ". Supported values: " + supportedRegionCodes()); | ||
| return US; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves a region from its numeric identifier, as used by the React Native and Flutter SDKs. | ||
| * <p> | ||
| * Unrecognised values fall back to {@link #US} and are logged, matching the behaviour of | ||
| * Iterable's other SDKs. | ||
| * | ||
| * @param code region identifier, see {@link #getCode()} | ||
| * @return the matching region, or {@link #US} if the code is not recognised | ||
| */ | ||
| @NonNull | ||
| public static IterableDataRegion from(int code) { | ||
| for (IterableDataRegion region : values()) { | ||
| if (region.code == code) { | ||
| return region; | ||
| } | ||
| } | ||
|
|
||
| IterableLogger.w(TAG, "Unsupported data region code " + code + ", defaulting to " + US.regionCode | ||
| + ". Supported values: " + supportedRegionCodes()); | ||
| return US; | ||
| } | ||
|
|
||
| private static String supportedRegionCodes() { | ||
| StringBuilder builder = new StringBuilder(); | ||
| for (IterableDataRegion region : values()) { | ||
| if (builder.length() > 0) { | ||
| builder.append(", "); | ||
| } | ||
| builder.append(region.regionCode).append(" (").append(region.code).append(")"); | ||
| } | ||
| return builder.toString(); | ||
| } | ||
| } | ||
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
81 changes: 81 additions & 0 deletions
81
iterableapi/src/test/java/com/iterable/iterableapi/IterableDataRegionTest.kt
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 |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package com.iterable.iterableapi | ||
|
|
||
| import org.hamcrest.Matchers.`is` | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertThat | ||
| import org.junit.Test | ||
|
|
||
| class IterableDataRegionTest { | ||
|
|
||
| @Test | ||
| fun endpointsMatchDataCenters() { | ||
| assertEquals("https://api.iterable.com/api/", IterableDataRegion.US.endpoint) | ||
| assertEquals("https://api.eu.iterable.com/api/", IterableDataRegion.EU.endpoint) | ||
| } | ||
|
|
||
| /** These identifiers are part of the cross-SDK contract; changing them breaks the wrappers. */ | ||
| @Test | ||
| fun stableIdentifiersMatchOtherSdks() { | ||
| assertEquals(0, IterableDataRegion.US.code) | ||
| assertEquals(1, IterableDataRegion.EU.code) | ||
| assertEquals("US", IterableDataRegion.US.regionCode) | ||
| assertEquals("EU", IterableDataRegion.EU.regionCode) | ||
| } | ||
|
|
||
| @Test | ||
| fun fromRegionCode() { | ||
| assertThat(IterableDataRegion.from("US"), `is`(IterableDataRegion.US)) | ||
| assertThat(IterableDataRegion.from("EU"), `is`(IterableDataRegion.EU)) | ||
| } | ||
|
|
||
| @Test | ||
| fun fromRegionCodeIsCaseAndWhitespaceInsensitive() { | ||
| assertThat(IterableDataRegion.from("eu"), `is`(IterableDataRegion.EU)) | ||
| assertThat(IterableDataRegion.from("Eu"), `is`(IterableDataRegion.EU)) | ||
| assertThat(IterableDataRegion.from(" EU "), `is`(IterableDataRegion.EU)) | ||
| } | ||
|
|
||
| /** iOS expresses the data region as the endpoint URL itself, so those values must resolve. */ | ||
| @Test | ||
| fun fromIosStyleEndpointUrl() { | ||
| assertThat( | ||
| IterableDataRegion.from("https://api.eu.iterable.com/api/"), | ||
| `is`(IterableDataRegion.EU) | ||
| ) | ||
| assertThat( | ||
| IterableDataRegion.from("https://api.iterable.com/api/"), | ||
| `is`(IterableDataRegion.US) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun fromUnsupportedStringFallsBackToUs() { | ||
| assertThat(IterableDataRegion.from("APAC"), `is`(IterableDataRegion.US)) | ||
| assertThat(IterableDataRegion.from(""), `is`(IterableDataRegion.US)) | ||
| assertThat(IterableDataRegion.from(" "), `is`(IterableDataRegion.US)) | ||
| assertThat(IterableDataRegion.from(null as String?), `is`(IterableDataRegion.US)) | ||
| } | ||
|
|
||
| @Test | ||
| fun fromCode() { | ||
| assertThat(IterableDataRegion.from(0), `is`(IterableDataRegion.US)) | ||
| assertThat(IterableDataRegion.from(1), `is`(IterableDataRegion.EU)) | ||
| } | ||
|
|
||
| @Test | ||
| fun fromUnsupportedCodeFallsBackToUs() { | ||
| assertThat(IterableDataRegion.from(2), `is`(IterableDataRegion.US)) | ||
| assertThat(IterableDataRegion.from(-1), `is`(IterableDataRegion.US)) | ||
| assertThat(IterableDataRegion.from(Int.MAX_VALUE), `is`(IterableDataRegion.US)) | ||
| } | ||
|
|
||
| /** Round-trips guard the wrappers, which serialize the region and resolve it back. */ | ||
| @Test | ||
| fun identifiersRoundTrip() { | ||
| for (region in IterableDataRegion.values()) { | ||
| assertThat(IterableDataRegion.from(region.code), `is`(region)) | ||
| assertThat(IterableDataRegion.from(region.regionCode), `is`(region)) | ||
| assertThat(IterableDataRegion.from(region.endpoint), `is`(region)) | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.