moshkit

CurlLoggerInterceptor

Dec 4th, 2020
570
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.readyforsky.apiapollo.api
  2.  
  3. import com.readyforsky.utils.info
  4. import okhttp3.HttpUrl
  5. import okhttp3.Interceptor
  6. import okhttp3.Request
  7. import okhttp3.Response
  8. import okio.Buffer
  9. import timber.log.Timber
  10. import java.nio.charset.Charset
  11.  
  12. class CurlLoggerInterceptor(
  13.     val tag: String
  14. ) : Interceptor {
  15.  
  16.     override fun intercept(chain: Interceptor.Chain): Response {
  17.         val timestamp = System.currentTimeMillis()
  18.  
  19.         print(chain.request().url.toString(), chain.request().toLog(), null)
  20.  
  21.         val response = chain.proceed(chain.request())
  22.         val request = response.request
  23.  
  24.         val responseStringBuilder = StringBuilder().apply {
  25.             append(timestamp.info("Response"))
  26.             append("  ")
  27.             response.headers.forEach {
  28.                 addHeader(it.first, it.second)
  29.             }
  30.         }
  31.  
  32.         print(request.url.toString(), request.toLog(), responseStringBuilder.toString())
  33.         return response
  34.     }
  35.  
  36.     private fun StringBuilder.addUrl(httpUrl: HttpUrl) {
  37.         append(" \"").append(httpUrl.toString()).append("\"")
  38.     }
  39.  
  40.     private fun StringBuilder.addBody(request: Request) {
  41.         val requestBody = request.body ?: return
  42.         val buffer = Buffer()
  43.         requestBody.writeTo(buffer)
  44.         val contentType = requestBody.contentType()
  45.         if (contentType != null) {
  46.             if (request.header("Content-Type").isNullOrEmpty()) {
  47.                 addHeader("Content-Type", contentType.toString())
  48.             }
  49.             val charset = contentType.charset(UTF8)
  50.             append(" -d \'").append(buffer.readString(charset!!)).append("\'")
  51.         }
  52.     }
  53.  
  54.     private fun StringBuilder.addHeader(
  55.         headerName: String,
  56.         headerValue: String
  57.     ) {
  58.         append("-H " + "\"")
  59.             .append(headerName)
  60.             .append(": ")
  61.             .append(headerValue)
  62.             .append("\" ")
  63.     }
  64.  
  65.     private fun Request.toLog(): String {
  66.         return StringBuilder("").apply {
  67.             append("curl ")
  68.             append("--compressed ")
  69.             append("-X ")
  70.             append(method.toUpperCase())
  71.             append(" ")
  72.             headers.forEach {
  73.                 addHeader(it.first, it.second)
  74.             }
  75.             addBody(this@toLog)
  76.             addUrl(url)
  77.             append(" -L -g -k -v ")
  78.             append(" | python3 -m json.tool")
  79.         }.toString()
  80.     }
  81.  
  82.     private fun print(url: String, command: String, response: String?) {
  83.         val logMsg = """
  84.            
  85.            URL: $url
  86.            $SINGLE_DIVIDER
  87.            $command
  88.            $SINGLE_DIVIDER
  89.            $response
  90.            
  91.            """.trimIndent()
  92.         Timber.d(logMsg)
  93.     }
  94.  
  95.     companion object {
  96.         private const val SINGLE_DIVIDER = "────────────────────────────────────────────"
  97.         private val UTF8 = Charset.forName("UTF-8")
  98.     }
  99. }
RAW Paste Data