package com.dxhy.auth.client.config;

import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.dxhy.auth.client.interceptor.OkHttpTokenInterceptor;

import feign.Feign;
import okhttp3.ConnectionPool;

/**
 * @author jiaohongyang
 */
@AutoConfigureBefore(FeignAutoConfiguration.class)
@Configuration
@ConditionalOnClass(Feign.class)
public class FeignOkHttpConfig {

    @Resource
    private OkHttpTokenInterceptor okHttpTokenInterceptor;

    @Bean
    public okhttp3.OkHttpClient okHttpClient() {
        int feignOkHttpReadTimeout = 60;
        int feignConnectTimeout = 60;
        int feignWriteTimeout = 120;
        return new okhttp3.OkHttpClient.Builder().readTimeout(feignOkHttpReadTimeout, TimeUnit.SECONDS)
            .connectTimeout(feignConnectTimeout, TimeUnit.SECONDS).writeTimeout(feignWriteTimeout, TimeUnit.SECONDS)
            .connectionPool(new ConnectionPool()).addInterceptor(okHttpTokenInterceptor).build();
    }
}
