package org.springblade.openinterface.dianxin.util;

import cn.hutool.json.JSONUtil;
import org.springblade.openinterface.dianxin.constants.JiYueConf;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

/**
 * @author lidexi
 * @date 2021/7/15 16:22
 */

public class MyHttpUtil {

	public static ResponseEntity post(Object object, JiYueConf.UrlConstant urlConstant){
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity responseEntity = null;
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type","application/json;charset=UTF-8");
		headers.add("accesstoken",  new AuthUtil().getToken());
		System.out.println("参数："+ JSONUtil.toJsonStr(object));
		HttpEntity<String> requestEntity = new HttpEntity<>(JSONUtil.toJsonStr(object), headers);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
		};
		URI uri = URI.create(urlConstant.getUrl());
		responseEntity = restTemplate.exchange(uri, urlConstant.getMethod(), requestEntity, reference);
		return responseEntity;
	}
}
