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

org.sdn.api.sdk.impl.SDKOpenClientImpl Maven / Gradle / Ivy

The newest version!
package org.sdn.api.sdk.impl;

import com.alibaba.fastjson.JSON;
import org.sdn.api.DefaultOpenClient;
import org.sdn.api.OpenApiException;
import org.sdn.api.OpenClient;
import org.sdn.api.constants.ErrorMsgEnum;
import org.sdn.api.constants.Methods;
import org.sdn.api.constants.Msg;
import org.sdn.api.constants.Request;
import org.sdn.api.sdk.SDKOpenClient;
import org.sdn.api.utils.ObjectToMap;
import org.sdn.api.utils.UrlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Author: Tao.Wang
 * Date: 2018/12/14
 * Time: 4:39 PM
 * Org : 思笛恩
 * To change this template use File | Settings | File Templates.
 * Description: ServiceInterface Interface
 *
 * @author: Tao.Wang
 */
public class SDKOpenClientImpl implements SDKOpenClient {

    private static final Logger logger = LoggerFactory.getLogger(SDKOpenClientImpl.class);

    private String path;

    private String appkey;

    private String appSecret;

    /**
     * 是否初始化 Kafka 监听器
     */
    private boolean needSubscribe = false;

    OpenClient openClient;


    @Override
    public OpenClient getOpenClient() throws Exception {
        if (openClient == null) {
            try {
                openClient = new DefaultOpenClient(path, appkey, appSecret, needSubscribe);
            } catch (OpenApiException e) {
                if (e.getErrno() == ErrorMsgEnum.DUPLICATE_ERROR.getErrno()) {
                    openClient = DefaultOpenClient.getClient(path, appkey);
                }
            } catch (Exception e) {
                throw e;
            }
        }
        return openClient;
    }

    /**
     * 默认 Post
     *
     * @param path   路径
     * @param object 参数
     * @return
     */
    @Override
    public Msg forward(String path, Object object) throws Exception {
        return forward(path, object, null);
    }

    @Override
    public Msg forward(String path, Object object, Map headers) throws Exception {
        Map params = ObjectToMap.objectToMap(object);
        return forward(path, Methods.POST, params, headers);
    }

    @Override
    public Msg forward(String path, Map params) throws Exception {
        return forward(path, params, null);
    }

    @Override
    public Msg forward(String path, Map params, Map headers) throws Exception {
        return forward(path, Methods.POST, params, headers);
    }

    @Override
    public Msg forward(String path, String method, Object object) throws Exception {
        return forward(path, method, object, null);
    }

    public Msg forward(String path, String method, Object object, Map headers) throws Exception {
        Map params = ObjectToMap.objectToMap(object);
        return forward(path, method, params, headers);
    }

    @Override
    public Msg forward(String path, String method, Map params) throws Exception {
        return forward(path, method, params, null);
    }

    /**
     * @param path    路径
     * @param method  方法
     * @param params  参数
     * @param headers 请求头
     * @return
     */
    @Override
    public Msg forward(String path, String method, Map params, Map headers) throws Exception {
        try {
            switch (method) {
                case Methods.GET:
                case Methods.DELETE:
                    path = UrlUtils.getUrl(path, params);
                    params = null;
                    break;
                case Methods.POST:
                case Methods.PUT:
                    break;
                default:
                    logger.warn("暂不支持的请求方式");
                    return null;
            }
            String json = getOpenClient().defaultExecute(new Request(path, method, params, headers)).getBody();
            return JSON.parseObject(json, Msg.class);
        } catch (Exception e) {
            logger.error("访问路径={},方式={},参数={},error={}", path, method, JSON.toJSONString(params), e.getMessage());
            throw e;
        } finally {
            logger.info("访问路径={},方式={},参数={}", path, method, JSON.toJSONString(params));
        }
    }

    @Override
    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public void setAppkey(String appkey) {
        this.appkey = appkey;
    }

    @Override
    public void setAppSecret(String appSecret) {
        this.appSecret = appSecret;
    }

    @Override
    public void setNeedSubscribe(boolean needSubscribe) {
        this.needSubscribe = needSubscribe;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy