Skip to content

Commit 3ddd237

Browse files
authored
fix(network): enabled directly adding interceptor (#295)
* fix(network): enabled directly adding interceptor * no-op fix
1 parent 4e05af6 commit 3ddd237

File tree

7 files changed

+39
-19
lines changed

7 files changed

+39
-19
lines changed

pluto-plugins/plugins/network/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ dependencies {
3939
Add interceptor in your OkHttp Client Builder
4040
```kotlin
4141
val client = OkHttpClient.Builder()
42-
addPlutoOkhttpInterceptor()
42+
.addInterceptor(PlutoOkhttpInterceptor)
4343
.build()
4444
```
4545
<br>

pluto-plugins/plugins/network/interceptor-ktor/lib/src/main/kotlin/com/pluto/plugins/network/ktor/PlutoKtorHelper.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pluto.plugins.network.ktor
22

3+
import androidx.annotation.Keep
34
import com.pluto.plugins.network.intercept.NetworkInterceptor
45
import com.pluto.plugins.network.ktor.internal.KtorRequestConverter.convert
56
import com.pluto.plugins.network.ktor.internal.KtorResponseConverter.convert
@@ -38,6 +39,7 @@ fun HttpClient.addPlutoKtorInterceptor() {
3839
}
3940
}
4041

42+
@Keep
4143
class PlutoKtorInterceptor {
4244
companion object : HttpClientPlugin<Unit, PlutoKtorInterceptor> {
4345

pluto-plugins/plugins/network/interceptor-okhttp/lib-no-op/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ android {
4848
}
4949

5050
dependencies {
51+
implementation "androidx.core:core-ktx:$androidXCoreVersion"
5152
implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
5253
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.pluto.plugins.network.okhttp
2+
3+
import androidx.annotation.Keep
4+
import okhttp3.Interceptor
5+
import okhttp3.Response
6+
7+
@SuppressWarnings("UtilityClassWithPublicConstructor")
8+
@Keep
9+
class PlutoOkhttpInterceptor {
10+
companion object : Interceptor {
11+
12+
override fun intercept(chain: Interceptor.Chain): Response {
13+
val request = chain.request()
14+
return chain.proceed(request)
15+
}
16+
}
17+
}

pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoOkhttpHelper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package com.pluto.plugins.network.okhttp
33
import okhttp3.OkHttpClient
44
import javax.net.SocketFactory
55

6+
@Deprecated("add PlutoHttpInterceptor directly to OkHttpClient instead")
67
fun OkHttpClient.Builder.addPlutoOkhttpInterceptor(): OkHttpClient.Builder {
78
// todo add okhttp settings block here
89
socketFactory(SocketFactory.getDefault())
9-
addInterceptor(PlutoInterceptor())
10+
addInterceptor(PlutoOkhttpInterceptor)
1011
return this
1112
}

pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoInterceptor.kt renamed to pluto-plugins/plugins/network/interceptor-okhttp/lib/src/main/kotlin/com/pluto/plugins/network/okhttp/PlutoOkhttpInterceptor.kt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,22 @@ import okio.buffer
1818
import java.io.IOException
1919

2020
@Keep
21-
internal class PlutoInterceptor : Interceptor {
21+
class PlutoOkhttpInterceptor {
22+
companion object : Interceptor {
23+
private const val NAME = "Okhttp"
2224

23-
override fun intercept(chain: Interceptor.Chain): Response {
24-
val request = chain.request()
25-
val networkInterceptor = NetworkInterceptor.intercept(request.convert(), NetworkInterceptor.Option(NAME))
26-
val response: Response = try {
27-
val builder = request.newBuilder().url(networkInterceptor.actualOrMockRequestUrl)
28-
chain.proceed(builder.build())
29-
} catch (e: IOException) {
30-
networkInterceptor.onError(e)
31-
throw e
25+
override fun intercept(chain: Interceptor.Chain): Response {
26+
val request = chain.request()
27+
val networkInterceptor = NetworkInterceptor.intercept(request.convert(), NetworkInterceptor.Option(NAME))
28+
val response: Response = try {
29+
val builder = request.newBuilder().url(networkInterceptor.actualOrMockRequestUrl)
30+
chain.proceed(builder.build())
31+
} catch (e: IOException) {
32+
networkInterceptor.onError(e)
33+
throw e
34+
}
35+
return response.processBody { networkInterceptor.onResponse(it) }
3236
}
33-
return response.processBody { networkInterceptor.onResponse(it) }
34-
}
35-
36-
companion object {
37-
private const val NAME = "Okhttp"
3837
}
3938
}
4039

sample/src/main/java/com/sampleapp/functions/network/internal/okhttp/Network.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.sampleapp.functions.network.internal.okhttp
22

3-
import com.pluto.plugins.network.okhttp.addPlutoOkhttpInterceptor
3+
import com.pluto.plugins.network.okhttp.PlutoOkhttpInterceptor
44
import java.util.concurrent.TimeUnit
55
import okhttp3.OkHttpClient
66
import okhttp3.logging.HttpLoggingInterceptor
@@ -23,7 +23,7 @@ object Network {
2323
.readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
2424
// .addInterceptor(GzipRequestInterceptor())
2525
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
26-
.addPlutoOkhttpInterceptor()
26+
.addInterceptor(PlutoOkhttpInterceptor)
2727
.build()
2828

2929
fun <T> getService(cls: Class<T>): T {

0 commit comments

Comments
 (0)