All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dahuatech.icc.oauth.utils.HttpUtils Maven / Gradle / Ivy

The newest version!
package com.dahuatech.icc.oauth.utils;

import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.http.IccClient;
import com.dahuatech.icc.oauth.http.IccTokenResponse;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigUserPwdInfo;
import com.dahuatech.icc.util.CollectionUtil;

import java.util.Map;

public class HttpUtils {


    /**
     * 执行HTTP请求
     *
     * @param url           请求地址
     * @param jsonData     JSON参数
     * @param formParams    表单参数
     * @param headers       HTTP头部信息
     * @param method        请求方法
     * @param config        用户密码配置信息
     * @param responseClass 响应类型
     * @return 响应结果
     * @throws ClientException 客户端异常
     */
    public static  T execute(String url, Object jsonData, Map formParams, Map headers, Method method, OauthConfigUserPwdInfo config, Class responseClass) throws ClientException {
        IccClient iClient = new IccClient(config);
        GeneralRequest generalRequest = new GeneralRequest(config.getHttpConfigInfo(), url, method);
        if (jsonData != null)
            generalRequest.body(JSONUtil.toJsonStr(jsonData));
        if (formParams != null)
            generalRequest.form(formParams);
        if (!CollectionUtil.isEmpty(headers))
            generalRequest.setHeader(headers);
        return  iClient.doAction(generalRequest, responseClass);
    }

    /**
     * 执行JSON请求
     *
     * @param url           请求地址
     * @param jsonData     JSON参数
     * @param headers       请求头
     * @param method        请求方法
     * @param config        用户密码配置信息
     * @param responseClass 响应类型
     * @return 响应结果
     * @throws ClientException 客户端异常
     */
    public static  T executeJson(String url, Object jsonData, Map headers, Method method, OauthConfigUserPwdInfo config, Class responseClass) throws ClientException {
        return execute(url, jsonData, null, headers, method, config, responseClass);
    }

    /**
     * 执行表单请求
     *
     * @param url           请求地址
     * @param formParams    表单参数
     * @param headers       请求头
     * @param method        请求方法
     * @param config        用户密码配置信息
     * @param responseClass 响应类型
     * @return 响应结果
     * @throws ClientException 客户端异常
     */
    public static  T executeForm(String url, Map formParams, Map headers, Method method, OauthConfigUserPwdInfo config, Class responseClass) throws ClientException {
        return execute(url, null, formParams, headers, method, config, responseClass);
    }

    /**
     * 获取访问令牌
     *
     * @param config 用户名和密码配置信息
     * @return 访问令牌响应对象
     * @throws ClientException 客户端异常
     */
    public static IccTokenResponse.IccToken getToken(OauthConfigUserPwdInfo config) throws ClientException {
        IccClient iClient = new IccClient(config);
        return iClient.getAccessToken();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy