package org.springblade.modules.finance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cloud.apigateway.sdk.utils.Client;
import com.cloud.apigateway.sdk.utils.Request;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springblade.common.constant.Constant;
import org.springblade.common.utils.SSLCipherSuiteUtil;
import org.springblade.core.log.exception.ServiceException;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * Author pangqijun
 * Date 2023/6/30
 * Description
 */
@Slf4j
@Service
public class HuaWeiApiService {

    public String get(String url) throws IOException {
        Request request = new Request();
        CloseableHttpClient client = null;
        try {
            request.setKey("926b374c31e84202b1d173b43c2bc779");
            request.setSecret("279f48ba5bf44e11aae78bb411f75943");
            request.setMethod("GET");
//            request.setUrl("http://wuliu.apistore.huaweicloud.com/wuliu?no=JT3030652432572");
            request.setUrl(url);
            request.addHeader("Content-Type", "text/plain");

            // Sign the request.
            HttpRequestBase signedRequest = Client.sign(request, Constant.SIGNATURE_ALGORITHM_SDK_HMAC_SHA256);
            // Do not verify ssl certificate
            client = (CloseableHttpClient) SSLCipherSuiteUtil.createHttpClient(Constant.INTERNATIONAL_PROTOCOL);
            HttpResponse response = client.execute(signedRequest);
            // Print the body of the response.
            HttpEntity resEntity = response.getEntity();
            if (resEntity != null) {
                return EntityUtils.toString(resEntity, "UTF-8");
            }
            throw new ServiceException("华为云api请求无返回结果");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException("华为云api请求异常");
        } finally {
            if (client != null) {
                client.close();
            }
        }
    }
}
