com.dahuatech.icc.oauth.utils.HttpUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-sdk-oauth Show documentation
Show all versions of java-sdk-oauth Show documentation
Dahua ICC Open API SDK for Java
package com.dahuatech.icc.oauth.utils;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONObject;
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.GeneralResponse;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigUserPwdInfo;
import com.dahuatech.icc.util.CollectionUtil;
import java.util.HashMap;
import java.util.Map;
public class HttpUtils {
/**
* 发送HTTP请求
*
* @param url 请求地址
* @param bodyParams 请求体参数
* @param formdParams 请求表单参数
* @param headers 请求头
* @param method 请求方法
* @param config 用户密码配置信息
* @return 响应结果
* @throws ClientException 客户端异常
*/
public static GeneralResponse execute(String url, Object bodyParams, Object formdParams, Map headers, Method method, OauthConfigUserPwdInfo config) throws ClientException {
IccClient iClient = new IccClient(config);
GeneralRequest generalRequest = new GeneralRequest(config.getHttpConfigInfo(), url, method);
if (bodyParams != null) {//body体参数
generalRequest.body(JSONUtil.toJsonStr(bodyParams));
}
if (formdParams != null) {//form表单提交
JSONObject jsonObject = JSONUtil.parseObj(formdParams);
generalRequest.form(jsonObject.toBean(HashMap.class));
}
if (!CollectionUtil.isEmpty(headers)) {
generalRequest.setHeader(headers);
}
return iClient.doAction(generalRequest, generalRequest.getResponseClass());
}
/**
* 执行JSON请求
*
* @param url 请求地址
* @param bodyParams 请求参数
* @param method 请求方法
* @param config 用户密码配置信息
* @return 响应结果
* @throws ClientException 客户端异常
*/
public static GeneralResponse executeJson(String url, Object bodyParams, Method method, OauthConfigUserPwdInfo config) throws ClientException {
return execute(url, bodyParams, null, null, method, config);
}
/**
* 执行带有表单数据的请求
*
* @param url 请求地址
* @param formdParams 表单参数
* @param method 请求方法
* @param config 用户密码配置信息
* @return 通用响应对象
* @throws ClientException 客户端异常
*/
public static GeneralResponse executeFormData(String url, Object formdParams, Method method, OauthConfigUserPwdInfo config) throws ClientException {
return execute(url, null, formdParams, null, method, config);
}
/**
* 获取访问令牌
*
* @param config 用户名和密码配置信息
* @return 访问令牌响应对象
* @throws ClientException 客户端异常
*/
public static IccTokenResponse.IccToken getToken(OauthConfigUserPwdInfo config) throws ClientException {
IccClient iClient = new IccClient(config);
return iClient.getAccessToken();
}
}