Skip to content

Instantly share code, notes, and snippets.

@ronjunevaldoz
Last active April 14, 2021 01:35
Show Gist options
  • Save ronjunevaldoz/8b6478b652b31fbe0172e7d564edbd41 to your computer and use it in GitHub Desktop.
Save ronjunevaldoz/8b6478b652b31fbe0172e7d564edbd41 to your computer and use it in GitHub Desktop.
/**
* Generates curl from requests
*/
class CurlLoggerInterceptor : Interceptor {
private val defaultContentType = Constants.CONTENT_TYPE
override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request()
if(!DevUtils.isDebuggableBuild()) {
return chain.proceed(request)
}
// enable only for debuggable build
val url = request.url
val headersRaw = if(request.headers.size == 0) "-H \"Content-Type: $defaultContentType\"" else request.headers.joinToString(" " , "-H \"", "\"") {
"${it.first}: ${it.second}"
}
val curl = "curl -d \"${url.query.orEmpty()}\" $headersRaw -X ${request.method} ${url.scheme}://${url.host}/${url.pathSegments.joinToString("/")}"
Timber.v(curl)
return chain.proceed(request)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment