package com.cooleshow.base.data.net;

import java.io.IOException;

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

/**
 * Author by pq, Date on 2022/4/21.
 */
public class CommonInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request oldRequest = chain.request();
        // 添加新的公共参数
        HttpUrl.Builder authorizedUrlBuilder = oldRequest.url()
                .newBuilder()
                .scheme(oldRequest.url().scheme())
                .host(oldRequest.url().host());
        CommonParamsHelper.getInstance().insertCommonUrlParams(authorizedUrlBuilder);

        // 新的请求,添加公共header参数
        Request.Builder builder = oldRequest.newBuilder()
                .method(oldRequest.method(), oldRequest.body())
                .url(authorizedUrlBuilder.build());
        CommonParamsHelper.getInstance().insertCommonHeaderParams(builder);
        Request newRequest = builder.build();
        return chain.proceed(newRequest);
    }
}
