Skip to content

Commit a4e0ab7

Browse files
Add experimental method to trace requests (#1320)
* add experimental method to trace requests * Update CustomBranchApp.java * Update ServerRequestQueue.java * Update gradle.properties * add request url * only invoke trace callback for open and install * clean up * Update CustomBranchApp.java * Update CustomBranchApp.java
1 parent 0a44f2f commit a4e0ab7

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

Branch-SDK-TestBed/src/main/java/io/branch/branchandroidtestbed/CustomBranchApp.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

88
import androidx.browser.customtabs.CustomTabsIntent;
99

10+
import org.json.JSONObject;
11+
1012
import java.io.File;
1113
import java.io.FileOutputStream;
1214
import java.io.OutputStreamWriter;
1315

14-
import io.branch.interfaces.IBranchLoggingCallbacks;
1516
import io.branch.referral.Branch;
1617
import io.branch.referral.BranchLogger;
17-
import io.branch.referral.Defines;
18+
import io.branch.referral.IBranchRequestTracingCallback;
1819

1920
public final class CustomBranchApp extends Application {
2021
@Override
@@ -32,6 +33,18 @@ public void onCreate() {
3233
.setColorScheme(COLOR_SCHEME_DARK)
3334
.build();
3435
Branch.getInstance().setCustomTabsIntent(customTabsIntent);
36+
Branch.setCallbackForTracingRequests(new IBranchRequestTracingCallback() {
37+
@Override
38+
public void onRequestCompleted(String uri, JSONObject request, JSONObject response, String error, String requestUrl) {
39+
Log.d("Shortlink_Session_Test",
40+
"URI Sent to Branch: " + uri
41+
+ "\nRequest: " + request
42+
+ "\nResponse: " + response
43+
+ "\nError Message: " + error
44+
+ "\nRequest Url: " + requestUrl
45+
);
46+
}
47+
});
3548
}
3649

3750
private void saveLogToFile(String logMessage) {

Branch-SDK/src/main/java/io/branch/referral/Branch.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ enum INTENT_STATE {
309309
private Uri deferredUri;
310310
private InitSessionBuilder deferredSessionBuilder;
311311

312+
private static IBranchRequestTracingCallback _iBranchRequestTracingCallback;
313+
312314
/**
313315
* <p>The main constructor of the Branch class is private because the class uses the Singleton
314316
* pattern.</p>
@@ -2821,4 +2823,12 @@ private void launchExternalBrowser(String url) {
28212823
BranchLogger.e("launchExternalBrowser caught exception: " + ex);
28222824
}
28232825
}
2826+
2827+
public static void setCallbackForTracingRequests(IBranchRequestTracingCallback iBranchRequestTracingCallback){
2828+
_iBranchRequestTracingCallback = iBranchRequestTracingCallback;
2829+
}
2830+
2831+
public static IBranchRequestTracingCallback getCallbackForTracingRequests(){
2832+
return _iBranchRequestTracingCallback;
2833+
}
28242834
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package io.branch.referral;
2+
3+
import org.json.JSONObject;
4+
5+
public interface IBranchRequestTracingCallback {
6+
void onRequestCompleted(String uri, JSONObject request, JSONObject response, String error, String requestUrl);
7+
}

Branch-SDK/src/main/java/io/branch/referral/ServerRequestQueue.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,32 @@ protected void onPostExecute(ServerResponse serverResponse) {
578578
}
579579

580580
void onPostExecuteInner(ServerResponse serverResponse) {
581-
BranchLogger.v("onPostExecuteInner " + this + " " + serverResponse);
581+
try {
582+
// For the time being, execute the callback only for init requests
583+
BranchLogger.v("onPostExecuteInner " + thisReq_);
584+
if (Branch.getCallbackForTracingRequests() != null && (thisReq_ instanceof ServerRequestInitSession)) {
585+
String uri = "";
586+
587+
if(thisReq_.getPost().has(Defines.Jsonkey.External_Intent_URI.getKey())){
588+
uri = thisReq_.getPost().getString(Defines.Jsonkey.External_Intent_URI.getKey());
589+
}
590+
591+
JSONObject requestJson = thisReq_.getPost();
592+
JSONObject requestResponse = serverResponse.getObject();
593+
594+
String error = "";
595+
596+
if (serverResponse.getStatusCode() != 200) {
597+
error = (new BranchError(serverResponse.getMessage(), serverResponse.getStatusCode())).toString();
598+
}
599+
600+
Branch.getCallbackForTracingRequests().onRequestCompleted(uri, requestJson, requestResponse, error, thisReq_.getRequestUrl());
601+
}
602+
}
603+
catch (Exception exception){
604+
BranchLogger.e("Failed to invoke tracing request callback:" + exception.getMessage());
605+
}
606+
582607
if (latch_ != null) {
583608
latch_.countDown();
584609
}

0 commit comments

Comments
 (0)