Skip to content

Commit 8c2b181

Browse files
committed
Conditionally-logging interceptor sample
1 parent 348ac8f commit 8c2b181

File tree

26 files changed

+115
-30
lines changed

26 files changed

+115
-30
lines changed

Diff for: gradle/libs.versions.toml

+4-3
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ incap-processor = { module = "net.ltgt.gradle.incap:incap-processor", version.re
5050
autoService-annotations = { module = "com.google.auto.service:auto-service-annotations", version.ref = "autoService" }
5151
autoService-compiler = { module = "com.google.auto.service:auto-service", version.ref = "autoService" }
5252

53-
kotlinCoroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version = "1.8.1" }
53+
kotlinx-coroutines = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version = "1.8.1" }
5454
kotlinx-serialization-core = { module = "org.jetbrains.kotlinx:kotlinx-serialization-core", version.ref = "kotlinx-serialization" }
5555
kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" }
5656
kotlinx-serialization-proto = { module = "org.jetbrains.kotlinx:kotlinx-serialization-protobuf", version.ref = "kotlinx-serialization" }
57-
okhttp = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
58-
mockwebserver = { module = "com.squareup.okhttp3:mockwebserver", version.ref = "okhttp" }
57+
okhttp-client = { module = "com.squareup.okhttp3:okhttp", version.ref = "okhttp" }
58+
okhttp-loggingInterceptor = { module = "com.squareup.okhttp3:logging-interceptor", version.ref = "okhttp" }
59+
okhttp-mockwebserver = { module = "com.squareup.okhttp3:mockwebserver", version.ref = "okhttp" }
5960
junit = { module = "junit:junit", version = "4.13.2" }
6061
truth = "com.google.truth:truth:1.4.2"
6162
guava = { module = "com.google.guava:guava", version = "33.2.0-jre" }

Diff for: retrofit-adapters/guava/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies {
99

1010
testImplementation libs.junit
1111
testImplementation libs.truth
12-
testImplementation libs.mockwebserver
12+
testImplementation libs.okhttp.mockwebserver
1313
}
1414

1515
jar {

Diff for: retrofit-adapters/java8/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies {
88
testImplementation libs.junit
99
testImplementation libs.truth
1010
testImplementation libs.guava
11-
testImplementation libs.mockwebserver
11+
testImplementation libs.okhttp.mockwebserver
1212
}
1313

1414
jar {

Diff for: retrofit-adapters/rxjava/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies {
99
testImplementation libs.junit
1010
testImplementation libs.truth
1111
testImplementation libs.guava
12-
testImplementation libs.mockwebserver
12+
testImplementation libs.okhttp.mockwebserver
1313
}
1414

1515
jar {

Diff for: retrofit-adapters/rxjava2/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies {
1010
testImplementation libs.junit
1111
testImplementation libs.truth
1212
testImplementation libs.guava
13-
testImplementation libs.mockwebserver
13+
testImplementation libs.okhttp.mockwebserver
1414
}
1515

1616
jar {

Diff for: retrofit-adapters/rxjava3/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies {
1010
testImplementation libs.junit
1111
testImplementation libs.truth
1212
testImplementation libs.guava
13-
testImplementation libs.mockwebserver
13+
testImplementation libs.okhttp.mockwebserver
1414
}
1515

1616
jar {

Diff for: retrofit-adapters/scala/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies {
99
testImplementation libs.junit
1010
testImplementation libs.truth
1111
testImplementation libs.guava
12-
testImplementation libs.mockwebserver
12+
testImplementation libs.okhttp.mockwebserver
1313
}
1414

1515
jar {

Diff for: retrofit-converters/gson/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies {
88

99
testImplementation libs.junit
1010
testImplementation libs.truth
11-
testImplementation libs.mockwebserver
11+
testImplementation libs.okhttp.mockwebserver
1212
}
1313

1414
jar {

Diff for: retrofit-converters/guava/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies {
88

99
testImplementation libs.junit
1010
testImplementation libs.truth
11-
testImplementation libs.mockwebserver
11+
testImplementation libs.okhttp.mockwebserver
1212
}
1313

1414
jar {

Diff for: retrofit-converters/jackson/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies {
88

99
testImplementation libs.junit
1010
testImplementation libs.truth
11-
testImplementation libs.mockwebserver
11+
testImplementation libs.okhttp.mockwebserver
1212
testImplementation libs.jacksonDataformatCbor
1313
}
1414

Diff for: retrofit-converters/java8/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dependencies {
77

88
testImplementation libs.junit
99
testImplementation libs.truth
10-
testImplementation libs.mockwebserver
10+
testImplementation libs.okhttp.mockwebserver
1111
testImplementation libs.findBugsAnnotations
1212
}
1313

Diff for: retrofit-converters/jaxb/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies {
99
testImplementation libs.jaxbImpl
1010
testImplementation libs.junit
1111
testImplementation libs.truth
12-
testImplementation libs.mockwebserver
12+
testImplementation libs.okhttp.mockwebserver
1313
testImplementation libs.findBugsAnnotations
1414
}
1515

Diff for: retrofit-converters/jaxb3/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies {
99
testImplementation libs.jaxb3Impl
1010
testImplementation libs.junit
1111
testImplementation libs.truth
12-
testImplementation libs.mockwebserver
12+
testImplementation libs.okhttp.mockwebserver
1313
testImplementation libs.findBugsAnnotations
1414
}
1515

Diff for: retrofit-converters/kotlinx-serialization/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies {
88
api libs.kotlinx.serialization.core
99

1010
testImplementation libs.junit
11-
testImplementation libs.mockwebserver
11+
testImplementation libs.okhttp.mockwebserver
1212
testImplementation libs.kotlinx.serialization.proto
1313
testImplementation libs.kotlinx.serialization.json
1414
}

Diff for: retrofit-converters/moshi/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies {
88

99
testImplementation libs.junit
1010
testImplementation libs.truth
11-
testImplementation libs.mockwebserver
11+
testImplementation libs.okhttp.mockwebserver
1212
}
1313

1414
jar {

Diff for: retrofit-converters/protobuf/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ dependencies {
99

1010
testImplementation libs.junit
1111
testImplementation libs.truth
12-
testImplementation libs.mockwebserver
12+
testImplementation libs.okhttp.mockwebserver
1313
}
1414

1515
jar {

Diff for: retrofit-converters/scalars/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dependencies {
77

88
testImplementation libs.junit
99
testImplementation libs.truth
10-
testImplementation libs.mockwebserver
10+
testImplementation libs.okhttp.mockwebserver
1111
}
1212

1313
jar {

Diff for: retrofit-converters/simplexml/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dependencies {
88

99
testImplementation libs.junit
1010
testImplementation libs.truth
11-
testImplementation libs.mockwebserver
11+
testImplementation libs.okhttp.mockwebserver
1212
}
1313

1414
jar {

Diff for: retrofit-converters/wire/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ dependencies {
1111

1212
testImplementation libs.junit
1313
testImplementation libs.truth
14-
testImplementation libs.mockwebserver
14+
testImplementation libs.okhttp.mockwebserver
1515
}
1616

1717
jar {

Diff for: retrofit-mock/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies {
1010
testImplementation libs.junit
1111
testImplementation libs.truth
1212
testImplementation libs.kotlin.stdLib
13-
testImplementation libs.kotlinCoroutines
13+
testImplementation libs.kotlinx.coroutines
1414
}
1515

1616
jar {

Diff for: retrofit/android-test/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ dependencies {
4747
androidTestImplementation projects.retrofit.testHelpers
4848
androidTestImplementation libs.junit
4949
androidTestImplementation libs.truth
50-
androidTestImplementation libs.mockwebserver
50+
androidTestImplementation libs.okhttp.mockwebserver
5151
androidTestImplementation libs.androidxTestRunner
5252
}

Diff for: retrofit/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ addMultiReleaseSourceSet(14)
3232
addMultiReleaseSourceSet(16)
3333

3434
dependencies {
35-
api libs.okhttp
35+
api libs.okhttp.client
3636

3737
compileOnly libs.android
3838
compileOnly libs.kotlin.stdLib
39-
compileOnly libs.kotlinCoroutines
39+
compileOnly libs.kotlinx.coroutines
4040

4141
compileOnly libs.animalSnifferAnnotations
4242
compileOnly libs.findBugsAnnotations

Diff for: retrofit/java-test/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dependencies {
66
testImplementation libs.junit
77
testImplementation libs.truth
88
testImplementation libs.guava
9-
testImplementation libs.mockwebserver
9+
testImplementation libs.okhttp.mockwebserver
1010
}
1111

1212
tasks.withType(JavaCompile).configureEach {

Diff for: retrofit/kotlin-test/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ dependencies {
55
testImplementation projects.retrofit.testHelpers
66
testImplementation libs.junit
77
testImplementation libs.truth
8-
testImplementation libs.mockwebserver
9-
testImplementation libs.kotlinCoroutines
8+
testImplementation libs.okhttp.mockwebserver
9+
testImplementation libs.kotlinx.coroutines
1010
}

Diff for: samples/build.gradle

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
apply plugin: 'java-library'
1+
apply plugin: 'org.jetbrains.kotlin.jvm'
22

33
dependencies {
44
implementation projects.retrofit
@@ -7,7 +7,10 @@ dependencies {
77
implementation projects.retrofitConverters.gson
88
implementation projects.retrofitConverters.simplexml
99
implementation projects.retrofitAdapters.rxjava
10-
implementation libs.mockwebserver
10+
implementation libs.okhttp.loggingInterceptor
11+
implementation libs.okhttp.mockwebserver
12+
implementation libs.kotlin.stdLib
13+
implementation libs.kotlinx.coroutines
1114
implementation libs.guava
1215
implementation libs.jsoup
1316
compileOnly libs.findBugsAnnotations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (C) 2024 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.retrofit
17+
18+
import okhttp3.Interceptor
19+
import okhttp3.OkHttpClient
20+
import okhttp3.Response
21+
import okhttp3.ResponseBody
22+
import okhttp3.logging.HttpLoggingInterceptor
23+
import okhttp3.mockwebserver.MockResponse
24+
import okhttp3.mockwebserver.MockWebServer
25+
import retrofit2.Invocation
26+
import retrofit2.Retrofit
27+
import retrofit2.create
28+
import retrofit2.http.GET
29+
30+
suspend fun main() {
31+
val server = MockWebServer()
32+
val client = OkHttpClient.Builder()
33+
.addInterceptor(
34+
ConditionalLoggingInterceptor(
35+
HttpLoggingInterceptor(::println).setLevel(
36+
HttpLoggingInterceptor.Level.BODY,
37+
),
38+
),
39+
)
40+
.build()
41+
val retrofit = Retrofit.Builder()
42+
.baseUrl(server.url("/"))
43+
.client(client)
44+
.build()
45+
val exampleApi = retrofit.create<ExampleApi>()
46+
47+
server.enqueue(MockResponse())
48+
exampleApi.one()
49+
50+
server.enqueue(MockResponse())
51+
exampleApi.two()
52+
}
53+
54+
private interface ExampleApi {
55+
@GET("one")
56+
suspend fun one(): ResponseBody
57+
58+
@Log
59+
@GET("two")
60+
suspend fun two(): ResponseBody
61+
}
62+
63+
/**
64+
* Retrofit service functions which are annotated with this class will have their HTTP calls
65+
* logged. You must add [ConditionalLoggingInterceptor] to your [OkHttpClient] for this to work.
66+
*/
67+
annotation class Log
68+
69+
class ConditionalLoggingInterceptor(
70+
private val loggingInterceptor: HttpLoggingInterceptor,
71+
) : Interceptor {
72+
override fun intercept(chain: Interceptor.Chain): Response {
73+
val request = chain.request()
74+
request.tag(Invocation::class.java)?.let { invocation ->
75+
if (invocation.method().isAnnotationPresent(Log::class.java)) {
76+
return loggingInterceptor.intercept(chain)
77+
}
78+
}
79+
return chain.proceed(request)
80+
}
81+
}

0 commit comments

Comments
 (0)