Skip to content

Commit

Permalink
Merge pull request #4159 from square/jw.conditional-interceptor.2024-…
Browse files Browse the repository at this point in the history
…05-29

Conditionally-logging interceptor sample
  • Loading branch information
JakeWharton committed May 29, 2024
2 parents 348ac8f + 8c2b181 commit e104ab6
Show file tree
Hide file tree
Showing 26 changed files with 115 additions and 30 deletions.
7 changes: 4 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ incap-processor = { module = "net.ltgt.gradle.incap:incap-processor", version.re
autoService-annotations = { module = "com.google.auto.service:auto-service-annotations", version.ref = "autoService" }
autoService-compiler = { module = "com.google.auto.service:auto-service", version.ref = "autoService" }

kotlinCoroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version = "1.8.1" }
kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version = "1.8.1" }
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
kotlinx-serialization-proto = { module = "org.jetbrains.kotlinx:kotlinx-serialization-protobuf", version.ref = "kotlinx-serialization" }
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
mockwebserver = { module = "com.squareup.okhttp3:mockwebserver", version.ref = "okhttp" }
okhttp-client = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
okhttp-loggingInterceptor = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "okhttp" }
okhttp-mockwebserver = { module = "com.squareup.okhttp3:mockwebserver", version.ref = "okhttp" }
junit = { module = "junit:junit", version = "4.13.2" }
truth = "com.google.truth:truth:1.4.2"
guava = { module = "com.google.guava:guava", version = "33.2.0-jre" }
Expand Down
2 changes: 1 addition & 1 deletion retrofit-adapters/guava/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {

testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-adapters/java8/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {
testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.guava
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-adapters/rxjava/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.guava
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-adapters/rxjava2/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies {
testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.guava
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-adapters/rxjava3/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies {
testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.guava
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-adapters/scala/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.guava
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/gson/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {

testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/guava/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {

testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/jackson/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {

testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
testImplementation libs.jacksonDataformatCbor
}

Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/java8/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies {

testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
testImplementation libs.findBugsAnnotations
}

Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/jaxb/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
testImplementation libs.jaxbImpl
testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
testImplementation libs.findBugsAnnotations
}

Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/jaxb3/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {
testImplementation libs.jaxb3Impl
testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
testImplementation libs.findBugsAnnotations
}

Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/kotlinx-serialization/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {
api libs.kotlinx.serialization.core

testImplementation libs.junit
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
testImplementation libs.kotlinx.serialization.proto
testImplementation libs.kotlinx.serialization.json
}
2 changes: 1 addition & 1 deletion retrofit-converters/moshi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {

testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/protobuf/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dependencies {

testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/scalars/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dependencies {

testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/simplexml/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {

testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-converters/wire/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies {

testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit-mock/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies {
testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.kotlin.stdLib
testImplementation libs.kotlinCoroutines
testImplementation libs.kotlinx.coroutines
}

jar {
Expand Down
2 changes: 1 addition & 1 deletion retrofit/android-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ dependencies {
androidTestImplementation projects.retrofit.testHelpers
androidTestImplementation libs.junit
androidTestImplementation libs.truth
androidTestImplementation libs.mockwebserver
androidTestImplementation libs.okhttp.mockwebserver
androidTestImplementation libs.androidxTestRunner
}
4 changes: 2 additions & 2 deletions retrofit/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ addMultiReleaseSourceSet(14)
addMultiReleaseSourceSet(16)

dependencies {
api libs.okhttp
api libs.okhttp.client

compileOnly libs.android
compileOnly libs.kotlin.stdLib
compileOnly libs.kotlinCoroutines
compileOnly libs.kotlinx.coroutines

compileOnly libs.animalSnifferAnnotations
compileOnly libs.findBugsAnnotations
Expand Down
2 changes: 1 addition & 1 deletion retrofit/java-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dependencies {
testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.guava
testImplementation libs.mockwebserver
testImplementation libs.okhttp.mockwebserver
}

tasks.withType(JavaCompile).configureEach {
Expand Down
4 changes: 2 additions & 2 deletions retrofit/kotlin-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ dependencies {
testImplementation projects.retrofit.testHelpers
testImplementation libs.junit
testImplementation libs.truth
testImplementation libs.mockwebserver
testImplementation libs.kotlinCoroutines
testImplementation libs.okhttp.mockwebserver
testImplementation libs.kotlinx.coroutines
}
7 changes: 5 additions & 2 deletions samples/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apply plugin: 'java-library'
apply plugin: 'org.jetbrains.kotlin.jvm'

dependencies {
implementation projects.retrofit
Expand All @@ -7,7 +7,10 @@ dependencies {
implementation projects.retrofitConverters.gson
implementation projects.retrofitConverters.simplexml
implementation projects.retrofitAdapters.rxjava
implementation libs.mockwebserver
implementation libs.okhttp.loggingInterceptor
implementation libs.okhttp.mockwebserver
implementation libs.kotlin.stdLib
implementation libs.kotlinx.coroutines
implementation libs.guava
implementation libs.jsoup
compileOnly libs.findBugsAnnotations
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2024 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.retrofit

import okhttp3.Interceptor
import okhttp3.OkHttpClient
import okhttp3.Response
import okhttp3.ResponseBody
import okhttp3.logging.HttpLoggingInterceptor
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import retrofit2.Invocation
import retrofit2.Retrofit
import retrofit2.create
import retrofit2.http.GET

suspend fun main() {
val server = MockWebServer()
val client = OkHttpClient.Builder()
.addInterceptor(
ConditionalLoggingInterceptor(
HttpLoggingInterceptor(::println).setLevel(
HttpLoggingInterceptor.Level.BODY,
),
),
)
.build()
val retrofit = Retrofit.Builder()
.baseUrl(server.url("/"))
.client(client)
.build()
val exampleApi = retrofit.create<ExampleApi>()

server.enqueue(MockResponse())
exampleApi.one()

server.enqueue(MockResponse())
exampleApi.two()
}

private interface ExampleApi {
@GET("one")
suspend fun one(): ResponseBody

@Log
@GET("two")
suspend fun two(): ResponseBody
}

/**
* Retrofit service functions which are annotated with this class will have their HTTP calls
* logged. You must add [ConditionalLoggingInterceptor] to your [OkHttpClient] for this to work.
*/
annotation class Log

class ConditionalLoggingInterceptor(
private val loggingInterceptor: HttpLoggingInterceptor,
) : Interceptor {
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
request.tag(Invocation::class.java)?.let { invocation ->
if (invocation.method().isAnnotationPresent(Log::class.java)) {
return loggingInterceptor.intercept(chain)
}
}
return chain.proceed(request)
}
}

0 comments on commit e104ab6

Please sign in to comment.