Last active
January 26, 2024 14:42
-
-
Save warting/4bd0d2a90cb9d058611a865ee3fe5c37 to your computer and use it in GitHub Desktop.
InvocationInterceptor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package se.warting.network | |
import se.warting.logger.Logger | |
import okhttp3.Interceptor | |
import okhttp3.Response | |
import retrofit2.Invocation | |
/** | |
* Network interceptor that reports invocations to the error reporter. | |
*/ | |
class InvocationInterceptor(private val errorReporter: Logger) : Interceptor { | |
override fun intercept(chain: Interceptor.Chain): Response { | |
val request = chain.request() | |
request.tag(Invocation::class.java)?.let { | |
val method = it.method().toString() | |
val message = "Network request called from: $method" | |
errorReporter.log(message) | |
} ?: errorReporter.logException(RuntimeException("No Invocation tag found on request.")) | |
return chain.proceed(request) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment