@@ -14,9 +14,12 @@ import okhttp3.Interceptor
14
14
import okhttp3.MediaType
15
15
import okhttp3.MediaType.Companion.toMediaType
16
16
import okhttp3.OkHttpClient
17
+ import okhttp3.Protocol
17
18
import okhttp3.Request
18
19
import okhttp3.RequestBody
19
20
import okhttp3.RequestBody.Companion.toRequestBody
21
+ import okhttp3.Response
22
+ import okhttp3.ResponseBody.Companion.toResponseBody
20
23
import java.util.UUID
21
24
22
25
private const val BACKOFF_MULTIPLIER : Int = 10
@@ -47,7 +50,7 @@ internal class StatsigNetwork(
47
50
private val gson = GsonBuilder ().setObjectToNumberStrategy(ToNumberPolicy .LONG_OR_DOUBLE ).create()
48
51
private val serverSessionID = UUID .randomUUID().toString();
49
52
50
- private inline fun <reified T > Gson.fromJson (json : String ) = fromJson<T >(json, object : TypeToken <T >() {}.type)
53
+ private inline fun <reified T > Gson.fromJson (json : String ) = fromJson<T >(json, object : TypeToken <T >() {}.type)
51
54
52
55
init {
53
56
val clientBuilder = OkHttpClient .Builder ()
@@ -64,6 +67,19 @@ internal class StatsigNetwork(
64
67
.build()
65
68
it.proceed(request)
66
69
})
70
+
71
+ clientBuilder.addInterceptor(Interceptor {
72
+ if (options.localMode) {
73
+ return @Interceptor Response .Builder ().code(200 )
74
+ .body(" {}" .toResponseBody(" application/json; charset=utf-8" .toMediaType()))
75
+ .protocol(Protocol .HTTP_2 )
76
+ .request(it.request())
77
+ .message(" Request blocked due to localMode being active" )
78
+ .build()
79
+ }
80
+ it.proceed(it.request())
81
+ })
82
+
67
83
statsigHttpClient = clientBuilder.build()
68
84
httpClient = OkHttpClient .Builder ().build()
69
85
}
@@ -84,7 +100,12 @@ internal class StatsigNetwork(
84
100
.build()
85
101
statsigHttpClient.newCall(request).await().use { response ->
86
102
val apiGate = gson.fromJson(response.body?.charStream(), APIFeatureGate ::class .java)
87
- return ConfigEvaluation (fetchFromServer = false , booleanValue = apiGate.value, apiGate.value.toString(), apiGate.ruleID ? : " " )
103
+ return ConfigEvaluation (
104
+ fetchFromServer = false ,
105
+ booleanValue = apiGate.value,
106
+ apiGate.value.toString(),
107
+ apiGate.ruleID ? : " "
108
+ )
88
109
}
89
110
}
90
111
@@ -104,7 +125,12 @@ internal class StatsigNetwork(
104
125
.build()
105
126
statsigHttpClient.newCall(request).await().use { response ->
106
127
val apiConfig = gson.fromJson(response.body?.charStream(), APIDynamicConfig ::class .java)
107
- return ConfigEvaluation (fetchFromServer = false , booleanValue = false , apiConfig.value, apiConfig.ruleID ? : " " )
128
+ return ConfigEvaluation (
129
+ fetchFromServer = false ,
130
+ booleanValue = false ,
131
+ apiConfig.value,
132
+ apiConfig.ruleID ? : " "
133
+ )
108
134
}
109
135
}
110
136
@@ -114,7 +140,8 @@ internal class StatsigNetwork(
114
140
}
115
141
try {
116
142
return gson.fromJson(specs, APIDownloadedConfigs ::class .java)
117
- } catch (e: Exception ) {}
143
+ } catch (_: Exception ) {
144
+ }
118
145
return null
119
146
}
120
147
@@ -134,7 +161,8 @@ internal class StatsigNetwork(
134
161
lastSyncTime = configs.time
135
162
return configs
136
163
}
137
- } catch (e: Exception ) {}
164
+ } catch (_: Exception ) {
165
+ }
138
166
139
167
return null
140
168
}
@@ -177,7 +205,8 @@ internal class StatsigNetwork(
177
205
list.size = list.size + contentLength
178
206
}
179
207
}
180
- } catch (e: Exception ) {}
208
+ } catch (_: Exception ) {
209
+ }
181
210
}
182
211
183
212
suspend fun getAllIDLists (evaluator : Evaluator ) {
@@ -196,7 +225,7 @@ internal class StatsigNetwork(
196
225
for ((name, serverList) in jsonResponse) {
197
226
var localList = allLocalLists[name]
198
227
if (localList == null ) {
199
- localList = IDList (name= name)
228
+ localList = IDList (name = name)
200
229
allLocalLists[name] = localList
201
230
}
202
231
if (serverList.url == null || serverList.fileID == null || serverList.creationTime < localList.creationTime) {
@@ -206,10 +235,10 @@ internal class StatsigNetwork(
206
235
// check if fileID has changed and it is indeed a newer file. If so, reset the list
207
236
if (serverList.fileID != localList.fileID && serverList.creationTime >= localList.creationTime) {
208
237
localList = IDList (
209
- name= name,
210
- url= serverList.url,
211
- fileID= serverList.fileID,
212
- size= 0 ,
238
+ name = name,
239
+ url = serverList.url,
240
+ fileID = serverList.fileID,
241
+ size = 0 ,
213
242
creationTime = serverList.creationTime
214
243
)
215
244
allLocalLists[name] = localList
@@ -234,7 +263,8 @@ internal class StatsigNetwork(
234
263
}
235
264
}
236
265
}
237
- } catch (e: Exception ) {}
266
+ } catch (_: Exception ) {
267
+ }
238
268
}
239
269
}
240
270
@@ -262,7 +292,12 @@ internal class StatsigNetwork(
262
292
retryPostLogs(events, statsigMetadata, 5 , 1 )
263
293
}
264
294
265
- suspend fun retryPostLogs (events : List <StatsigEvent >, statsigMetadata : Map <String , String >, retries : Int , backoff : Int ) {
295
+ suspend fun retryPostLogs (
296
+ events : List <StatsigEvent >,
297
+ statsigMetadata : Map <String , String >,
298
+ retries : Int ,
299
+ backoff : Int
300
+ ) {
266
301
if (events.isEmpty()) {
267
302
return
268
303
}
@@ -285,7 +320,8 @@ internal class StatsigNetwork(
285
320
return @coroutineScope
286
321
}
287
322
}
288
- } catch (e: Exception ) { }
323
+ } catch (_: Exception ) {
324
+ }
289
325
290
326
val count = retries - -- currRetry
291
327
delay(backoff * (backoffMultiplier * count) * MS_IN_S )
@@ -297,4 +333,4 @@ internal class StatsigNetwork(
297
333
statsigHttpClient.dispatcher.executorService.shutdown()
298
334
httpClient.dispatcher.executorService.shutdown()
299
335
}
300
- }
336
+ }
0 commit comments