package com.yidian.bcommon.http

import com.orhanobut.hawk.Hawk
import com.yidian.bcommon.HawkConfig
import okhttp3.Interceptor
import okhttp3.Response

class ApiSaveCookiesInterceptor : Interceptor {

  companion object {
    // 忽略cookie的host集合
    val cookieInterceptWhiteList = arrayListOf(CommonService.DOMAIN_YD_JIRA)
  }

  override fun intercept(chain: Interceptor.Chain): Response {
    val originalResponse = chain.proceed(chain.request())
    // 如果当前请求host在白名单内则不进行cookie拦截
    if (cookieInterceptWhiteList.contains(chain.request().url.host)) {
      return originalResponse
    }
    val cookieList = originalResponse.headers("Set-Cookie")
    if (cookieList.isNotEmpty()) {
      for (cookie in cookieList) {
        if (cookie.contains("JSESSIONID")) {
          Hawk.put(HawkConfig.Cookie, cookie)
        }
      }
    }
    return originalResponse
  }
}
