From 5ba7d38ce33d915a885724b5cdc9f9f2972382ce Mon Sep 17 00:00:00 2001 From: Maciej Walkowiak Date: Fri, 12 Nov 2021 09:23:51 +0100 Subject: [PATCH] Refactor OkHttp and Apollo to Kotlin functional interfaces. (#1797) --- CHANGELOG.md | 2 ++ .../android/okhttp/SentryOkHttpInterceptor.kt | 2 +- .../okhttp/SentryOkHttpInterceptorTest.kt | 30 +++++++------------ .../sentry/apollo/SentryApolloInterceptor.kt | 2 +- .../apollo/SentryApolloInterceptorTest.kt | 23 ++++---------- 5 files changed, 19 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 047ac84365..03c6e8ba08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +* Feat: Refactor OkHttp and Apollo to Kotlin functional interfaces (#1797) + ## 5.4.0 * Feat: Add `graphql-java` instrumentation (#1777) diff --git a/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt b/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt index ee5a781989..c9eb52acd7 100644 --- a/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt +++ b/sentry-android-okhttp/src/main/java/io/sentry/android/okhttp/SentryOkHttpInterceptor.kt @@ -91,7 +91,7 @@ class SentryOkHttpInterceptor( /** * The BeforeSpan callback */ - interface BeforeSpanCallback { + fun interface BeforeSpanCallback { /** * Mutates or drops span before being added * diff --git a/sentry-android-okhttp/src/test/java/io/sentry/android/okhttp/SentryOkHttpInterceptorTest.kt b/sentry-android-okhttp/src/test/java/io/sentry/android/okhttp/SentryOkHttpInterceptorTest.kt index 9942d0dd7e..c312b1f486 100644 --- a/sentry-android-okhttp/src/test/java/io/sentry/android/okhttp/SentryOkHttpInterceptorTest.kt +++ b/sentry-android-okhttp/src/test/java/io/sentry/android/okhttp/SentryOkHttpInterceptorTest.kt @@ -8,7 +8,6 @@ import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.whenever import io.sentry.Breadcrumb import io.sentry.IHub -import io.sentry.ISpan import io.sentry.SentryOptions import io.sentry.SentryTraceHeader import io.sentry.SentryTracer @@ -20,7 +19,6 @@ import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody -import okhttp3.Response import okhttp3.mockwebserver.MockResponse import okhttp3.mockwebserver.MockWebServer import okhttp3.mockwebserver.SocketPolicy @@ -206,11 +204,9 @@ class SentryOkHttpInterceptorTest { @Test fun `customizer modifies span`() { val sut = fixture.getSut( - beforeSpan = object : SentryOkHttpInterceptor.BeforeSpanCallback { - override fun execute(span: ISpan, request: Request, response: Response?): ISpan { - span.description = "overwritten description" - return span - } + beforeSpan = { span, _, _ -> + span.description = "overwritten description" + span } ) val request = getRequest() @@ -224,15 +220,13 @@ class SentryOkHttpInterceptorTest { @Test fun `customizer receives request and response`() { val sut = fixture.getSut( - beforeSpan = object : SentryOkHttpInterceptor.BeforeSpanCallback { - override fun execute(span: ISpan, request: Request, response: Response?): ISpan { - assertEquals(request.url, request.url) - assertEquals(request.method, request.method) - assertNotNull(response) { - assertEquals(201, it.code) - } - return span + beforeSpan = { span, request, response -> + assertEquals(request.url, request.url) + assertEquals(request.method, request.method) + assertNotNull(response) { + assertEquals(201, it.code) } + span } ) val request = getRequest() @@ -242,11 +236,7 @@ class SentryOkHttpInterceptorTest { @Test fun `customizer can drop the span`() { val sut = fixture.getSut( - beforeSpan = object : SentryOkHttpInterceptor.BeforeSpanCallback { - override fun execute(span: ISpan, request: Request, response: Response?): ISpan? { - return null - } - } + beforeSpan = { _, _, _ -> null } ) sut.newCall(getRequest()).execute() val httpClientSpan = fixture.sentryTracer.children.first() diff --git a/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt b/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt index cf4cdc8be6..c515da0154 100644 --- a/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt +++ b/sentry-apollo/src/main/java/io/sentry/apollo/SentryApolloInterceptor.kt @@ -126,7 +126,7 @@ class SentryApolloInterceptor( /** * The BeforeSpan callback */ - interface BeforeSpanCallback { + fun interface BeforeSpanCallback { /** * Mutates span before being added. * diff --git a/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt b/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt index e1d5ef6f7a..645f9bba30 100644 --- a/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt +++ b/sentry-apollo/src/test/java/io/sentry/apollo/SentryApolloInterceptorTest.kt @@ -3,8 +3,6 @@ package io.sentry.apollo import com.apollographql.apollo.ApolloClient import com.apollographql.apollo.coroutines.await import com.apollographql.apollo.exception.ApolloException -import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorRequest -import com.apollographql.apollo.interceptor.ApolloInterceptor.InterceptorResponse import com.nhaarman.mockitokotlin2.anyOrNull import com.nhaarman.mockitokotlin2.check import com.nhaarman.mockitokotlin2.mock @@ -12,7 +10,6 @@ import com.nhaarman.mockitokotlin2.verify import com.nhaarman.mockitokotlin2.whenever import io.sentry.Breadcrumb import io.sentry.IHub -import io.sentry.ISpan import io.sentry.ITransaction import io.sentry.SentryOptions import io.sentry.SentryTraceHeader @@ -135,14 +132,10 @@ class SentryApolloInterceptorTest { @Test fun `customizer modifies span`() { executeQuery( - fixture.getSut( - beforeSpan = object : SentryApolloInterceptor.BeforeSpanCallback { - override fun execute(span: ISpan, request: InterceptorRequest, response: InterceptorResponse?): ISpan { - span.description = "overwritten description" - return span - } - } - ) + fixture.getSut { span, _, _ -> + span.description = "overwritten description" + span + } ) verify(fixture.hub).captureTransaction( @@ -158,13 +151,7 @@ class SentryApolloInterceptorTest { @Test fun `when customizer throws, exception is handled`() { executeQuery( - fixture.getSut( - beforeSpan = object : SentryApolloInterceptor.BeforeSpanCallback { - override fun execute(span: ISpan, request: InterceptorRequest, response: InterceptorResponse?): ISpan { - throw RuntimeException() - } - } - ) + fixture.getSut { _, _, _ -> throw RuntimeException() } ) verify(fixture.hub).captureTransaction(