package com.persagy.dmp.auth.config;

import feign.Logger;
import feign.RequestInterceptor;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

/**
 * 鉴权的Feign配置类
 * @author : lijie
 * Update By 2022/1/11 14:58
 */
@EnableFeignClients(basePackages="com.persagy.dmp.auth.**.client")
@Configuration
public class AuthFeignConfig {

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    @Primary
    @Scope(SCOPE_PROTOTYPE)
    Encoder feignFormEncoder() {
        return new SpringFormEncoder(new SpringEncoder(this.messageConverters));
    }

    @Bean
    Logger.Level feignLevel(){
        return Logger.Level.FULL;
    }

    @Bean
    @ConditionalOnMissingBean({AuthFeignInterceptor.class})
    @ConditionalOnProperty(value = "persagy.common.auth.channel", havingValue = "2")
    RequestInterceptor authFeignInterceptor(){
        return new AuthFeignInterceptor();
    }
}
