package com.yn.bftl.thirdparty.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * http对应请求
 * 当前只写了post请求，有待优化代码
 *
 * @Author: lg
 * @Create: 2021-09-24
 */
@Slf4j
public class HttpRequestUtil {

    public static String contentType = ContentType.APPLICATION_JSON.toString();

    /**
     * post请求
     * 待简化代码
     *
     * @param url
     * @param param
     * @return
     */
    public static String httpPost(String url, String param) {
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        CloseableHttpResponse closeableHttpResponse;
        String asset_synchronization;
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.addHeader(HttpHeaders.CONTENT_TYPE, contentType);
            StringEntity entity = new StringEntity(param,  ContentType.create("text/json", "UTF-8"));
            httpPost.setEntity(entity);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
            httpPost.setConfig(requestConfig);

            closeableHttpResponse = closeableHttpClient.execute(httpPost);

            int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                //TODO:状态码非200代表没有正常返回,此处处理你的业务
                log.error("HTTP请求外部链接：" + url + ",参数:" + param + ",状态码:" + statusCode);
                return null;
            } else {
                HttpEntity httpEntity = closeableHttpResponse.getEntity();
                asset_synchronization = EntityUtils.toString(httpEntity, "UTF-8");
            }
        } catch (Exception e) {
            return null;
        }
        return asset_synchronization;
    }

    /**
     * get请求
     *
     * @param url
     * @return
     */
    public static String httpGet(String url) {
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        CloseableHttpResponse closeableHttpResponse;
        String asset_synchronization;
        try {
            HttpGet httpGet = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();
            httpGet.setConfig(requestConfig);

            closeableHttpResponse = closeableHttpClient.execute(httpGet);

            int statusCode = closeableHttpResponse.getStatusLine().getStatusCode();
            if (statusCode != HttpStatus.SC_OK) {
                return null;
            } else {
                HttpEntity httpEntity = closeableHttpResponse.getEntity();
                asset_synchronization = EntityUtils.toString(httpEntity, "UTF-8");
            }
        } catch (Exception e) {
            return null;
        }
        return asset_synchronization;
    }
}
