package com.sonic.lion.config;

import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.OkHttp3ClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * @author: code
 * @date: 2025/06/24
 * @Description:
 * @version: 1.0.0
 */
public class RestConfig implements RestTemplateCustomizer {

    @Override
    public void customize(RestTemplate restTemplate) {
        if(restTemplate.getRequestFactory() instanceof OkHttp3ClientHttpRequestFactory) {
            OkHttp3ClientHttpRequestFactory okHttpClient = (OkHttp3ClientHttpRequestFactory)restTemplate.getRequestFactory();
            okHttpClient.setConnectTimeout(20000);
            okHttpClient.setReadTimeout(20000);
        } else if(restTemplate.getRequestFactory() instanceof HttpComponentsClientHttpRequestFactory) {
            HttpComponentsClientHttpRequestFactory factory = (HttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory();
            factory.setConnectTimeout(20000);
            factory.setReadTimeout(20000);
        }

    }

    @Bean
    public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
        return restTemplateBuilder.build();
    }
}
