├── README.md ├── pom.xml └── src └── main └── java └── com └── aliyun └── iotx └── api └── client ├── IoTApiClientBuilderParams.java ├── IoTApiRequest.java ├── RequestDemo.java ├── SyncApiClient.java └── SyncApiGetClient.java /README.md: -------------------------------------------------------------------------------- 1 | 2 | # 介绍 3 | 阿里云IoT网关,旨在通过http/https协议将[物联网平台](https://help.aliyun.com/product/30520.html?spm=a2c4g.750001.7.4.XRrIyT)及衍生服务开放给开发者。本文档主要介绍如何访问阿里云IoT网关。IoT网关依赖阿里云[API网关](https://help.aliyun.com/product/29462.html?spm=a2c4g.750001.3.44.ieDVIY),IoT网关在阿里云API网关的基础上扩展了IoT的特殊业务协议。 4 | 5 | # 协议框架 6 | 7 | ```json 8 | { 9 | "id": "1509086454180", 10 | "version": "1.0", 11 | "request": { }, 12 | "params": { } 13 | } 14 | ``` 15 | 16 | **id**: 一次请求的标示,用于请求跟踪问题排查,通常用UUID生成
17 | **version**: 协议版本,当前版本1.0
18 | **request**: JSON对象,与业务无关的通用参数,包括如下可用参数
19 | 20 | *apiVer*:API版本 必填
21 | *iotToken*:登录用户的token(客户端) 非必填
22 | *cloudToken*:云端资源token(云端) 非必填
23 | *language*:国际化扩展,语言。 非必填
24 | *locale*:国际化扩展,地理位置、ip。 非必填
25 | 26 | **params**: JSON对象,访问指定api的业务参数
27 | # 示例 28 | 假设您需要访问IoT网关提供的如下API:
29 | **定义描述**
30 | ``` 31 | path:/awss/enrollee/list/get 32 | 版本:1.0.2 33 | 描述:分页查询发现设备列表,用于配网流程 34 | 鉴权:是,客户端SDK需启用身份的鉴权 35 | ``` 36 | 37 | **请求参数**
38 | ``` 39 | 参数名称:pageSize,参数类型:Integer,是否必填:是,描述:分页大小 40 | 参数名称:pageNum,参数类型:Integer,是否必填:是,描述:页编号 41 | ``` 42 | 43 | 44 | **请求示例** 45 | 版本1.0.2对应request中的apiVer,由于API要求用户身份验证,request中必须包含参数iotToken。请求参数统一放到params中。 46 | 47 | ```json 48 | { 49 | "id": "1509086454180", 50 | "version": "1.0", 51 | "request": { 52 | "apiVer": "1.0.2", 53 | "iotToken": "token" 54 | }, 55 | "params": { 56 | "pageSize": 10, 57 | "pageNum": 1 58 | } 59 | } 60 | ``` 61 | 62 | 假设您已经拥有了访问IoT网关的appKey和appSecret 分别为appkey1和appsecret1,通过IoT网关访问如下: 63 | ``` 64 | IoTApiClientBuilderParams ioTApiClientBuilderParams = new IoTApiClientBuilderParams(); 65 | 66 | ioTApiClientBuilderParams.setAppKey("xxxx"); 67 | ioTApiClientBuilderParams.setAppSecret("xxxxxxxxxx"); 68 | 69 | SyncApiClient syncApiClient = new SyncApiClient(ioTApiClientBuilderParams); 70 | 71 | IoTApiRequest request = new IoTApiRequest(); 72 | //设置协议版本号 73 | request.setVersion("1.0"); 74 | String uuid = UUID.randomUUID().toString(); 75 | String id = uuid.replace("-", ""); 76 | //设置请求ID 77 | request.setId(id); 78 | //设置API版本号 79 | request.setApiVer("1.0.2"); 80 | //如果需要登陆,设置当前的会话的token,token通过登录api获取 81 | request.setIoTToken("token"); 82 | //设置参数 83 | request.putParam("pageSize", 10); 84 | request.putParam("pageNum", 1); 85 | 86 | Map headers = new HashMap<>(8); 87 | 88 | //设置请求参数域名、path、request ,如果使用HTTPS,设置为true 89 | ApiResponse response = syncApiClient.postBody("api.link.aliyun.com", "/awss/enrollee/list/get", request, true, headers); 90 | 91 | System.out.println( "response code = " + response.getCode() + " response = " + new String(response.getBody(), "UTF-8")); 92 | ``` 93 | 94 | # 依赖 95 | ``` 96 | 97 | com.aliyun.api.gateway 98 | sdk-core-java 99 | 1.1.0 100 | 101 | ``` 102 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | com.aliyun.iotx 7 | iotx-api-gateway-client 8 | jar 9 | iotx-api-gateway-client 10 | 1.0.0 11 | 12 | 13 | UTF-8 14 | 1.8 15 | 16 | 17 | 18 | 19 | 20 | com.alibaba 21 | fastjson 22 | 1.2.28 23 | 24 | 25 | 26 | com.github.fge 27 | json-schema-core 28 | 1.2.5 29 | 30 | 31 | 32 | com.aliyun.api.gateway 33 | sdk-core-java 34 | 1.1.0 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | org.apache.maven.plugins 44 | maven-compiler-plugin 45 | 46 | ${java.version} 47 | ${java.version} 48 | ${project.build.sourceEncoding} 49 | 50 | 51 | 52 | org.apache.maven.plugins 53 | maven-resources-plugin 54 | 55 | ${project.build.sourceEncoding} 56 | 57 | 58 | 59 | 60 | org.apache.maven.plugins 61 | maven-source-plugin 62 | 63 | 64 | attach-sources 65 | 66 | jar 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/iotx/api/client/IoTApiClientBuilderParams.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.iotx.api.client; 2 | 3 | import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams; 4 | 5 | /** 6 | * @author yuxing.liyuxing@alibaba-inc.com 7 | * @date 2018/9/7 8 | */ 9 | public class IoTApiClientBuilderParams extends HttpClientBuilderParams { 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/iotx/api/client/IoTApiRequest.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.iotx.api.client; 2 | 3 | import java.util.HashMap; 4 | import java.util.Map; 5 | import java.util.UUID; 6 | 7 | /** 8 | * @author zhongfu.xiezf on 17/10/18. 9 | */ 10 | public class IoTApiRequest { 11 | private String id; 12 | private String version = "1.0"; 13 | private Map request = new HashMap(); 14 | private Map params = new HashMap(); 15 | 16 | public IoTApiRequest() { 17 | id = UUID.randomUUID().toString(); 18 | } 19 | 20 | public String getId() { 21 | return id; 22 | } 23 | 24 | public void setId(String id) { 25 | this.id = id; 26 | } 27 | 28 | public String getVersion() { 29 | return version; 30 | } 31 | 32 | public void setVersion(String version) { 33 | this.version = version; 34 | } 35 | 36 | public Map getRequest() { 37 | return request; 38 | } 39 | 40 | public void setRequest(Map request) { 41 | this.request = request; 42 | } 43 | 44 | public Map getParams() { 45 | return params; 46 | } 47 | 48 | public void setParams(Map params) { 49 | this.params = params; 50 | } 51 | 52 | public void setApiVer(String apiVer) { 53 | request.put("apiVer", apiVer); 54 | } 55 | 56 | public void setIotToken(String iotToken) { 57 | request.put("iotToken", iotToken); 58 | } 59 | 60 | public void setCloudToken(String cloudToken) { 61 | request.put("cloudToken", cloudToken); 62 | } 63 | 64 | public void putParam(String key, Object value) { 65 | params.put(key, value); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/iotx/api/client/RequestDemo.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.iotx.api.client; 2 | /* 3 | * Copyright 2017 Alibaba Group 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | import java.io.UnsupportedEncodingException; 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | import java.util.UUID; 22 | 23 | import com.alibaba.cloudapi.sdk.model.ApiResponse; 24 | 25 | /** 26 | * @author zhongfu.xiezf 27 | */ 28 | public class RequestDemo { 29 | 30 | public static void main(String[] args) throws UnsupportedEncodingException { 31 | postRequestDemo(); 32 | getRequestDemo(); 33 | } 34 | 35 | public static void postRequestDemo() throws UnsupportedEncodingException { 36 | IoTApiClientBuilderParams builderParams = new IoTApiClientBuilderParams(); 37 | builderParams.setAppKey("xxxx"); 38 | builderParams.setAppSecret("xxxx"); 39 | SyncApiClient syncClient = new SyncApiClient(builderParams); 40 | 41 | IoTApiRequest request = new IoTApiRequest(); 42 | //设置api的版本 43 | request.setApiVer("1.0.0"); 44 | request.setId("42423423"); 45 | 46 | //如果需要登陆,设置当前的会话的token 47 | request.setIotToken("xxxxxxxxxxxxxxx"); 48 | 49 | //设置参数 50 | request.putParam("xxxx", "xxxxx"); 51 | 52 | //请求参数域名、path、request 53 | String host = "xxx.xxx.xxx:8080"; 54 | String path = "/xxxx/xxxx/xxxx"; 55 | ApiResponse response = syncClient.postBody(host, path, request); 56 | 57 | System.out.println( 58 | "response code = " + response.getCode() + " response content = " + new String(response.getBody(), 59 | "utf-8")); 60 | } 61 | 62 | public static void getRequestDemo() throws UnsupportedEncodingException { 63 | IoTApiClientBuilderParams builderParams = new IoTApiClientBuilderParams(); 64 | builderParams.setAppKey("xxxx"); 65 | builderParams.setAppSecret("xxxx"); 66 | SyncApiGetClient syncClient = new SyncApiGetClient(builderParams); 67 | 68 | Map headers = new HashMap<>(8); 69 | 70 | Map querys = new HashMap<>(8); 71 | // 必填 72 | querys.put("apiVer", "1.0.0"); 73 | querys.put("id", UUID.randomUUID().toString()); 74 | // 可选 75 | querys.put("iotToken", "XXXXXXXXXX"); 76 | querys.put("xxxxx", "xxxxxx"); 77 | 78 | //请求参数域名、path、request 79 | String host = "xxx.xxx.xxx:8080"; 80 | String path = "/xxxx/xxxx/xxxx"; 81 | ApiResponse response = syncClient.doGet(host, path, true, headers, querys); 82 | 83 | System.out.println( 84 | "response code = " + response.getCode() + " response content = " + new String(response.getBody(), 85 | "utf-8")); 86 | } 87 | } 88 | 89 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/iotx/api/client/SyncApiClient.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.iotx.api.client; 2 | /* 3 | * Copyright 2017 Alibaba Group 4 | * 5 | * Licensed under the Apache License, Version 2.0 (the "License"); 6 | * you may not use this file except in compliance with the License. 7 | * You may obtain a copy of the License at 8 | * 9 | * http://www.apache.org/licenses/LICENSE-2.0 10 | * 11 | * Unless required by applicable law or agreed to in writing, software 12 | * distributed under the License is distributed on an "AS IS" BASIS, 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | * See the License for the specific language governing permissions and 15 | * limitations under the License. 16 | */ 17 | 18 | import java.io.UnsupportedEncodingException; 19 | import java.util.Arrays; 20 | import java.util.Map; 21 | 22 | import com.alibaba.cloudapi.sdk.annotation.ThreadSafe; 23 | import com.alibaba.cloudapi.sdk.client.ApacheHttpClient; 24 | import com.alibaba.cloudapi.sdk.enums.HttpMethod; 25 | import com.alibaba.cloudapi.sdk.enums.Scheme; 26 | import com.alibaba.cloudapi.sdk.model.ApiRequest; 27 | import com.alibaba.cloudapi.sdk.model.ApiResponse; 28 | import com.alibaba.fastjson.JSONObject; 29 | 30 | import static com.alibaba.cloudapi.sdk.enums.HttpConnectionModel.MULTIPLE_CONNECTION; 31 | 32 | /** 33 | * @author zhongfu.xiezf 34 | */ 35 | @ThreadSafe 36 | public final class SyncApiClient extends ApacheHttpClient { 37 | 38 | public SyncApiClient(IoTApiClientBuilderParams builderParams) { 39 | super.init(builderParams); 40 | } 41 | 42 | public ApiResponse postBody(String host, String path, IoTApiRequest request, boolean isHttps, 43 | Map headers) 44 | throws UnsupportedEncodingException { 45 | byte[] body = JSONObject.toJSONString(request).getBytes("UTF-8"); 46 | ApiRequest apiRequest = new ApiRequest(HttpMethod.POST_BODY, path, 47 | body); 48 | apiRequest.setHttpConnectionMode(MULTIPLE_CONNECTION); 49 | apiRequest.setScheme(isHttps ? Scheme.HTTPS : Scheme.HTTP); 50 | apiRequest.setHost(host); 51 | if (null != headers && headers.size() > 0) { 52 | for (Map.Entry header : headers.entrySet()) { 53 | apiRequest.getHeaders().put(header.getKey(), Arrays.asList(header.getValue())); 54 | } 55 | } 56 | return sendSyncRequest(apiRequest); 57 | } 58 | 59 | public ApiResponse postBody(String host, String path, IoTApiRequest request, boolean isHttps) 60 | throws UnsupportedEncodingException { 61 | 62 | return postBody(host, path, request, isHttps, null); 63 | } 64 | 65 | public ApiResponse postBody(String host, String path, IoTApiRequest request) 66 | throws UnsupportedEncodingException { 67 | return postBody(host, path, request, false, null); 68 | } 69 | 70 | } 71 | 72 | -------------------------------------------------------------------------------- /src/main/java/com/aliyun/iotx/api/client/SyncApiGetClient.java: -------------------------------------------------------------------------------- 1 | package com.aliyun.iotx.api.client; 2 | 3 | import com.alibaba.cloudapi.sdk.client.ApacheHttpClient; 4 | import com.alibaba.cloudapi.sdk.enums.HttpMethod; 5 | import com.alibaba.cloudapi.sdk.enums.Scheme; 6 | import com.alibaba.cloudapi.sdk.model.ApiRequest; 7 | import com.alibaba.cloudapi.sdk.model.ApiResponse; 8 | 9 | import java.util.Arrays; 10 | import java.util.Map; 11 | 12 | import static com.alibaba.cloudapi.sdk.enums.HttpConnectionModel.MULTIPLE_CONNECTION; 13 | 14 | /** 15 | * @author zhenzhao.czz 16 | * @date 2018/2/2 17 | */ 18 | public class SyncApiGetClient extends ApacheHttpClient { 19 | 20 | public SyncApiGetClient(IoTApiClientBuilderParams builderParams) { 21 | super.init(builderParams); 22 | } 23 | 24 | public ApiResponse doGet(String host, String path, boolean isHttps, Map headers, 25 | Map querys) { 26 | 27 | ApiRequest apiRequest = new ApiRequest(HttpMethod.GET, path); 28 | apiRequest.setHttpConnectionMode(MULTIPLE_CONNECTION); 29 | apiRequest.setScheme(isHttps ? Scheme.HTTPS : Scheme.HTTP); 30 | apiRequest.setHost(host); 31 | if (null != headers && headers.size() > 0) { 32 | for (Map.Entry header : headers.entrySet()) { 33 | apiRequest.getHeaders().put(header.getKey(), Arrays.asList(header.getValue())); 34 | } 35 | } 36 | if (null != querys && querys.size() > 0) { 37 | for (Map.Entry query : querys.entrySet()) { 38 | apiRequest.getQuerys().put(query.getKey(), query.getValue()); 39 | } 40 | } 41 | return sendSyncRequest(apiRequest); 42 | } 43 | } 44 | --------------------------------------------------------------------------------