package com.seeyon.apps.esign.utils;

import com.seeyon.apps.esign.po.esignapi.EsignException;
import com.seeyon.apps.esign.po.esignapi.EsignHttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @description Http 请求 辅助类
 * @author 澄泓
 * @since JDK1.7
 */
public class EsignHttpHelper {
	private static final Logger LOGGER = LoggerFactory.getLogger(EsignHttpHelper.class);

	/**
	 * 不允许外部创建实例
	 */
	private EsignHttpHelper() {

	}

	/**
	 * @description 发送常规HTTP 请求
	 *
	 * @param reqType 请求方式
	 * @param url 请求路径
	 * @param paramStr 请求参数
	 * @return
	 * @throws EsignException
	 * @author 澄泓
	 */
	public static EsignHttpResponse doCommHttp(String host, String url, EsignRequestType reqType, Object paramStr , Map<String,String> httpHeader, boolean debug) throws EsignException {
		return EsignHttpCfgHelper.sendHttp(reqType, host+url,httpHeader, paramStr, debug);
	}


	/**
	 * @description 发送文件流上传 HTTP 请求
	 *
	 * @param reqType 请求方式
	 * @param uploadUrl 请求路径
	 * @param param 请求参数
	 * @param fileContentMd5 文件fileContentMd5
	 * @param contentType 文件MIME类型
	 * @return
	 * @author 澄泓
	 */
	public static EsignHttpResponse doUploadHttp( String uploadUrl,EsignRequestType reqType,byte[] param, String fileContentMd5,
												 String contentType, boolean debug) throws EsignException {
		Map<String, String> uploadHeader = buildUploadHeader(fileContentMd5, contentType);
		if(debug){
			LOGGER.info("----------------------------start------------------------");
			LOGGER.info("fileContentMd5:{}",fileContentMd5);
			LOGGER.info("contentType:{}",contentType);
		}
		return EsignHttpCfgHelper.sendHttp(reqType,uploadUrl, uploadHeader, param,debug);
	}




	/**
	 * @description 创建文件流上传 请求头
	 *
	 * @param fileContentMd5
	 * @param contentType
	 * @return
	 * @author 澄泓
	 */
	public static Map<String, String> buildUploadHeader(String fileContentMd5, String contentType) {
		Map<String, String> header = new HashMap<>();
		header.put("Content-MD5", fileContentMd5);
		header.put("Content-Type", contentType);

		return header;
	}

	// ------------------------------私有方法end----------------------------------------------
}
