package org.springblade.datamigration.util;

import org.springblade.common.constant.HttpConstant;
import org.springblade.core.tool.api.R;
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/6/17 14:54
 * 物业系统http
 */

public class WuyeSystemHttpUtil {
	public static R postQuery(String body, String url, String accessToken) {
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity responseEntity = null;
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", HttpConstant.APPLICATION_JSON);
		headers.add("Cookie", "access-token=" + accessToken);
		HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
		};
		URI uri = URI.create(url);
		responseEntity = restTemplate.exchange(uri, HttpMethod.POST, requestEntity, reference);
		System.out.println();
		return R.data(responseEntity.getBody());
	}

	public static R getQuery(String body, String url, String accessToken) {
		RestTemplate restTemplate = new RestTemplate();
		ResponseEntity responseEntity = null;
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", HttpConstant.APPLICATION_JSON);
		headers.add("Cookie", "access-token=" + accessToken);
		HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
		ParameterizedTypeReference<Object> reference = new ParameterizedTypeReference<Object>() {
		};
		URI uri = URI.create(url);
		responseEntity = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, reference);
		System.out.println();
		return R.data(responseEntity.getBody());
	}
}
