package com.ho.business.common;

import okhttp3.*;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;

/**
 * @author fancl
 * @desc:
 * @date 2024/1/30
 */
public class CustomLoggingInterceptor implements Interceptor {
    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        Request request = chain.request();

        long startTime = System.nanoTime();
        System.out.println("Sending request " + request.url() + " on " + chain.connection() + "\n" + request.headers());

        Response response = chain.proceed(request);

        long endTime = System.nanoTime();
        System.out.println("Received response for " + response.request().url() + " in " + ((endTime - startTime) / 1e6) + "ms\n" + response.headers());

        MediaType contentType = response.body().contentType();
        String content = response.body().string();
        System.out.println("Response body:\n" + content);

        ResponseBody wrappedBody = ResponseBody.create(contentType, content);
        return response.newBuilder().body(wrappedBody).build();
    }
}
