package com.sw.mes.utils;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Version 1.0
 * @Author:zbp
 * @Date:2022/10/279:48
 * @Content:
 */
@Component
public class RestUtils {
    @Resource
    private RestTemplate restTemplate;

    public ResponseEntity<String> send(String data, String url, String token) {
        HttpHeaders headers = new HttpHeaders();
        MediaType mediaType = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(mediaType);
        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
        headers.add("Authorization", "token_admin");
        if (!StringUtils.isBlank(token)) {
            headers.add("token", token);
        }
        HttpEntity<String> httpEntity = new HttpEntity<>(data, headers);
        //使用exchange请求接口
        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class);
        return exchange;
    }

    public String send(String urlValue) {
        String json = restTemplate.getForObject(urlValue, String.class);
        return json;
    }
}
