Skip to content

Commit b1ea77b

Browse files
authored
Merge pull request #23 from Instabug/feature/network_logging
🤝 Feature/network logging
2 parents a16dc95 + 0ccb0e1 commit b1ea77b

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

android/src/main/java/com/instabug/reactlibrary/RNInstabugReactnativeModule.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.instabug.library.ui.onboarding.WelcomeMessage;
4444
import com.instabug.library.InstabugCustomTextPlaceHolder;
4545
import com.instabug.library.model.Report;
46+
import com.instabug.library.model.NetworkLog;
4647
import com.instabug.library.user.UserEventParam;
4748
import com.instabug.library.visualusersteps.State;
4849

@@ -1983,6 +1984,27 @@ public void setEmailFieldRequiredForActions(boolean isEmailRequired, ReadableArr
19831984
}
19841985
}
19851986

1987+
/**
1988+
* Extracts HTTP connection properties. Request method, Headers, Date, Url and Response code
1989+
*
1990+
* @param jsonObject the JSON object containing all HTTP connection properties
1991+
* @throws JSONException
1992+
*/
1993+
@ReactMethod
1994+
public void networkLog(String jsonObject) throws JSONException {
1995+
NetworkLog networkLog = new NetworkLog();
1996+
String date = System.currentTimeMillis()+"";
1997+
networkLog.setDate(date);
1998+
JSONObject newJSONObject = new JSONObject(jsonObject);
1999+
networkLog.setUrl(newJSONObject.getString("url"));
2000+
networkLog.setRequest(newJSONObject.getString("requestBody"));
2001+
networkLog.setResponse(newJSONObject.getString("responseBody"));
2002+
networkLog.setMethod(newJSONObject.getString("method"));
2003+
networkLog.setResponseCode(newJSONObject.getInt("responseCode"));
2004+
networkLog.setHeaders(newJSONObject.getString("headers"));
2005+
networkLog.insert();
2006+
}
2007+
19862008
private InstabugCustomTextPlaceHolder.Key getStringToKeyConstant(String key) {
19872009
switch (key) {
19882010
case SHAKE_HINT:

index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,58 @@ import {
55
Platform
66
} from 'react-native';
77
let { Instabug } = NativeModules;
8+
import interceptor from './utils/NetworkInterceptor.js';
89
import InstabugUtils from './utils/InstabugUtils.js';
910
import BugReporting from './modules/BugReporting.js';
1011
import Surveys from './modules/Surveys.js';
1112
import FeatureRequests from './modules/FeatureRequests.js';
1213

1314
InstabugUtils.captureJsErrors();
1415

16+
var jsonObject = {
17+
url: '',
18+
requestBody: '',
19+
responseBody: '',
20+
method: '',
21+
responseCode: undefined,
22+
headers: ''
23+
}
24+
25+
// Register the interceptor for fetch requests
26+
interceptor.register({
27+
request: function (url, config) {
28+
// Modify the url or config here
29+
jsonObject.url = url;
30+
if(!config || !config.method) {
31+
//TO-DO: set method to GET!
32+
jsonObject.method = 'GET';
33+
}
34+
if(config) {
35+
if(config.body) {
36+
jsonObject.requestBody = config.body;
37+
} else {
38+
jsonObject.requestBody = '';
39+
}
40+
if(config.method) {
41+
jsonObject.method = config.method;
42+
}
43+
if(config.headers) {
44+
jsonObject.headers = config.headers;
45+
} else {
46+
jsonObject.headers = '';
47+
}
48+
}
49+
return [url, config];
50+
},
51+
response: function (response) {
52+
// Modify the reponse object;
53+
jsonObject.responseCode = response.status;
54+
jsonObject.responseBody = response._bodyText;
55+
Instabug.networkLog(JSON.stringify(jsonObject));
56+
return response;
57+
}
58+
});
59+
1560
/**
1661
* Instabug
1762
* @exports Instabug

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "instabug-reactnative",
3-
"version": "8.0.9",
3+
"version": "8.0.10",
44
"description": "React Native plugin for integrating the Instabug SDK",
55
"main": "index.js",
66
"repository": {

utils/NetworkInterceptor.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*Configuration for intercepting the fetch request
2+
*/
3+
4+
attach(global);
5+
var interceptorObject;
6+
7+
function attach(env) {
8+
env.fetch = (function (fetch) {
9+
return function (...args) {
10+
return interceptor(fetch, ...args);
11+
};
12+
})(env.fetch);
13+
}
14+
15+
function interceptor(fetch, ...args) {
16+
let promise = Promise.resolve(args);
17+
18+
// Register request interceptors
19+
if (interceptorObject.request || interceptorObject.requestError) {
20+
promise = promise.then(args => interceptorObject.request(...args), interceptorObject.requestError);
21+
}
22+
23+
// Register fetch call
24+
promise = promise.then(args => fetch(...args));
25+
26+
// Register response interceptors
27+
if (interceptorObject.response || interceptorObject.responseError) {
28+
promise = promise.then(interceptorObject.response, interceptorObject.responseError);
29+
}
30+
31+
return promise;
32+
}
33+
34+
module.exports = {
35+
register: function (interceptor) {
36+
interceptorObject = interceptor;
37+
return () => {
38+
interceptorObject = null;
39+
};
40+
}
41+
};

0 commit comments

Comments
 (0)