OpenApiInvoker.java 2.94 KB
package com.glsx.service.component;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONObject;
import com.glsx.constant.OpenAPIConstant;
import com.glsx.exception.ApiException;
import com.glsx.utils.SystemProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author fengzhi
 * @version 1.0.0
 * @ClassName OpenApiInvoker.java
 * @description: http协议接口
 * @createTime 2020年10月21日 14:26:00
 */
@Slf4j
@Component
public class OpenApiInvoker {
    /**
     *
     */
    private String invokeGet(String url,Map<String,Object> params) {
        if (params==null) {
            log.info("openapi data is NULL:"+url);
            return null;
        }

        String res = null;
        try {
            Integer timeOut = Integer.parseInt(SystemProperty.getProperty(OpenAPIConstant.HTTP_TIME_OUT));
            log.info("invoker openapiGet serverUrl:" + url);
            HttpResponse httpResponse = HttpRequest.get(url).form(params).timeout(timeOut * 1000).execute();
            log.info("httpResponse:"+httpResponse.getStatus());
            if(httpResponse.getStatus() == 200)
            res= httpResponse.body();
        } catch (Exception e) {
            log.error("invoker openapiGet error:" +url+"==="+ e.getMessage());
            throw new ApiException(e.getMessage());
        }

        log.info("invoker openapiGet api res:" + res);
        return res;
    }

    private String invokePost(String url,Map<String,Object> params) {
        if (params==null) {
            log.info("openapi data is NULL:"+url);
            return null;
        }

        String res = null;
        try {
            Integer timeOut = Integer.parseInt(SystemProperty.getProperty(OpenAPIConstant.HTTP_TIME_OUT));
            log.info("invoker openapiPost serverUrl:" + url);
            HttpResponse httpResponse = HttpRequest.post(url).body(JSONObject.toJSONString(params)).timeout(timeOut * 1000).execute();
            log.info("httpResponse:"+httpResponse.getStatus());
            if(httpResponse.getStatus() == 200)
                res= httpResponse.body();
        } catch (Exception e) {
            log.error("invoker openapiPost error:" +url+"==="+ e.getMessage());
            throw new ApiException(e.getMessage());
        }

        log.info("invoker openapiPost api res:" +url+"==="+ res);
        return res;
    }



    public String invokeFenceOrRescue(String methodName,Map<String,Object> params) {
        String serverUrl = SystemProperty.getProperty(OpenAPIConstant.HTTP_OPENAPI_SERVER_1);
        String url = serverUrl+methodName;
        return invokeGet(url,params);
    }

    public String invokCommandOrDeviceStatus(String methodName,Map<String,Object> params) {
        String serverUrl = SystemProperty.getProperty(OpenAPIConstant.HTTP_OPENAPI_SERVER_2);
        String url = serverUrl+methodName;
        return invokePost(url,params);
    }
}