package ru.nbch.credit_tracker.config.rest;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.springframework.web.client.RestTemplate;
import ru.nbch.credit_tracker.config.settings.CreditTrackerSettings;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Configuration
public class RestConfig {

    @Bean(name = "restTemplate")
    public RestTemplate restTemplate(CreditTrackerSettings config) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setUriTemplateHandler(new RootUriTemplateHandler(config.getSignalsRootUri()));
        restTemplate.setErrorHandler(new ResponseErrorHandler());
        return restTemplate;
    }

    @Bean(name = "executorService")
    public ExecutorService executorService() {
        return  Executors.newCachedThreadPool(new CustomizableThreadFactory("package-registration-"));
    }
}
