From 31c5ca2144c63207a90f17f83e0fd78a3a3c078f Mon Sep 17 00:00:00 2001 From: srtvprateek Date: Tue, 28 Nov 2023 12:02:34 +0530 Subject: [PATCH 1/2] fix(network): enabled directly adding interceptor --- pluto-plugins/plugins/network/README.md | 2 +- .../plugins/network/ktor/PlutoKtorHelper.kt | 2 ++ .../network/okhttp/PlutoOkhttpHelper.kt | 3 +- ...terceptor.kt => PlutoOkhttpInterceptor.kt} | 29 +++++++++---------- .../network/internal/okhttp/Network.kt | 4 +-- 5 files changed, 21 insertions(+), 19 deletions(-) rename pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/{PlutoInterceptor.kt => PlutoOkhttpInterceptor.kt} (72%) diff --git a/pluto-plugins/plugins/network/README.md b/pluto-plugins/plugins/network/README.md index 1f9896f8..de51b396 100644 --- a/pluto-plugins/plugins/network/README.md +++ b/pluto-plugins/plugins/network/README.md @@ -39,7 +39,7 @@ dependencies { Add interceptor in your OkHttp Client Builder ```kotlin val client = OkHttpClient.Builder() - addPlutoOkhttpInterceptor() + .addInterceptor(PlutoOkhttpInterceptor) .build() ```
diff --git a/pluto-plugins/plugins/network/interceptor-ktor/lib/src/main/kotlin/com/pluto/plugins/network/ktor/PlutoKtorHelper.kt b/pluto-plugins/plugins/network/interceptor-ktor/lib/src/main/kotlin/com/pluto/plugins/network/ktor/PlutoKtorHelper.kt index 73a37f3c..71264a6f 100644 --- a/pluto-plugins/plugins/network/interceptor-ktor/lib/src/main/kotlin/com/pluto/plugins/network/ktor/PlutoKtorHelper.kt +++ b/pluto-plugins/plugins/network/interceptor-ktor/lib/src/main/kotlin/com/pluto/plugins/network/ktor/PlutoKtorHelper.kt @@ -1,5 +1,6 @@ package com.pluto.plugins.network.ktor +import androidx.annotation.Keep import com.pluto.plugins.network.intercept.NetworkInterceptor import com.pluto.plugins.network.ktor.internal.KtorRequestConverter.convert import com.pluto.plugins.network.ktor.internal.KtorResponseConverter.convert @@ -38,6 +39,7 @@ fun HttpClient.addPlutoKtorInterceptor() { } } +@Keep class PlutoKtorInterceptor { companion object : HttpClientPlugin { diff --git a/pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoOkhttpHelper.kt b/pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoOkhttpHelper.kt index 4d0e1bad..b13f588e 100644 --- a/pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoOkhttpHelper.kt +++ b/pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoOkhttpHelper.kt @@ -3,9 +3,10 @@ package com.pluto.plugins.network.okhttp import okhttp3.OkHttpClient import javax.net.SocketFactory +@Deprecated("add PlutoHttpInterceptor directly to OkHttpClient instead") fun OkHttpClient.Builder.addPlutoOkhttpInterceptor(): OkHttpClient.Builder { // todo add okhttp settings block here socketFactory(SocketFactory.getDefault()) - addInterceptor(PlutoInterceptor()) + addInterceptor(PlutoOkhttpInterceptor) return this } diff --git a/pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoInterceptor.kt b/pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoOkhttpInterceptor.kt similarity index 72% rename from pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoInterceptor.kt rename to pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoOkhttpInterceptor.kt index 3905b191..fd85306d 100644 --- a/pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoInterceptor.kt +++ b/pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoOkhttpInterceptor.kt @@ -18,23 +18,22 @@ import okio.buffer import java.io.IOException @Keep -internal class PlutoInterceptor : Interceptor { +class PlutoOkhttpInterceptor { + companion object : Interceptor { + private const val NAME = "Okhttp" - override fun intercept(chain: Interceptor.Chain): Response { - val request = chain.request() - val networkInterceptor = NetworkInterceptor.intercept(request.convert(), NetworkInterceptor.Option(NAME)) - val response: Response = try { - val builder = request.newBuilder().url(networkInterceptor.actualOrMockRequestUrl) - chain.proceed(builder.build()) - } catch (e: IOException) { - networkInterceptor.onError(e) - throw e + override fun intercept(chain: Interceptor.Chain): Response { + val request = chain.request() + val networkInterceptor = NetworkInterceptor.intercept(request.convert(), NetworkInterceptor.Option(NAME)) + val response: Response = try { + val builder = request.newBuilder().url(networkInterceptor.actualOrMockRequestUrl) + chain.proceed(builder.build()) + } catch (e: IOException) { + networkInterceptor.onError(e) + throw e + } + return response.processBody { networkInterceptor.onResponse(it) } } - return response.processBody { networkInterceptor.onResponse(it) } - } - - companion object { - private const val NAME = "Okhttp" } } diff --git a/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/Network.kt b/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/Network.kt index 47e1246a..4ac1a0d8 100644 --- a/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/Network.kt +++ b/sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/Network.kt @@ -1,6 +1,6 @@ package com.sampleapp.functions.network.internal.okhttp -import com.pluto.plugins.network.okhttp.addPlutoOkhttpInterceptor +import com.pluto.plugins.network.okhttp.PlutoOkhttpInterceptor import java.util.concurrent.TimeUnit import okhttp3.OkHttpClient import okhttp3.logging.HttpLoggingInterceptor @@ -23,7 +23,7 @@ object Network { .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS) // .addInterceptor(GzipRequestInterceptor()) .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) - .addPlutoOkhttpInterceptor() + .addInterceptor(PlutoOkhttpInterceptor) .build() fun getService(cls: Class): T { From f00c42e3cd6c4f88ee830abe3faf57cd57c2ea8a Mon Sep 17 00:00:00 2001 From: srtvprateek Date: Tue, 28 Nov 2023 12:17:59 +0530 Subject: [PATCH 2/2] no-op fix --- .../interceptor-okhttp/lib-no-op/build.gradle | 1 + .../network/okhttp/PlutoOkhttpInterceptor.kt | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) create mode 100644 pluto-plugins/plugins/network/interceptor-okhttp/lib-no-op/src/main/java/com/pluto/plugins/network/okhttp/PlutoOkhttpInterceptor.kt diff --git a/pluto-plugins/plugins/network/interceptor-okhttp/lib-no-op/build.gradle b/pluto-plugins/plugins/network/interceptor-okhttp/lib-no-op/build.gradle index 0afbaa18..3793ffa0 100644 --- a/pluto-plugins/plugins/network/interceptor-okhttp/lib-no-op/build.gradle +++ b/pluto-plugins/plugins/network/interceptor-okhttp/lib-no-op/build.gradle @@ -48,5 +48,6 @@ android { } dependencies { + implementation "androidx.core:core-ktx:$androidXCoreVersion" implementation "com.squareup.okhttp3:okhttp:$okhttpVersion" } \ No newline at end of file diff --git a/pluto-plugins/plugins/network/interceptor-okhttp/lib-no-op/src/main/java/com/pluto/plugins/network/okhttp/PlutoOkhttpInterceptor.kt b/pluto-plugins/plugins/network/interceptor-okhttp/lib-no-op/src/main/java/com/pluto/plugins/network/okhttp/PlutoOkhttpInterceptor.kt new file mode 100644 index 00000000..6b322e5c --- /dev/null +++ b/pluto-plugins/plugins/network/interceptor-okhttp/lib-no-op/src/main/java/com/pluto/plugins/network/okhttp/PlutoOkhttpInterceptor.kt @@ -0,0 +1,17 @@ +package com.pluto.plugins.network.okhttp + +import androidx.annotation.Keep +import okhttp3.Interceptor +import okhttp3.Response + +@SuppressWarnings("UtilityClassWithPublicConstructor") +@Keep +class PlutoOkhttpInterceptor { + companion object : Interceptor { + + override fun intercept(chain: Interceptor.Chain): Response { + val request = chain.request() + return chain.proceed(request) + } + } +}