package shop.alien.config.feign;

import feign.Logger;
import feign.Request;
import feign.codec.Decoder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 从 Openfeign配置
 *
 * @author ssk
 * @version 1.0
 * @date 2024/10/21 19:52
 */
@Configuration
@RequiredArgsConstructor
public class FeignOptionConfig {

    private final FeignNacosConfig feignNacosConfig;

    @Bean
    public Request.Options options() {
        return new Request.Options(feignNacosConfig.getConnectTimeout(), feignNacosConfig.getReadTimeout(), true);
    }

    /**
     * OpenFeign打印日志, 需在配置文件中指定feign类位置
     * <p>
     * NONE, 默认，不显示任何日志
     * BASIC, 仅记录请求方法、url、响应状态码及执行时间
     * HEADERS, 除记录BASIC信息外，还记录请求头和响应头
     * FULL,除了HEADERS信息外，还有请求和响应正文以及元数据
     *
     * @return 日志等级
     */
    @Bean
    Logger.Level feignLoggerLevel() {
        return Logger.Level.BASIC;
//        return Logger.Level.FULL; // 必须设置为 FULL 才能获取响应体
    }

//    @Bean
//    public FeignBodyLogger feignLogger() {
//        return new FeignBodyLogger();
//    }

    @Bean
    public Decoder feignDecoder(ObjectFactory<HttpMessageConverters> converters) {
        return new GzipResponseDecoder(new SpringDecoder(converters));
    }


}
