package com.example.zhpan.banner.net.interceptor;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.NetworkUtils;

import java.io.IOException;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by zhpan on 2018/3/21.
 */

public class HttpCacheInterceptor implements Interceptor {
  //  配置缓存的拦截器
  @Override
  public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    if (!NetworkUtils.isConnected()) {  //没网强制从缓存读取
      request = request.newBuilder()
          .cacheControl(CacheControl.FORCE_CACHE)
          .build();
      LogUtils.d("Okhttp", "no network");
    }

    Response originalResponse = chain.proceed(request);
    if (NetworkUtils.isConnected()) {
      //有网的时候读接口上的@Headers里的配置，你可以在这里进行统一的设置
      String cacheControl = request.cacheControl().toString();

      return originalResponse.newBuilder()
          .header("Cache-Control", cacheControl)
          .removeHeader("Pragma")
          .build();
    } else {
      return originalResponse.newBuilder()
          .header("Cache-Control", "public, only-if-cached, max-stale=2419200")
          .removeHeader("Pragma")
          .build();
    }
  }
}
