Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow app to init an OkHttpClientProvider.IProvider with custom settings(FrescoModule and NetworkingModule) #14675

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,58 @@
*/
public class OkHttpClientProvider {

// Centralized OkHttpClient for all networking requests.
private static @Nullable OkHttpClient sClient;
public interface IProvider {
OkHttpClient create();

public static OkHttpClient getOkHttpClient() {
if (sClient == null) {
sClient = createClient();
OkHttpClient get();
}

public static class DefaultProvider implements OkHttpClientProvider.IProvider {
// Centralized OkHttpClient for all networking requests.
private @Nullable OkHttpClient client;

@Override
public OkHttpClient create() {
// No timeouts by default
OkHttpClient.Builder client = new OkHttpClient.Builder()
.connectTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS)
.cookieJar(new ReactCookieJarContainer());

return enableTls12OnPreLollipop(client).build();
}

@Override
public OkHttpClient get() {
if (client == null) {
client = create();
}
return client;
}
return sClient;
}

// okhttp3 OkHttpClient is immutable
// This allows app to init an OkHttpClient with custom settings.
public static void replaceOkHttpClient(OkHttpClient client) {
sClient = client;

private static @Nullable OkHttpClientProvider.IProvider sPprovider;

private static OkHttpClientProvider.IProvider getProvider() {
if (sPprovider == null) {
sPprovider = new DefaultProvider();
}
return sPprovider;
}

// okhttp3 OkHttpClientProvider.IProvider is immutable
// This allows app to init an OkHttpClientProvider.IProvider with custom settings.
public static void replaceProvider(OkHttpClientProvider.IProvider provider) {
sPprovider = provider;
}

public static OkHttpClient getOkHttpClient() {
return getProvider().get();
}

public static OkHttpClient createClient() {
// No timeouts by default
OkHttpClient.Builder client = new OkHttpClient.Builder()
.connectTimeout(0, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS)
.cookieJar(new ReactCookieJarContainer());

return enableTls12OnPreLollipop(client).build();
return getProvider().create();
}

/*
Expand Down