Skip to content

Commit

Permalink
[SDK-2329] Added apiUrl check in BranchJSONConfig (#1183)
Browse files Browse the repository at this point in the history
* Added apiUrl check in BranchJSONConfig

* Fixed backslash issue

* Update Branch.java

* Update Branch.java

* Extracted to function and added log

* Cleaned branch.json

* Added / logic

* Update branch.json

* Removed redundant logic

* Update BranchJsonConfig.java

* Update BranchJsonConfig.java
  • Loading branch information
nsingh-branch authored Apr 1, 2024
1 parent c83aa93 commit e9bea73
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Branch-SDK-TestBed/src/main/assets/branch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ protected void onStart() {
Branch.getInstance().addFacebookPartnerParameterWithName("ph", getHashedValue("6516006060"));
Log.d("BranchSDK_Tester", "initSession");

initSessionsWithTests();
//initSessionsWithTests();

// Branch integration validation: Validate Branch integration with your app
// NOTE : The below method will run few checks for verifying correctness of the Branch integration.
Expand Down
15 changes: 14 additions & 1 deletion Branch-SDK/src/main/java/io/branch/referral/Branch.java
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ synchronized public static Branch getAutoInstance(@NonNull Context context) {
// Should only be set in json config
deferInitForPluginRuntime(BranchUtil.getDeferInitForPluginRuntimeConfig(context));

BranchUtil.setAPIBaseUrlFromConfig(context);

BranchUtil.setTestMode(BranchUtil.checkTestMode(context));
branchReferral_ = initBranchSDK(context, BranchUtil.readBranchKey(context));
getPreinstallSystemData(branchReferral_, context);
Expand Down Expand Up @@ -404,6 +406,8 @@ public static Branch getAutoInstance(@NonNull Context context, @NonNull String b
// Should only be set in json config
deferInitForPluginRuntime(BranchUtil.getDeferInitForPluginRuntimeConfig(context));

BranchUtil.setAPIBaseUrlFromConfig(context);

BranchUtil.setTestMode(BranchUtil.checkTestMode(context));
// If a Branch key is passed already use it. Else read the key
if (!isValidBranchKey(branchKey)) {
Expand Down Expand Up @@ -503,7 +507,16 @@ public static void expectDelayedSessionInitialization(boolean expectDelayedInit)
* @param url The {@link String} URL base URL that the Branch API uses.
*/
public static void setAPIUrl(String url) {
PrefHelper.setAPIUrl(url);
if (!TextUtils.isEmpty(url)) {
if (!url.endsWith("/")) {
url = url + "/";
}

PrefHelper.setAPIUrl(url);
BranchLogger.v("setAPIUrl: Branch API URL was set to " + url);
} else {
BranchLogger.w("setAPIUrl: URL cannot be empty or null");
}
}
/**
* <p>Sets a custom CDN base URL.</p>
Expand Down
24 changes: 20 additions & 4 deletions Branch-SDK/src/main/java/io/branch/referral/BranchJsonConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public enum BranchJsonKey {
liveKey,
useTestInstance,
enableLogging,
deferInitForPluginRuntime
deferInitForPluginRuntime,
apiUrl
}

/*
Expand All @@ -44,7 +45,8 @@ public enum BranchJsonKey {
"liveKey":"key_live_hcnegAumkH7Kv18M8AOHhfgiohpXq5tB",
"useTestInstance": true,
"enableLogging": true,
"deferInitForPluginRuntime": true
"deferInitForPluginRuntime": true,
"apiUrl": "https://api2.branch.io"
}
*/

Expand Down Expand Up @@ -167,8 +169,8 @@ private String getTestKey() {
if (mConfiguration == null) return null;

try {
if (!mConfiguration.has("testKey")) return null;
return mConfiguration.getString("testKey");
if (!mConfiguration.has(BranchJsonKey.testKey.toString())) return null;
return mConfiguration.getString(BranchJsonKey.testKey.toString());
}
catch (JSONException exception) {
Log.e(TAG, "Error parsing branch.json: " + exception.getMessage());
Expand All @@ -186,4 +188,18 @@ private String getTestKey() {
return false;
}
}

@Nullable
public String getAPIUrl() {
if (mConfiguration == null) return null;

try {
if (!mConfiguration.has(BranchJsonKey.apiUrl.toString())) return null;
return mConfiguration.getString(BranchJsonKey.apiUrl.toString());
}
catch (JSONException exception) {
Log.e(TAG, "Error parsing branch.json: " + exception.getMessage());
return null;
}
}
}
7 changes: 7 additions & 0 deletions Branch-SDK/src/main/java/io/branch/referral/BranchUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;

import android.text.TextUtils;
import android.util.DisplayMetrics;

import org.json.JSONArray;
Expand Down Expand Up @@ -139,6 +140,12 @@ public static boolean getDeferInitForPluginRuntimeConfig(Context context){
return deferInitForPluginRuntime;
}

public static void setAPIBaseUrlFromConfig(Context context) {
BranchJsonConfig jsonConfig = BranchJsonConfig.getInstance(context);
String apiUrl = jsonConfig.getAPIUrl();
Branch.setAPIUrl(apiUrl);
}

/**
* Get the value of "io.branch.sdk.TestMode" entry in application manifest or from String res.
* This value can be overridden via. {@link Branch#enableTestMode()}
Expand Down

0 comments on commit e9bea73

Please sign in to comment.