SendRequestService.java 2.4 KB
package cn.com.glsx.modules.service;

import cn.com.glsx.modules.model.param.ResponseBody;
import cn.com.glsx.modules.utils.HeaderParamUtils;
import cn.com.glsx.modules.utils.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

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

/**
 * @author zhouhaibao
 * @date 2021/3/5 14:37
 */
@Service
@Slf4j
public class SendRequestService {

    @Value("${tuya.client_id}")
    private String client_id;

    @Value("${tuya.secret}")
    private String secret;

    public ResponseBody sendRequestPost(String requesturl,String accessToken, String requestBody) {
        ResponseBody responseBody = null;
        Map<String, String> headerParams = HeaderParamUtils.buildHeaderParams(client_id, secret, accessToken);
        try {
            JSONObject jsonObject = HttpUtil.sendPost(requesturl, JSONObject.toJSONString(requestBody), headerParams);
            responseBody = JSON.toJavaObject(jsonObject, ResponseBody.class);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("调用失败");
        }
        return responseBody;
    }

    public ResponseBody sendRequestGet(String requesturl, String accessToken) {
        ResponseBody responseBody = null;
        Map<String, String> headerParams = HeaderParamUtils.buildHeaderParams(client_id, secret, accessToken);
        try {
            JSONObject jsonObject = HttpUtil.sendGet(requesturl, null, headerParams);
            responseBody = JSON.toJavaObject(jsonObject, ResponseBody.class);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("请求用户信息接口异常,异常信息{}", e);
        }
        return responseBody;
    }

    public ResponseBody sendRequestGet(String requesturl) {
        ResponseBody responseBody = null;
        Map<String, String> headerParams = HeaderParamUtils.buildHeaderParams(client_id, secret);
        try {
            JSONObject jsonObject = HttpUtil.sendGet(requesturl, null, headerParams);
            responseBody = JSON.toJavaObject(jsonObject, ResponseBody.class);
        } catch (IOException e) {
            e.printStackTrace();
            log.info("请求用户信息接口异常,异常信息{}", e);
        }
        return responseBody;
    }
}