package com.friday.custody.service.trustService.htsc.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class RestTemplateUtil {


    public static HttpEntity<JSONObject> getDataFromRemote(String url, HttpHeaders headers) {
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setReadTimeout(120*1000);
        requestFactory.setConnectTimeout(120*1000);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        //设置接收返回值的格式为json
        List<MediaType> mediaTypeList = new ArrayList<>();
        mediaTypeList.add(MediaType.APPLICATION_JSON_UTF8);
        headers.setAccept(mediaTypeList);
        return restTemplate
                .exchange(url,
                        HttpMethod.GET,
                        new HttpEntity<>(null, headers),
                        JSONObject.class);
    }

}
