mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-01-12 22:50:10 +08:00
Add the ability to customize the http client in networking native module
Summary: Add the ability to build on top of the OkHttpClient.Builder by defining a lambda that gets called on network requests [Android][Added] - Public method `setCustomClientBuilder` to the native `com.facebook.react.modules.network.NetworkingModule`, that allows customizing the OkHttpClient per-request for greater control over HTTP requests. Reviewed By: hramos Differential Revision: D14288613 fbshipit-source-id: 34df0ff3e18eeea1d565ccfcf97408379900120b
This commit is contained in:
committed by
Facebook Github Bot
parent
b312543d3c
commit
81fcaa151d
@@ -116,6 +116,8 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
private static final int CHUNK_TIMEOUT_NS = 100 * 1000000; // 100ms
|
||||
private static final int MAX_CHUNK_SIZE_BETWEEN_FLUSHES = 8 * 1024; // 8K
|
||||
|
||||
private static @Nullable CustomClientBuilder customClientBuilder = null;
|
||||
|
||||
private final OkHttpClient mClient;
|
||||
private final ForwardingCookieHandler mCookieHandler;
|
||||
private final @Nullable String mDefaultUserAgent;
|
||||
@@ -188,6 +190,20 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
this(context, defaultUserAgent, OkHttpClientProvider.createClient(context), null);
|
||||
}
|
||||
|
||||
public static void setCustomClientBuilder(CustomClientBuilder ccb) {
|
||||
customClientBuilder = ccb;
|
||||
}
|
||||
|
||||
public static interface CustomClientBuilder {
|
||||
public void apply(OkHttpClient.Builder builder);
|
||||
}
|
||||
|
||||
private static void applyCustomBuilder(OkHttpClient.Builder builder) {
|
||||
if (customClientBuilder != null) {
|
||||
customClientBuilder.apply(builder);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize() {
|
||||
mCookieJarContainer.setCookieJar(new JavaNetCookieJar(mCookieHandler));
|
||||
@@ -301,6 +317,8 @@ public final class NetworkingModule extends ReactContextBaseJavaModule {
|
||||
|
||||
OkHttpClient.Builder clientBuilder = mClient.newBuilder();
|
||||
|
||||
applyCustomBuilder(clientBuilder);
|
||||
|
||||
if (!withCredentials) {
|
||||
clientBuilder.cookieJar(CookieJar.NO_COOKIES);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user