Skip to content

Commit

Permalink
fix(network): enabled directly adding interceptor (#295)
Browse files Browse the repository at this point in the history
* fix(network): enabled directly adding interceptor

* no-op fix
  • Loading branch information
srtvprateek authored Nov 28, 2023
1 parent 4e05af6 commit 3ddd237
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pluto-plugins/plugins/network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dependencies {
Add interceptor in your OkHttp Client Builder
```kotlin
val client = OkHttpClient.Builder()
addPlutoOkhttpInterceptor()
.addInterceptor(PlutoOkhttpInterceptor)
.build()
```
<br>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -38,6 +39,7 @@ fun HttpClient.addPlutoKtorInterceptor() {
}
}

@Keep
class PlutoKtorInterceptor {
companion object : HttpClientPlugin<Unit, PlutoKtorInterceptor> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,6 @@ android {
}

dependencies {
implementation "androidx.core:core-ktx:$androidXCoreVersion"
implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,7 +23,7 @@ object Network {
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
// .addInterceptor(GzipRequestInterceptor())
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.addPlutoOkhttpInterceptor()
.addInterceptor(PlutoOkhttpInterceptor)
.build()

fun <T> getService(cls: Class<T>): T {
Expand Down

0 comments on commit 3ddd237

Please sign in to comment.