package com.aucma.api.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;

import java.util.Base64;

/**
 * @Author YinQ
 * @create 2023-10-12 10:52
 */
public class ApiUtils {


    /**
     * POST请求json格式
     * @param apiUrl
     * @param jsonData
     * @return
     */
    public static String sendSAPHttpPost(String apiUrl, String jsonData) {
        try {
            // 构建POST请求
            HttpRequest request = HttpRequest.post(apiUrl)
                    .header("Authorization", "Basic " + Base64.getUrlEncoder().
                            encodeToString((SAPConstants.USER_NAME + ":" + SAPConstants.PASS_WORD).getBytes()))
                    .body(jsonData)  // 设置JSON格式的请求体
                    .contentType("application/json");  // 设置Content-Type为application/json

            // 发送POST请求
            HttpResponse httpResponse = request.execute();

            // 获取响应内容
            return httpResponse.body();
        } catch (Exception e) {
            // 捕获异常并重新抛出自定义异常
            throw new RuntimeException("Failed to send SAP HTTP POST request: " + e.getMessage(), e);
        }

    }
}
