Skip to content

Commit

Permalink
Allow configuring Call.Factory instead of OkHttpClient (#3137) (#2987)
Browse files Browse the repository at this point in the history
Co-authored-by: Bart Louwers <[email protected]>
  • Loading branch information
yschimke and louwers authored Nov 4, 2024
1 parent b58c0c2 commit 286f9d0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
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

0 comments on commit 286f9d0

Please sign in to comment.