数据平台Open-API SDK使用说明

: 非Java技术栈的厂商,请参考《Open-API鉴权协议》自行实现。

JAVA SDK

实例化客户端


 // 实例化API客户端对象
 // accessId: hzGD1JNK7u4vDtHlqyrt
 // secretKey: QjYLk+R7lUSvaPkTNOub4TdFg9yehRSCh9YXB31Hn
 ApiClient apiClient = new ApiClientImpl("hzGD1JNK7u4vDtHlqyrt", "QjYLk+R7lUSvaPkTNOub4TdFg9yehRSCh9YXB31Hn");

客户端方法


 /**
 * Created by pengran on 2018/3/2.
 * 数据平台API客户端
 */
public interface ApiClient {


    /**
     * 发送Get请求
     * <p>contentType 默认为 application/json; charset=UTF-8 </p>
     *
     * @param api RESTFUL API uri
     * @return api response
     */
    ApiResponse get(String api);

    /**
     * 发送POST请求
     * <p>contentType 默认为 application/json; charset=UTF-8 </p>
     *
     * @param api  RESTFUL API uri
     * @param data Post body content
     * @return api response
     */
    ApiResponse post(String api, String data);

    /**
     * 发送PUT请求
     * <p>contentType 默认为 application/json; charset=UTF-8 </p>
     *
     * @param api  RESTFUL API uri
     * @param data Post body content
     * @return api response
     */
    ApiResponse put(String api, String data);

    /**
     * 发送DELETE请求
     * <p>contentType 默认为 application/json; charset=UTF-8 </p>
     *
     * @param api RESTFUL API uri
     * @return api response
     */
    ApiResponse delete(String api);

    /**
     * 发送Get请求
     *
     * @param contentType HTTP Content-Type
     * @param api         RESTFUL API uri
     * @return api response
     */
    ApiResponse get(String contentType, String api);

    /**
     * 发送POST请求
     *
     * @param contentType HTTP Content-Type
     * @param api         RESTFUL API uri
     * @param data        Post body content
     * @return api response
     */
    ApiResponse post(String contentType, String api, String data);

    /**
     * 发送PUT请求
     *
     * @param contentType HTTP Content-Type
     * @param api         RESTFUL API uri
     * @param data        Post body content
     * @return api response
     */
    ApiResponse put(String contentType, String api, String data);

    /**
     * 发送DELETE请求
     *
     * @param contentType HTTP Content-Type
     * @param api         RESTFUL API uri
     * @return api response
     */
    ApiResponse delete(String contentType, String api);
}

返回结果对象 ApiResponse

  1. success() 判断Open-API调用是否成功, 成功返回为true, 失败返回false;
  2. 当success()为true时,getResponseText()用于获取接口响应的文本内容,getResponseStream()用于获取接口响应的字节数组流对象,客户端根据实际情况选择使用;
  3. 当success()为false时,getInvokeError()用于获取错误信息对象,错误对象属性说明如下:
对应字段 说明
getErrorCode() 获取错误信息编码
getErrorMsg() 获取错误信息
getSignature() 当网关鉴权失败,获取原始签名字符串用于问题分析, 若为服务端异常,该字段为null
getEncryptedSignature() 当网关鉴权失败,获取加密后的签名字符串用于问题分析,若为服务端异常,该字段为null
getAccessId() 当网关鉴权失败,获取accessId用于问题分析, 若为服务端异常,该字段为null

使用示例


package com.mc.dp.api.client;


import com.mc.dp.api.client.model.ApiResponse;
import lombok.SneakyThrows;
import org.junit.Assert;
import org.junit.Test;

/**
 * Unit test for simple App.
 */
public class AppTest {

    @Test
    @SneakyThrows
    public void post() {
        // 定义RESTFUL API接口地址
        String api = "https://dev.mctech.vip/api/iwop/sequence/next";
        // 实例化API客户端对象
        ApiClient apiClient = new ApiClientImpl("hzGD1JNK7u4vDtHlqyrt", "QjYLk+R7lUSvaPkTNOub4TdFg9yehRSCh9YXB31Hn");
        // 构造POST内容
        String postData = "{\n" +
                "    \"a\": 343434\n" +
                "}";
        // 调用API客户端对象方法 发送POST 请求
        ApiResponse response = apiClient.post(api, postData);

        // 判断调用是否成功
        // 发生调用错误 获取错误信息
        if (!response.success()) {
            // 获取错误对象
            InvokeError invokeError = response.getInvokeError();
             // 获取错误编码
            System.out.println(invokeError.getErrorCode());
             // 获取错误信息
            System.out.println(invokeError.getErrorMsg());
             // api gateway鉴权失败 则可获取sdk生成的签名用于问题分析
            System.out.println(invokeError.getSignature());
             // api gateway鉴权失败 则可获取sdk生成的加密签名用于问题分析
            System.out.println(invokeError.getEncryptedSignature());
            // api gateway鉴权失败 则可获取sdk生成签名所使用的accessId
            System.out.println(invokeError.getAccessId());
        } else {
            // 请求成功 获取响应内容
            System.out.println(response.getResponseText());
        }
    }

}
Copyright © 北京梦诚科技有限公司 2020 all right reserved,powered by Gitbook该文件修订时间: 2022-08-25 11:05:18

results matching ""

    No results matching ""