package cclotus.weixin.api.feign

import feign.codec.Decoder
import feign.codec.ErrorDecoder
import org.springframework.beans.factory.ObjectFactory
import org.springframework.boot.autoconfigure.http.HttpMessageConverters
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder
import org.springframework.cloud.openfeign.support.SpringDecoder
import org.springframework.cloud.openfeign.support.SpringEncoder
import org.springframework.context.annotation.Bean
import org.springframework.http.MediaType
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter


class WxFeignConfiguration {
    @Bean
    fun feignErrorDecoder(): ErrorDecoder {
        return WxErrorDecoder()
    }

    @Bean
    fun feignDecoder(): Decoder {
        val objectFactory = ObjectFactory<HttpMessageConverters> {
            HttpMessageConverters(MappingJackson2HttpMessageConverter().apply {
                supportedMediaTypes = listOf(MediaType.APPLICATION_JSON, MediaType("application", "*+json"), MediaType.TEXT_PLAIN)
            })
        }
        @Suppress("DEPRECATION")
        return ResponseEntityDecoder(SpringDecoder(objectFactory))
    }

    @Bean
    fun feignEncoder(): SpringEncoder {
        val objectFactory = ObjectFactory<HttpMessageConverters> {
            HttpMessageConverters(MappingJackson2HttpMessageConverter())
        }
        return SpringEncoder(objectFactory)
    }

//    @Bean
//    fun decoder(): Decoder? {
//        return JacksonDecoder()
//    }
//    @Bean
//    fun encoder(): Encoder? {
//        return JacksonEncoder()
//    }
}