Skip to content

Instantly share code, notes, and snippets.

@warting
Last active January 26, 2024 14:42
Show Gist options
  • Save warting/4bd0d2a90cb9d058611a865ee3fe5c37 to your computer and use it in GitHub Desktop.
Save warting/4bd0d2a90cb9d058611a865ee3fe5c37 to your computer and use it in GitHub Desktop.
InvocationInterceptor
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