Skip to content

Commit 18dcfc6

Browse files
authored
Merge pull request #293 from Automattic/mehow/lazy-http
Use Call.Factory instead of OkHttpClient directly
2 parents 8da166e + 7985925 commit 18dcfc6

File tree

5 files changed

+11
-10
lines changed

5 files changed

+11
-10
lines changed

experimentation/api/experimentation.api

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public abstract interface class com/automattic/android/experimentation/Variation
2929
}
3030

3131
public final class com/automattic/android/experimentation/VariationsRepository$Companion {
32-
public final fun create (Ljava/lang/String;Ljava/util/Set;Lcom/automattic/android/experimentation/ExperimentLogger;ZLjava/io/File;Lokhttp3/OkHttpClient;Lkotlinx/coroutines/CoroutineScope;Lkotlinx/coroutines/CoroutineDispatcher;)Lcom/automattic/android/experimentation/VariationsRepository;
33-
public static synthetic fun create$default (Lcom/automattic/android/experimentation/VariationsRepository$Companion;Ljava/lang/String;Ljava/util/Set;Lcom/automattic/android/experimentation/ExperimentLogger;ZLjava/io/File;Lokhttp3/OkHttpClient;Lkotlinx/coroutines/CoroutineScope;Lkotlinx/coroutines/CoroutineDispatcher;ILjava/lang/Object;)Lcom/automattic/android/experimentation/VariationsRepository;
32+
public final fun create (Ljava/lang/String;Ljava/util/Set;Lcom/automattic/android/experimentation/ExperimentLogger;ZLjava/io/File;Lokhttp3/Call$Factory;Lkotlinx/coroutines/CoroutineScope;Lkotlinx/coroutines/CoroutineDispatcher;)Lcom/automattic/android/experimentation/VariationsRepository;
33+
public static synthetic fun create$default (Lcom/automattic/android/experimentation/VariationsRepository$Companion;Ljava/lang/String;Ljava/util/Set;Lcom/automattic/android/experimentation/ExperimentLogger;ZLjava/io/File;Lokhttp3/Call$Factory;Lkotlinx/coroutines/CoroutineScope;Lkotlinx/coroutines/CoroutineDispatcher;ILjava/lang/Object;)Lcom/automattic/android/experimentation/VariationsRepository;
3434
}
3535

3636
public final class com/automattic/android/experimentation/VariationsRepository$DefaultImpls {

experimentation/src/main/java/com/automattic/android/experimentation/VariationsRepository.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.automattic.android.experimentation.repository.AssignmentsRepository
1010
import kotlinx.coroutines.CoroutineDispatcher
1111
import kotlinx.coroutines.CoroutineScope
1212
import kotlinx.coroutines.Dispatchers
13+
import okhttp3.Call
1314
import okhttp3.OkHttpClient
1415
import java.io.File
1516

@@ -54,7 +55,7 @@ public interface VariationsRepository {
5455
* @param logger to log errors and debug information.
5556
* @param failFast If `true`, [getVariation] will throw exceptions if the [VariationsRepository] is not initialized or if the [Experiment] is not found.
5657
* @param cacheDir Directory to use for caching the [Assignments]. This directory should be private to the application.
57-
* @param okhttpClient to use for network operations. Defaults to a new instance of [OkHttpClient].
58+
* @param callFactory to use for network operations. Defaults to a new instance of [OkHttpClient].
5859
* @param coroutineScope to use for async operations. Preferably a [CoroutineScope] that is tied to the lifecycle of the application.
5960
* @param dispatcher to use for async I/O operations. Defaults to [Dispatchers.IO].
6061
*/
@@ -64,7 +65,7 @@ public interface VariationsRepository {
6465
logger: ExperimentLogger,
6566
failFast: Boolean,
6667
cacheDir: File,
67-
okhttpClient: OkHttpClient = OkHttpClient(),
68+
callFactory: Call.Factory = OkHttpClient(),
6869
coroutineScope: CoroutineScope,
6970
dispatcher: CoroutineDispatcher = Dispatchers.IO,
7071
): VariationsRepository {
@@ -76,7 +77,7 @@ public interface VariationsRepository {
7677
failFast = failFast,
7778
assignmentsValidator = AssignmentsValidator(SystemClock()),
7879
repository = AssignmentsRepository(
79-
ExperimentRestClient(dispatcher = dispatcher, okHttpClient = okhttpClient),
80+
ExperimentRestClient(dispatcher = dispatcher, callFactory = callFactory),
8081
FileBasedCache(
8182
cacheDir,
8283
dispatcher = dispatcher,

experimentation/src/main/java/com/automattic/android/experimentation/remote/ExperimentRestClient.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import com.automattic.android.experimentation.remote.AssignmentsDtoMapper.toAssi
77
import com.squareup.moshi.Moshi
88
import kotlinx.coroutines.CoroutineDispatcher
99
import kotlinx.coroutines.withContext
10-
import okhttp3.OkHttpClient
10+
import okhttp3.Call
1111
import okhttp3.Request
1212
import java.io.IOException
1313

1414
internal class ExperimentRestClient(
15-
private val okHttpClient: OkHttpClient,
15+
private val callFactory: Call.Factory,
1616
private val moshi: Moshi = Moshi.Builder().build(),
1717
private val jsonAdapter: AssignmentsDtoJsonAdapter = AssignmentsDtoJsonAdapter(moshi),
1818
private val urlBuilder: UrlBuilder = ExPlatUrlBuilder(),
@@ -36,7 +36,7 @@ internal class ExperimentRestClient(
3636

3737
return withContext(dispatcher) {
3838
try {
39-
okHttpClient.newCall(request).execute().use { response ->
39+
callFactory.newCall(request).execute().use { response ->
4040
if (!response.isSuccessful) {
4141
Result.failure(IOException("Unexpected code $response"))
4242
} else {

experimentation/src/test/java/com/automattic/android/experimentation/ExPlatTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ internal class ExPlatTest {
192192
urlBuilder = MockWebServerUrlBuilder(ExPlatUrlBuilder(), server),
193193
dispatcher = dispatcher,
194194
clock = clock,
195-
okHttpClient = OkHttpClient(),
195+
callFactory = OkHttpClient(),
196196
)
197197
tempCache = FileBasedCache(
198198
createTempDirectory().toFile(),

experimentation/src/test/java/com/automattic/android/experimentation/remote/ExperimentRestClientTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ internal class ExperimentRestClientTest {
103103
urlBuilder = urlBuilder,
104104
clock = { TEST_TIMESTAMP },
105105
dispatcher = StandardTestDispatcher(scope.testScheduler),
106-
okHttpClient = OkHttpClient(),
106+
callFactory = OkHttpClient(),
107107
)
108108

109109
companion object {

0 commit comments

Comments
 (0)