package ru.kayashov.bar.service;

import lombok.experimental.UtilityClass;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.Optional;

@UtilityClass
public class RestUtil {

    public static <T> Optional<ResponseEntity<T>> sendRequest(Class<T> responseType, RequestEntity<?> request) {
        try {
            RestTemplate restTemplate = new RestTemplate();
            return Optional.of(restTemplate.exchange(request, responseType));
        } catch (HttpClientErrorException e) {
            return Optional.empty();
        }
    }
}