package com.docus.server.common.config;

import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import feign.optionals.OptionalDecoder;
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.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;

@Configuration
public class SpringCloudConfig {

    @Autowired(required = false)
    private ObjectFactory<HttpMessageConverters> messageConverters;

    //feign统一输出体响应拦截
    @ConditionalOnMissingBean(value = Decoder.class)
    @Bean
    public Decoder feignResponseDecoder() {
        return new OptionalDecoder(
            new FeignResponseDecoder(new SpringDecoder(this.messageConverters)));
    }

    @ConditionalOnMissingBean(value = Decoder.class)
    @Bean
    @Primary
    @Scope("prototype")
    public Encoder multipartFormEncoder() {
        return new SpringFormEncoder();
    }
}
