From 5989c8388cfbab765467de808d7f07bda0a54c7e Mon Sep 17 00:00:00 2001 From: Chris Lewis Date: Tue, 30 Jan 2018 11:39:20 -0800 Subject: [PATCH] Add back ability to customise OkHttp client Summary: Prior to 0a71f48b1349ed09bcb6e76ba9ff8eb388518b15, 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: https://github.com/facebook/react-native/pull/14675, https://github.com/facebook/react-native/pull/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: https://github.com/facebook/react-native/pull/16972 [Android] [Minor] [Networking] - | Provide interface for customising the OkHttp client used by React Native | Closes https://github.com/facebook/react-native/pull/17237 Differential Revision: D6837734 Pulled By: hramos fbshipit-source-id: 81e63df7716e6f9039ea12e99233f6336c6dd7ef --- .../modules/network/OkHttpClientFactory.java | 16 ++++++++++++++++ .../modules/network/OkHttpClientProvider.java | 11 +++++++++++ 2 files changed, 27 insertions(+) create mode 100644 ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientFactory.java 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)