package com.iflytek.aicloud.http;

import com.alibaba.fastjson.JSONObject;
import com.iflytek.aicloud.common.config.PropertiesConfig;
import com.iflytek.aicloud.common.dealer.ResultDealer;

import java.io.IOException;
import java.security.SignatureException;
import java.util.Map;

public class Main {
    private static final String appId = PropertiesConfig.getAppId();
    private static final String apiKey = PropertiesConfig.getApiKey();
    private static final String apiSecret = PropertiesConfig.getApiSecret();

    public static void main(String[] args) throws IOException, SignatureException {
        String hostUrl = RequestMetaData.getHostUrl();
        String jsonStringRequestData = RequestMetaData.getJsonStringRequestData();
        Map<String, String> requestPathMap = RequestMetaData.getRequestPathMap();

        checkDataValidate(hostUrl, jsonStringRequestData, requestPathMap);

        AppClient appClient = new AppClient(appId, apiKey, apiSecret, hostUrl);
        String respData = appClient.doRequest(JSONObject.parseObject(jsonStringRequestData), requestPathMap);

        ResultDealer.respDataPostProcess(respData, RequestMetaData.getResponsePathList());
    }

    private static void checkDataValidate(String hostUrl, String jsonData, Map<String, String> requestPathMap) {
        if (hostUrl == null || hostUrl.isEmpty()) {
            throw new RuntimeException("请求地址不能为空");
        }
        if (jsonData == null || jsonData.isEmpty()) {
            throw new RuntimeException("请求协议不能为空");
        }
        if (requestPathMap == null || requestPathMap.isEmpty()) {
            throw new RuntimeException("请求数据路径不能不为空");
        }
    }
}
