package com.rc.httpcore.interceptor



import android.util.Log
import okhttp3.FormBody
import okhttp3.Interceptor
import okhttp3.Response
import okhttp3.ResponseBody

class HttpLoggingInterceptorLog : Interceptor {
    private val TAG="okhttps"
    override fun intercept(chain: Interceptor.Chain): Response {
        val request = chain.request()
        val startTime = System.currentTimeMillis()
        val response = chain.proceed(chain.request())
        val endTime = System.currentTimeMillis()
        val duration = endTime - startTime
        val mediaType = response.body()!!.contentType()
        val content = response.body()!!.string()
        Log.d(TAG, "\n")
        Log.d(TAG, "----------Start----------------")
        Log.d(TAG, "| $request")
        val method = request.method()
        if ("POST" == method) {
            val sb = StringBuilder()
            if (request.body() is FormBody) {
                val body = request.body() as FormBody?
                for (i in 0 until body!!.size()) {
                    sb.append(body!!.encodedName(i) + "=" + body!!.encodedValue(i) + ",")
                }
                sb.delete(sb.length - 1, sb.length)
                Log.d(TAG, "| RequestParams:{$sb}")
            }
        }
        Log.d(TAG, "| Response:$content")
        Log.d(TAG, "----------End:" + duration + "毫秒----------")
        return response.newBuilder()
            .body(ResponseBody.create(mediaType, content))
            .build()
    }



}