Skip to content

Commit 22efd95

Browse files
cdlewisfacebook-github-bot
authored andcommitted
Add back ability to customise OkHttp client
Summary: Prior to 0a71f48, users could customise the OkHttp client used by React Native on Android by calling replaceOkHttpClient in OkHttpClientProvider. This functionality has a variety of legitimate applications from changing connection timeouts or pool size to Stetho integration. The challenge is to add back support for replacing the client without causing a breaking change or reintroducing the problems olegbl sought to address in his original commit. Introducing a client factory archives these aims, it adds a new, backwards compatible interface and is called each time a client is requested rather than re-using the same instance (unless you explicitly want this behaviour, in which case you could replicate it using a static class property inside your custom factory). A number of PRs have been opened to add this functionality: #14675, #14068. I don't have a lot of Java experience so I'm open to better/more idiomatic ways to achieve this :) Create React Native application and set a custom factory in the constructor, e.g. `OkHttpClientProvider.setOkHttpClientFactory(new CustomNetworkModule());` Where a custom factory would look like: ``` class CustomNetworkModule implements OkHttpClientFactory { public OkHttpClient createNewNetworkModuleClient() { return new OkHttpClient.Builder().build(); } } ``` Remove the existing replace client method to prevent accident use and alert existing users that its functionality has changed: #16972 [Android] [Minor] [Networking] - | Provide interface for customising the OkHttp client used by React Native | Closes #17237 Differential Revision: D6837734 Pulled By: hramos fbshipit-source-id: 81e63df7716e6f9039ea12e99233f6336c6dd7ef
1 parent ef4214a commit 22efd95

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
package com.facebook.react.modules.network;
11+
12+
import okhttp3.OkHttpClient;
13+
14+
public interface OkHttpClientFactory {
15+
OkHttpClient createNewNetworkModuleClient();
16+
};

Diff for: ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java

+11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public class OkHttpClientProvider {
3232
// Centralized OkHttpClient for all networking requests.
3333
private static @Nullable OkHttpClient sClient;
3434

35+
// User-provided OkHttpClient factory
36+
private static @Nullable OkHttpClientFactory sFactory;
37+
38+
public static void setOkHttpClientFactory(OkHttpClientFactory factory) {
39+
sFactory = factory;
40+
}
41+
3542
public static OkHttpClient getOkHttpClient() {
3643
if (sClient == null) {
3744
sClient = createClient();
@@ -46,6 +53,10 @@ public static void replaceOkHttpClient(OkHttpClient client) {
4653
}
4754

4855
public static OkHttpClient createClient() {
56+
if (sFactory != null) {
57+
return sFactory.createNewNetworkModuleClient();
58+
}
59+
4960
// No timeouts by default
5061
OkHttpClient.Builder client = new OkHttpClient.Builder()
5162
.connectTimeout(0, TimeUnit.MILLISECONDS)

0 commit comments

Comments
 (0)