From 286f9d0adc32de5d788869e13997bea740509d8e Mon Sep 17 00:00:00 2001 From: Yuri Schimke Date: Mon, 4 Nov 2024 13:06:52 +0000 Subject: [PATCH] Allow configuring Call.Factory instead of OkHttpClient (#3137) (#2987) Co-authored-by: Bart Louwers --- .../maplibre/android/module/http/HttpRequestImpl.java | 11 ++++------- .../maplibre/android/module/http/HttpRequestUtil.java | 8 ++++---- .../android/module/http/HttpRequestUtilTest.kt | 4 ++-- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/module/http/HttpRequestImpl.java b/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/module/http/HttpRequestImpl.java index 183e90e2aa7..275e363cd21 100644 --- a/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/module/http/HttpRequestImpl.java +++ b/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/module/http/HttpRequestImpl.java @@ -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; @@ -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; @@ -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 { diff --git a/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/module/http/HttpRequestUtil.java b/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/module/http/HttpRequestUtil.java index 0750a96dfbd..1c3b268246a 100644 --- a/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/module/http/HttpRequestUtil.java +++ b/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android/module/http/HttpRequestUtil.java @@ -3,7 +3,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import okhttp3.OkHttpClient; +import okhttp3.Call; import okio.Buffer; /** @@ -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. *

* This configuration survives across mapView instances. * Reset the OkHttpClient to the default by passing null as parameter. *

* - * @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); } diff --git a/platform/android/MapLibreAndroid/src/test/java/org/maplibre/android/module/http/HttpRequestUtilTest.kt b/platform/android/MapLibreAndroid/src/test/java/org/maplibre/android/module/http/HttpRequestUtilTest.kt index 432d23b30bf..7377762e120 100644 --- a/platform/android/MapLibreAndroid/src/test/java/org/maplibre/android/module/http/HttpRequestUtilTest.kt +++ b/platform/android/MapLibreAndroid/src/test/java/org/maplibre/android/module/http/HttpRequestUtilTest.kt @@ -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 @@ -18,7 +18,7 @@ class HttpRequestUtilTest { assertEquals(HttpRequestImpl.DEFAULT_CLIENT, HttpRequestImpl.client) - val httpMock = mockk() + val httpMock = mockk() HttpRequestUtil.setOkHttpClient(httpMock) assertEquals( "Http client should have set to the mocked client",