package com.sxyz.rfid.retrofit.http;


import androidx.annotation.NonNull;

import com.sxyz.rfid.utils.L;
import com.sxyz.rfid.utils.LocalUtils;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @Description: 日志拦截器
 * @Author: wuchunyuan
 * @CreateDate: 2020/7/24 14:17
 * @UpdateUser: wuchunyuan
 * @UpdateDate: 2020/7/24 14:17
 * @UpdateRemark: 更新说明：
 * @Version: 1.0  "Bearer "+LocalUtils.getAdminToken()
 */
public class OkLogInterceptor implements Interceptor {

    @NonNull
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request original = chain.request();
        Request request = original.newBuilder().header("Content-Type", "application/json").header("Authorization", "Bearer " + LocalUtils.getToken()).method(original.method(), original.body()).build();
        String method = request.method();
        HttpUrl url = request.url();
//        String headers = request.headers().toString();
        Response response = chain.proceed(request);
        L.w("后台请求链接===>" + url + "\n请求方式-->" + method + "\n请求响应码-->" + response.code());

        Headers headers = request.headers();
        Set<String> names = headers.names();
        Iterator<String> iterator = names.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            String value = headers.get(next);
            L.w("后台请求Header===>" + next + " : " + value);
        }
        return response;
    }
}