diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientFactory.java b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientFactory.java new file mode 100644 index 00000000000000..9e417d00fdc7bf --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientFactory.java @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.modules.network; + +import okhttp3.OkHttpClient; + +public interface OkHttpClientFactory { + OkHttpClient createNewNetworkModuleClient(); +}; diff --git a/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java index 5b82aa027a3fad..c90e908ad1c931 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java +++ b/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java @@ -32,6 +32,13 @@ public class OkHttpClientProvider { // Centralized OkHttpClient for all networking requests. private static @Nullable OkHttpClient sClient; + // User-provided OkHttpClient factory + private static @Nullable OkHttpClientFactory sFactory; + + public static void setOkHttpClientFactory(OkHttpClientFactory factory) { + sFactory = factory; + } + public static OkHttpClient getOkHttpClient() { if (sClient == null) { sClient = createClient(); @@ -46,6 +53,10 @@ public static void replaceOkHttpClient(OkHttpClient client) { } public static OkHttpClient createClient() { + if (sFactory != null) { + return sFactory.createNewNetworkModuleClient(); + } + // No timeouts by default OkHttpClient.Builder client = new OkHttpClient.Builder() .connectTimeout(0, TimeUnit.MILLISECONDS)