From 4487697971dff1c477293f7735dbf93ffc71598a Mon Sep 17 00:00:00 2001 From: Nicola Corti Date: Tue, 27 May 2025 09:51:24 -0700 Subject: [PATCH] Migrate OkHttpCompat to Kotlin (#51625) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/51625 This just migrates this class to Kotlin, which is also the last class in this package. Changelog: [Internal] [Changed] - Reviewed By: rshest Differential Revision: D75448161 fbshipit-source-id: d5457dd8017fd459d166d2945ff440c303943db2 --- .../ReactAndroid/api/ReactAndroid.api | 8 ++-- .../react/modules/network/OkHttpCompat.java | 38 ------------------- .../react/modules/network/OkHttpCompat.kt | 31 +++++++++++++++ 3 files changed, 35 insertions(+), 42 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpCompat.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpCompat.kt diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index ac7e0366e7c..51896fce132 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -3025,10 +3025,10 @@ public final class com/facebook/react/modules/network/OkHttpClientProvider { public static final fun setOkHttpClientFactory (Lcom/facebook/react/modules/network/OkHttpClientFactory;)V } -public class com/facebook/react/modules/network/OkHttpCompat { - public fun ()V - public static fun getCookieJarContainer (Lokhttp3/OkHttpClient;)Lcom/facebook/react/modules/network/CookieJarContainer; - public static fun getHeadersFromMap (Ljava/util/Map;)Lokhttp3/Headers; +public final class com/facebook/react/modules/network/OkHttpCompat { + public static final field INSTANCE Lcom/facebook/react/modules/network/OkHttpCompat; + public final fun getCookieJarContainer (Lokhttp3/OkHttpClient;)Lcom/facebook/react/modules/network/CookieJarContainer; + public final fun getHeadersFromMap (Ljava/util/Map;)Lokhttp3/Headers; } public abstract interface class com/facebook/react/modules/network/ProgressListener { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpCompat.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpCompat.java deleted file mode 100644 index b0bc0d243a9..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpCompat.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.modules.network; - -import androidx.annotation.Nullable; -import com.facebook.infer.annotation.Nullsafe; -import java.util.Collections; -import java.util.Map; -import okhttp3.Headers; -import okhttp3.OkHttpClient; - -/** - * Helper class that provides wrappers for compatibility between different OkHttp versions. - * - *

This is required for Kotlin code compatibility, in particular, therefore if you are going to - * migrate this file to Kotlin, please first ensure that there is no OkHttp API discrepancy between - * different RN platform environments, and then consider getting rid of this compat layer - * altogether. - */ -@Nullsafe(Nullsafe.Mode.LOCAL) -public class OkHttpCompat { - public static CookieJarContainer getCookieJarContainer(OkHttpClient client) { - return (CookieJarContainer) client.cookieJar(); - } - - public static Headers getHeadersFromMap(@Nullable Map headers) { - if (headers == null) { - return Headers.of(Collections.emptyMap()); - } else { - return Headers.of(headers); - } - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpCompat.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpCompat.kt new file mode 100644 index 00000000000..e4e0eaa8647 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpCompat.kt @@ -0,0 +1,31 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +@file:Suppress("DEPRECATION_ERROR") // Conflicting okhttp versions + +package com.facebook.react.modules.network + +import com.facebook.infer.annotation.Nullsafe +import okhttp3.Headers +import okhttp3.OkHttpClient + +/** + * Helper class that provides wrappers for compatibility between different OkHttp versions. + * + * This is required for Kotlin code compatibility, in particular, therefore if you are going to + * migrate this file to Kotlin, please first ensure that there is no OkHttp API discrepancy between + * different RN platform environments, and then consider getting rid of this compat layer + * altogether. + */ +@Nullsafe(Nullsafe.Mode.LOCAL) +public object OkHttpCompat { + public fun getCookieJarContainer(client: OkHttpClient): CookieJarContainer = + client.cookieJar() as CookieJarContainer + + public fun getHeadersFromMap(headers: Map?): Headers = + Headers.of(headers ?: emptyMap()) +}