/**
 * 
 */
package org.springblade.etl.controller;

import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.etl.dto.HttpRequestDto;
import org.springblade.etl.enums.HttpMethod;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.http.HttpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author wookvn
 *
 */
@RestController
@RequestMapping("/http")
@Api(value = "HTTP请求相关接口", tags = "HTTP请求接口")
public class HttpController extends BladeController {

	/**
	 * 通过http接口获取数据
	 * @param requestDto
	 * @return
	 */
	@PostMapping("/execute")
	@ApiOperation(value = "请求接口获取数据", httpMethod = "POST")
	public R<String> execute(@Validated @RequestBody HttpRequestDto requestDto){
		String resBody = null;
		if(requestDto.getMethod() == HttpMethod.GET) {
			resBody = HttpUtil.createGet(requestDto.getUrl()).addHeaders(requestDto.getHeaders()).form(requestDto.getParams()).execute().body();
		}else {
			resBody = HttpUtil.createPost(requestDto.getUrl()).addHeaders(requestDto.getHeaders()).body(requestDto.getBody()).form(requestDto.getParams()).execute().body();
		}
		return R.data(resBody);
	}
}
