Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network): enabled directly adding interceptor #295

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading