-
Notifications
You must be signed in to change notification settings - Fork 6
feat: custom base url support #109
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
59 changes: 59 additions & 0 deletions
59
android/src/main/java/com/stallion/utils/StallionApiBaseUrl.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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package com.stallion.utils; | ||
|
|
||
| import com.stallion.storage.StallionConfig; | ||
| import com.stallion.storage.StallionStateManager; | ||
| import com.stallion.networkmanager.StallionApiConstants; | ||
|
|
||
| public class StallionApiBaseUrl { | ||
|
|
||
| /** | ||
| * Gets the API base URL: custom baseUrl if set, else regional URL from app token. | ||
| * @return String - The base URL to use | ||
| */ | ||
| public static String get() { | ||
| try { | ||
| StallionStateManager stateManager = StallionStateManager.getInstance(); | ||
| if (stateManager != null && stateManager.getStallionConfig() != null) { | ||
| return resolve(stateManager.getStallionConfig()); | ||
| } | ||
| } catch (Exception e) { | ||
| // Fallback to regional default on any error | ||
| } | ||
| return StallionApiConstants.REGIONAL_API_BASE_AP; | ||
| } | ||
|
|
||
| static String resolve(StallionConfig config) { | ||
| String stored = config.getBaseUrl(); | ||
| if (stored != null && !stored.isEmpty()) { | ||
| return stored; | ||
| } | ||
|
|
||
| String region = StallionTokenRegion.parseTokenRegion(config.getAppToken()); | ||
| if (region == null) { | ||
| region = StallionTokenRegion.defaultRegion(); | ||
| } | ||
| return regionalBaseUrl(region); | ||
| } | ||
|
|
||
| static String regionalBaseUrl(String region) { | ||
| if ("us".equals(region)) { | ||
| return StallionApiConstants.REGIONAL_API_BASE_US; | ||
| } | ||
| return StallionApiConstants.REGIONAL_API_BASE_AP; | ||
| } | ||
|
|
||
| /** | ||
| * Sets a custom base URL, or clears it when null/empty. | ||
| * @param baseUrl - The custom base URL to set, or null/empty to clear | ||
| */ | ||
| public static void set(String baseUrl) { | ||
| try { | ||
| StallionStateManager stateManager = StallionStateManager.getInstance(); | ||
| if (stateManager != null && stateManager.getStallionConfig() != null) { | ||
| stateManager.getStallionConfig().setBaseUrl(baseUrl); | ||
| } | ||
| } catch (Exception e) { | ||
| // Silently fail | ||
| } | ||
| } | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
android/src/main/java/com/stallion/utils/StallionTokenRegion.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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.stallion.utils; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.HashSet; | ||
| import java.util.Locale; | ||
| import java.util.Set; | ||
|
|
||
| public final class StallionTokenRegion { | ||
|
|
||
| private static final Set<String> VALID_REGIONS = new HashSet<>(Arrays.asList("ap", "us")); | ||
|
|
||
| private StallionTokenRegion() {} | ||
|
|
||
| /** | ||
| * Parses the region prefix from a Stallion token. | ||
| * Returns "ap", "us", or null. Null means legacy unprefixed token; caller should default to "ap". | ||
| */ | ||
| public static String parseTokenRegion(String token) { | ||
| if (token == null) { | ||
| return null; | ||
| } | ||
| token = token.trim(); | ||
| if (token.isEmpty()) { | ||
| return null; | ||
| } | ||
|
|
||
| // App token: spb_<region>_<44-char nanoid> → 49 chars | ||
| if (token.startsWith("spb_") && token.length() == 49 && token.charAt(6) == '_') { | ||
| return extractRegion(token); | ||
| } | ||
|
|
||
| // CI token: stl_<region>_<36-char nanoid> → 43 chars | ||
| if (token.startsWith("stl_") && token.length() == 43 && token.charAt(6) == '_') { | ||
| return extractRegion(token); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| public static String defaultRegion() { | ||
| return "ap"; | ||
| } | ||
|
|
||
| private static String extractRegion(String token) { | ||
| String code = token.substring(4, 6).toLowerCase(Locale.ROOT); | ||
| if (!code.matches("[a-z]{2}")) { | ||
| return null; | ||
| } | ||
| return VALID_REGIONS.contains(code) ? code : null; | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // | ||
| // StallionApiBaseUrl.swift | ||
| // react-native-stallion | ||
| // | ||
| // Created for centralized base URL management | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| class StallionApiBaseUrl { | ||
| /** | ||
| * Gets the API base URL: custom baseUrl if set, else regional URL from app token. | ||
| */ | ||
| static func get() -> String { | ||
| guard let stateManager = StallionStateManager.sharedInstance() else { | ||
| return StallionConstants.REGIONAL_API_BASE_AP | ||
| } | ||
|
|
||
| return resolve(config: stateManager.stallionConfig) | ||
| } | ||
|
|
||
| static func resolve(config: StallionConfig) -> String { | ||
| let stored = config.baseUrl ?? "" | ||
| if !stored.isEmpty { | ||
| return stored | ||
| } | ||
|
|
||
| var region = StallionTokenRegion.parseTokenRegion(config.appToken) | ||
| if region == nil { | ||
| region = StallionTokenRegion.defaultRegion() | ||
| } | ||
| return regionalBaseUrl(region!) | ||
| } | ||
|
|
||
| static func regionalBaseUrl(_ region: String) -> String { | ||
| if region == "us" { | ||
| return StallionConstants.REGIONAL_API_BASE_US | ||
| } | ||
| return StallionConstants.REGIONAL_API_BASE_AP | ||
| } | ||
|
|
||
| /** | ||
| * Sets a custom base URL, or clears it when empty. | ||
| */ | ||
| static func set(_ baseUrl: String) { | ||
| guard let stateManager = StallionStateManager.sharedInstance() else { | ||
| return | ||
| } | ||
| stateManager.stallionConfig.updateBaseUrl(baseUrl) | ||
| } | ||
| } |
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
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.