小豹API网关提供一个jar包用于简化访问API网关,主要处理登录换取token,超时刷新token,异常等。

请下载quickstart代码。 核心代码如下:

public class APITest {
    private static Logger logger = LoggerFactory.getLogger(APITest.class);
    private String appID = "fcd2fe1f-b44b-4a7c-af1d-35fd9de16017";
    private String authKey = "123456";
    private String gatewayAddr = "112.124.45.134";
    private Integer gatewayPort = 9000;
    private Integer gatewayPortOfSSL=9001;
    private Integer requestExpireTime = 2000;
    APIClientService apiClientService = new APIClientService.Builder()
            .appID(this.appID)
            .appAuthKey(this.authKey)
            .gatewayAddr(this.gatewayAddr)
            .gatewayPort(this.gatewayPort)
            .gatewayPortOfSSL(this.gatewayPortOfSSL)
            .requestExpireTime(this.requestExpireTime)
            .build();

    @Test
    public void testCategoryListWithToken() {

        //构建请求
        Request req = new Request();
        req.setPath("/quickstart/subject/categoryList");
        req.setMethod(HttpMethod.GET);
        req.setRequestId(UUID.randomUUID().toString());

        apiClientService.sendRequest(req, new IResponseHandler() {
            @Override
            public void onFailure(Request request, RespErrMsg respErrMsg) {
                logger.error("Error Code: " + respErrMsg.getErrCode() + ", Error Msg: " + respErrMsg.getErrMsg());
                Assert.fail();
            }

            @Override
            public void onResponse(Request request, Response response) {
                logger.info(response.getBody());
                Assert.assertTrue(true);
            }
        });
    }

}

results matching ""

    No results matching ""