package com.sifang.modules.youkua.utils;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.sifang.modules.utils.http.HttpUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YWUtils {
    private static final Logger log = LoggerFactory.getLogger(YWUtils.class);
    public static String request(String url, Map body){
        String result= HttpUtil.createPost(url).body(JSONUtil.toJsonStr(body)).execute().body();
        return result;
    }
    public static String request2(String url, List body){
        log.info("YWUtils  request2  url:{},jsonStr:{}",url,JSONUtil.toJsonStr(body));
        String result= HttpUtil.createPost(url).body(JSONUtil.toJsonStr(body)).execute().body();
        return result;
    }
    /**
     * get请求
     */
    public static String requestForGet(String host,String path,Map<String,String> body){
        Map<String,String> headers=new HashMap<>();
        try {
            HttpResponse response = HttpUtils.doGet(host, path, headers, body);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                String result = null;
                if (entity != null) {
                    result = EntityUtils.toString(entity, "UTF-8");
                }
                return result;
            }
        } catch (Exception e) {
            log.info("请求失败：" + e.getMessage());
            throw new RuntimeException(e);
        }
        return "";
    }
}
