7
7
8
8
import androidx .annotation .NonNull ;
9
9
10
+ import com .facebook .react .bridge .Arguments ;
10
11
import com .facebook .react .bridge .Promise ;
11
12
import com .facebook .react .bridge .ReactApplicationContext ;
12
13
import com .facebook .react .bridge .ReactMethod ;
14
+ import com .facebook .react .bridge .ReadableMap ;
15
+ import com .facebook .react .bridge .WritableMap ;
16
+ import com .facebook .react .bridge .WritableNativeMap ;
13
17
import com .instabug .apm .InternalAPM ;
18
+ import com .instabug .apm .sanitization .AsyncSanitizer ;
19
+ import com .instabug .apm .sanitization .OnCompleteCallback ;
20
+ import com .instabug .library .logging .listeners .networklogs .NetworkLogSnapshot ;
14
21
import com .instabug .reactlibrary .utils .EventEmitterModule ;
15
22
import com .instabug .reactlibrary .utils .MainThreadHandler ;
16
23
24
+ import org .json .JSONException ;
25
+ import org .json .JSONObject ;
26
+
27
+ import java .util .HashMap ;
28
+ import java .util .Map ;
29
+ import java .util .concurrent .ConcurrentHashMap ;
30
+
17
31
18
32
public class RNInstabugNetworkLoggerModule extends EventEmitterModule {
19
33
34
+ private final ConcurrentHashMap <String , OnCompleteCallback <NetworkLogSnapshot >> callbackMap = new ConcurrentHashMap <String , OnCompleteCallback <NetworkLogSnapshot >>();
35
+
20
36
public RNInstabugNetworkLoggerModule (ReactApplicationContext reactContext ) {
21
37
super (reactContext );
22
38
}
23
39
40
+
24
41
@ NonNull
25
42
@ Override
26
43
public String getName () {
@@ -39,7 +56,17 @@ public void removeListeners(Integer count) {
39
56
}
40
57
41
58
private boolean getFlagValue (String key ) {
42
- return InternalAPM ._isFeatureEnabledCP ( key , "" );
59
+ return InternalAPM ._isFeatureEnabledCP (key , "" );
60
+ }
61
+
62
+ private WritableMap convertFromMapToWriteableMap (Map map ) {
63
+ WritableMap writableMap = new WritableNativeMap ();
64
+ for (int i = 0 ; i < map .size (); i ++) {
65
+ Object key = map .keySet ().toArray ()[i ];
66
+ Object value = map .get (key );
67
+ writableMap .putString ((String ) key , (String ) value );
68
+ }
69
+ return writableMap ;
43
70
}
44
71
45
72
/**
@@ -81,4 +108,68 @@ public void run() {
81
108
}
82
109
});
83
110
}
111
+
112
+
113
+ @ ReactMethod
114
+ public void registerNetworkLogsListener () {
115
+ MainThreadHandler .runOnMainThread (new Runnable () {
116
+ @ Override
117
+ public void run () {
118
+ InternalAPM ._registerNetworkLogSanitizer ((networkLogSnapshot , onCompleteCallback ) -> {
119
+ final String id = String .valueOf (onCompleteCallback .hashCode ());
120
+ callbackMap .put (id , onCompleteCallback );
121
+
122
+ WritableMap networkSnapshotParams = Arguments .createMap ();
123
+ networkSnapshotParams .putString ("id" , id );
124
+ networkSnapshotParams .putString ("url" , networkLogSnapshot .getUrl ());
125
+ networkSnapshotParams .putInt ("responseCode" , networkLogSnapshot .getResponseCode ());
126
+ networkSnapshotParams .putString ("requestBody" , networkLogSnapshot .getRequestBody ());
127
+ networkSnapshotParams .putString ("response" , networkLogSnapshot .getResponse ());
128
+ final Map <String , Object > requestHeaders = networkLogSnapshot .getRequestHeaders ();
129
+ if (requestHeaders != null ) {
130
+ networkSnapshotParams .putMap ("requestHeader" , convertFromMapToWriteableMap (networkLogSnapshot .getRequestHeaders ()));
131
+ }
132
+ final Map <String , Object > responseHeaders = networkLogSnapshot .getResponseHeaders ();
133
+ if (responseHeaders != null ) {
134
+ networkSnapshotParams .putMap ("responseHeader" , convertFromMapToWriteableMap (networkLogSnapshot .getResponseHeaders ()));
135
+ }
136
+
137
+ sendEvent ("IBGNetworkLoggerHandler" , networkSnapshotParams );
138
+ });
139
+ }
140
+ });
141
+ }
142
+
143
+ @ ReactMethod
144
+ protected void updateNetworkLogSnapshot (String jsonString ) {
145
+
146
+ JSONObject newJSONObject = null ;
147
+ String url = "" ;
148
+ NetworkLogSnapshot modifiedSnapshot = null ;
149
+ try {
150
+ newJSONObject = new JSONObject (jsonString );
151
+ url = newJSONObject .optString ("url" );
152
+ } catch (JSONException e ) {
153
+ throw new RuntimeException (e );
154
+ }
155
+ final String ID = newJSONObject .optString ("id" );
156
+
157
+ if (!url .isEmpty ()) {
158
+ modifiedSnapshot = new NetworkLogSnapshot (
159
+ url ,
160
+ null ,
161
+ null ,
162
+ null ,
163
+ null ,
164
+ newJSONObject .optInt ("responseCode" )
165
+ );
166
+ }
167
+
168
+ final OnCompleteCallback <NetworkLogSnapshot > callback = callbackMap .get (ID );
169
+ if (callback != null ) {
170
+ callback .onComplete (modifiedSnapshot );
171
+ }
172
+ callbackMap .remove (ID );
173
+
174
+ }
84
175
}
0 commit comments