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 configuring Call.Factory instead of OkHttpClient (#3137) #2987

Merged
merged 4 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
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 @@ -22,6 +22,7 @@
import java.net.ProtocolException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Objects;

import javax.net.ssl.SSLException;

Expand Down Expand Up @@ -51,7 +52,7 @@ public class HttpRequestImpl implements HttpRequest {
static final OkHttpClient DEFAULT_CLIENT = new OkHttpClient.Builder().dispatcher(getDispatcher()).build();

@VisibleForTesting
static OkHttpClient client = DEFAULT_CLIENT;
static Call.Factory client = DEFAULT_CLIENT;

private Call call;

Expand Down Expand Up @@ -105,12 +106,8 @@ public static void enableLog(boolean enabled) {
HttpLogger.logEnabled = enabled;
}

public static void setOkHttpClient(@Nullable OkHttpClient okHttpClient) {
if (okHttpClient != null) {
HttpRequestImpl.client = okHttpClient;
} else {
HttpRequestImpl.client = DEFAULT_CLIENT;
}
public static void setOkHttpClient(@Nullable Call.Factory client) {
HttpRequestImpl.client = Objects.requireNonNullElse(client, DEFAULT_CLIENT);
}

private static class OkHttpCallback implements Callback {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import okhttp3.OkHttpClient;
import okhttp3.Call;
import okio.Buffer;

/**
Expand Down Expand Up @@ -39,15 +39,15 @@ public static void setPrintRequestUrlOnFailure(boolean enabled) {
}

/**
* Set the OkHttpClient used for requesting map resources.
* Set the OkHttp Call.Factory used for requesting map resources.
* <p>
* This configuration survives across mapView instances.
* Reset the OkHttpClient to the default by passing null as parameter.
* </p>
*
* @param client the OkHttpClient
* @param client the OkHttp Call.Factory, typically OkHttpClient.
*/
public static void setOkHttpClient(@Nullable OkHttpClient client) {
public static void setOkHttpClient(@Nullable Call.Factory client) {
HttpRequestImpl.setOkHttpClient(client);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.maplibre.android.module.http
import org.maplibre.android.MapLibreInjector
import org.maplibre.android.utils.ConfigUtils
import io.mockk.mockk
import okhttp3.OkHttpClient
import okhttp3.Call
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -18,7 +18,7 @@ class HttpRequestUtilTest {

assertEquals(HttpRequestImpl.DEFAULT_CLIENT, HttpRequestImpl.client)

val httpMock = mockk<OkHttpClient>()
val httpMock = mockk<Call.Factory>()
HttpRequestUtil.setOkHttpClient(httpMock)
assertEquals(
"Http client should have set to the mocked client",
Expand Down
Loading