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

com.dahuatech.icc.multiinone.event.EventSubcribeSDK Maven / Gradle / Ivy

The newest version!
package com.dahuatech.icc.multiinone.event;

import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.hutool.log.Log;
import com.dahuatech.hutool.log.LogFactory;
import com.dahuatech.icc.multiinone.Constants;
import com.dahuatech.icc.multiinone.event.vo.CancelSubcribeRequest;
import com.dahuatech.icc.multiinone.event.vo.EventSubcribeRequest;
import com.dahuatech.icc.multiinone.event.vo.SimpleEventSubcribeRequest;
import com.dahuatech.icc.multiinone.exception.BusinessException;
import com.dahuatech.icc.multiinone.utils.EventParamsUtils;
import com.dahuatech.icc.multiinone.vo.BaseResponse;
import com.dahuatech.icc.oauth.handle.InitVersionProcessor;
import com.dahuatech.icc.oauth.http.IccClient;
import com.dahuatech.icc.oauth.model.v202010.GeneralRequest;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;

/**
 * 事件订阅SDK
 */
public class EventSubcribeSDK {

    private static final Log logger = LogFactory.get();

    private final String subcribeUrlTemplate = "/evo-apigw/evo-event/%s/subscribe/mqinfo";
    private final String cancelSubcribeUrlTemplate = "/evo-apigw/evo-event/%s/subscribe/mqinfo?name=%s";

    /**
     * 简易事件订阅
     * @param request
     * @return
     */
    public BaseResponse simpleEventSubcribe(SimpleEventSubcribeRequest request){
        BaseResponse baseResponse = new BaseResponse();
        try{
            request.valid();
            EventSubcribeRequest eventSubcribeRequest = new EventSubcribeRequest();
            eventSubcribeRequest.setOauthConfigBaseInfo(request.getOauthConfigBaseInfo());
            eventSubcribeRequest.setSubscribeVo(EventParamsUtils.simpleSubscribeVo(
                    request.getReceiveIp(),request.getReceivePort(),request.getUri(),request.isHttps(),request.getName(),request.getTypesConfigs()));
            return eventSubscribe(eventSubcribeRequest);
        }catch (BusinessException businessException){
            logger.error("简易事件订阅失败{}",businessException,businessException.getMessage());
            baseResponse.setCode(businessException.code);
            baseResponse.setErrMsg(businessException.msg);
            return baseResponse;
        }catch (Exception e){
            logger.error("简易事件订阅异常{}",e,e.getMessage());
            baseResponse.setCode(Constants.SYSTEMERROR_CODE);
            baseResponse.setErrMsg(Constants.SYSTEMERROR_MSG);
            return baseResponse;
        }

    }

    public BaseResponse eventSubscribe(EventSubcribeRequest eventSubcribeRequest){
        BaseResponse baseResponse = new BaseResponse();
        try{
            eventSubcribeRequest.valid();
            IccClient iccClient = new IccClient(eventSubcribeRequest.getOauthConfigBaseInfo());
            String url = String.format(subcribeUrlTemplate, InitVersionProcessor.systemVersionMap.get(eventSubcribeRequest.getOauthConfigBaseInfo().getHttpConfigInfo().getHost() + "evo-event"));
            GeneralRequest generalRequest = new GeneralRequest(eventSubcribeRequest.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.POST);
            GeneralResponse generalResponse = null;
            try{
                generalRequest.setBody(JSONUtil.toJsonStr(eventSubcribeRequest.getSubscribeVo()));
                generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
            }catch (Exception e){
                logger.error("事件订阅异常{}",e,e.getMessage());
                throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
            }
            if(!generalResponse.isSuccess()){
                logger.error("事件订阅失败code:{}-msg:{}",generalResponse.getCode(),generalResponse.getErrMsg());
                throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
            }
        }catch (BusinessException businessException){
            logger.error("事件订阅失败{}",businessException,businessException.getMessage());
            baseResponse.setCode(businessException.code);
            baseResponse.setErrMsg(businessException.msg);
            return baseResponse;
        }catch (Exception e){
            logger.error("事件订阅异常{}",e,e.getMessage());
            baseResponse.setCode(Constants.SYSTEMERROR_CODE);
            baseResponse.setErrMsg(Constants.SYSTEMERROR_MSG);
            return baseResponse;
        }
        baseResponse.setErrMsg(Constants.SUCCESS_MSG);
        baseResponse.setCode(Constants.SUCESS_CODE);
        return baseResponse;
    }


    public BaseResponse cancelSubcribe(CancelSubcribeRequest eventSubcribeRequest){
        BaseResponse baseResponse = new BaseResponse();
        try{
            eventSubcribeRequest.valid();
            IccClient iccClient = new IccClient(eventSubcribeRequest.getOauthConfigBaseInfo());
            String url = String.format(cancelSubcribeUrlTemplate,
                    InitVersionProcessor.systemVersionMap.get(eventSubcribeRequest.getOauthConfigBaseInfo().getHttpConfigInfo().getHost() + "evo-event"),eventSubcribeRequest.getName());
            GeneralRequest generalRequest = new GeneralRequest(eventSubcribeRequest.getOauthConfigBaseInfo().getHttpConfigInfo(),url, Method.DELETE);
            GeneralResponse generalResponse = null;
            try{
                generalResponse = iccClient.doAction(generalRequest,generalRequest.getResponseClass());
            }catch (Exception e){
                logger.error("取消事件订阅异常{}",e,e.getMessage());
                throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
            }
            if(!generalResponse.isSuccess()){
                logger.error("取消事件订阅失败code:{}-msg:{}",generalResponse.getCode(),generalResponse.getErrMsg());
                throw new BusinessException(generalResponse.getCode(),generalResponse.getErrMsg());
            }
        }catch (BusinessException businessException){
            logger.error("取消事件订阅失败{}",businessException,businessException.getMessage());
            baseResponse.setCode(businessException.code);
            baseResponse.setErrMsg(businessException.msg);
            return baseResponse;
        }catch (Exception e){
            logger.error("取消事件订阅异常{}",e,e.getMessage());
            baseResponse.setCode(Constants.SYSTEMERROR_CODE);
            baseResponse.setErrMsg(Constants.SYSTEMERROR_MSG);
            return baseResponse;
        }
        baseResponse.setErrMsg(Constants.SUCCESS_MSG);
        baseResponse.setCode(Constants.SUCESS_CODE);
        return baseResponse;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy