package com.turlir.abakgists.data;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import timber.log.Timber;

public class LogInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        log("Request: " + request.url().toString());
        if (request.method().equals("POST")) {
            log("Request body: " + body2str(request));
        }

        Response response = chain.proceed(request);
        String bodyString = response.body().string();
        log("Response (" + response.code() + "): " + bodyString);

        MediaType mediaType = response.body().contentType();
        ResponseBody body = ResponseBody.create(mediaType, bodyString);
        return response.newBuilder().body(body).build();
    }

    private String body2str(final Request request) {
        try {
            final Request copy = request.newBuilder().build();
            final Buffer buffer = new Buffer();
            if ((copy.body().contentLength() > 1024 * 1024)) { // > 1 MB
                return "<too long>";
            } else {
                copy.body().writeTo(buffer);
                return buffer.readUtf8();
            }
        } catch (IOException e) {
            return "<no body>";
        }
    }

    private static void log(String message) {
        Timber.i(message);
    }

}

@@ -29,16 +29,153 @@
 .data;%0A%0A
+import com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder;%0Aimport com.google.gson.JsonElement;%0Aimport com.google.gson.JsonParser;%0A%0A
 import j
@@ -331,28 +331,8 @@
 dy;%0A
-import okio.Buffer;%0A
 impo
@@ -408,16 +408,222 @@
 ptor %7B%0A%0A
+    private final Gson mGson;%0A    private final JsonParser mParser;%0A%0A    public LogInterceptor() %7B%0A        mGson = new GsonBuilder().setPrettyPrinting().create();%0A        mParser = new JsonParser();%0A    %7D%0A%0A
     @Ove
@@ -735,35 +735,40 @@
 uest();%0A        
-log
+Timber.i
 (%22Request: %22 + r
@@ -762,19 +762,20 @@
 equest: 
-%22 +
+%25s%22,
  request
@@ -849,27 +849,32 @@
             
-log
+Timber.i
 (%22Request bo
@@ -881,29 +881,21 @@
 dy: 
-%22 + body2str(
+%25s%22, 
 request)
 );%0A 
@@ -890,17 +890,16 @@
 request)
-)
 ;%0A      
@@ -1017,19 +1017,124 @@
         
-log
+if (%22application/json; charset=utf-8%22.equals(response.headers().get(%22Content-Type%22))) %7B%0A            Timber.i
 (%22Respon
@@ -1137,19 +1137,21 @@
 sponse (
-%22 +
+%25d)%22,
  respons
@@ -1162,18 +1162,125 @@
 de()
- + %22): %22 +
+);%0A            prettyJsonLog(bodyString);%0A        %7D else %7B%0A            Timber.i(%22Response (%25d): %25s%22, response.code(),
  bod
@@ -1288,16 +1288,26 @@
 String);
+%0A        %7D
 %0A%0A      
@@ -1513,559 +1513,171 @@
 ate 
-String body2str(final Request request) %7B%0A        try %7B%0A            final Request copy = request.newBuilder().build();%0A            final Buffer buffer = new Buffer();%0A            if ((copy.body().contentLength() %3E 1024 * 1024)) %7B // %3E 1 MB%0A                return %22%3Ctoo long%3E%22;%0A            %7D else %7B%0A                copy.body().writeTo(buffer);%0A                return buffer.readUtf8();%0A            %7D%0A        %7D catch (IOException e) %7B%0A            return %22%3Cno body%3E%22;%0A        %7D%0A    %7D%0A%0A    private static void log(String message) %7B%0A        Timber.i(message
+void prettyJsonLog(String message) %7B%0A        JsonElement json = mParser.parse(message);%0A        String prettyJson = mGson.toJson(json);%0A        Timber.i(prettyJson
 );%0A 

improve LogInterceptor