http-api.md 10.1 KB
Newer Older
如梦技术's avatar
如梦技术 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
# Http Api 接口

### HTTP 状态码 (status codes)

接口在调用成功时总是返回 200 OK,响应内容则以 JSON 格式返回。

可能的状态码如下:

| Status Code | Description                                              |
| ----------- | -------------------------------------------------------- |
| 200         | 成功,返回的 JSON 数据将提供更多信息                     |
| 400         | 客户端请求无效,例如请求体或参数错误                     |
| 401         | 客户端未通过服务端认证,使用无效的身份验证凭据可能会发生 |
| 404         | 找不到请求的路径或者请求的对象不存在                     |
| 405         | 请求方法错误                     						 |
| 500         | 服务端处理请求时发生内部错误                             |

### 返回码 (result codes)

接口的响应消息体为 JSON 格式,其中总是包含返回码 `code`

可能的返回码如下:

| Return Code | Description                |
| ----------- | -------------------------- |
| 1           | 成功                       |
| 101         | 关键请求参数缺失           |
| 102         | 请求参数错误               |
| 103         | 用户名或密码错误           |
| 104         | 请求方法错误               |
| 105         | 未知错误                   |

33
## 获取所有 api 接口列表
浅梦2013's avatar
浅梦2013 已提交
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
### GET /api/v1/endpoints

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- |---------|-------------|
| code | Integer | 1           |
| data | Array   | 接口列表        |
| method | String  | 方法名         |
| path | String  | 路径          |

**Examples:**

```bash
$ curl -i --basic -u mica:mica "http://localhost:8083/api/v1/endpoints"

{
  "code": 1,
  "data": [
    {
      "method": "POST",
      "path": "/api/v1/mqtt/subscribe"
    },
    {
      "method": "POST",
      "path": "/api/v1/mqtt/unsubscribe/batch"
    },
    {
      "method": "GET",
      "path": "/api/v1/client/subscriptions"
    },
    {
      "method": "POST",
      "path": "/api/v1/clients/delete"
    },
    {
      "method": "GET",
      "path": "/api/v1/endpoints"
    },
    {
      "method": "POST",
      "path": "/api/v1/mqtt/publish"
    },
    {
      "method": "POST",
      "path": "/api/v1/mqtt/publish/batch"
    },
    {
      "method": "POST",
      "path": "/api/v1/mqtt/subscribe/batch"
    },
    {
      "method": "POST",
      "path": "/api/v1/mqtt/unsubscribe"
    }
  ]
}
```
如梦技术's avatar
如梦技术 已提交
93 94 95 96 97 98 99 100 101 102 103

## 消息发布

### POST /api/v1/mqtt/publish

发布 MQTT 消息。

**Parameters (json):**

| Name     | Type    | Required | Default | Description                                                 |
| -------- | ------- | -------- | ------- | ----------------------------------------------------------- |
如梦技术's avatar
如梦技术 已提交
104 105 106
| topic    | String  | Required |         | 主题                                                         |
| clientId | String  | Required |         | 客户端标识符                                                  |
| payload  | String  | Required |         | 消息正文                                                      |
如梦技术's avatar
如梦技术 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119
| encoding | String  | Optional | plain   | 消息正文使用的编码方式,目前仅支持 目前仅支持 `plain``hex``base64` |
| qos      | Integer | Optional | 0       | QoS 等级                                                    |
| retain   | Boolean | Optional | false   | 是否为保留消息                                              |

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

```bash
如梦技术's avatar
如梦技术 已提交
120
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/publish" -d '{"topic":"a/b/c","payload":"Hello World","qos":1,"retain":false,"clientId":"example"}'
如梦技术's avatar
如梦技术 已提交
121

122
{"code":1}
如梦技术's avatar
如梦技术 已提交
123 124 125 126 127 128 129 130 131 132 133 134
```

## 主题订阅

### POST /api/v1/mqtt/subscribe

订阅 MQTT 主题。

**Parameters (json):**

| Name     | Type    | Required | Default | Description                                           |
| -------- | ------- | -------- | ------- | ----------------------------------------------------- |
如梦技术's avatar
如梦技术 已提交
135 136
| topic    | String  | Required |         | 主题                                                  |
| clientId | String  | Required |         | 客户端标识符                                          |
如梦技术's avatar
如梦技术 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149
| qos      | Integer | Optional | 0       | QoS 等级                                              |

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

同时订阅 `a`, `b`, `c` 三个主题

```bash
如梦技术's avatar
如梦技术 已提交
150
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/subscribe" -d '{"topic":"a/b/c","qos":1,"clientId":"example"}'
如梦技术's avatar
如梦技术 已提交
151

152
{"code":1}
如梦技术's avatar
如梦技术 已提交
153 154 155 156 157 158 159 160 161 162 163
```

### POST /api/v1/mqtt/unsubscribe

取消订阅。

**Parameters (json):**

| Name     | Type   | Required | Default | Description  |
| -------- | ------ | -------- | ------- | ------------ |
| topic    | String | Required |         | 主题         |
如梦技术's avatar
如梦技术 已提交
164
| clientId | String | Required |         | 客户端标识符 |
如梦技术's avatar
如梦技术 已提交
165 166 167 168 169 170 171 172 173 174 175 176

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

取消订阅 `a` 主题

```bash
177
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/unsubscribe" -d '{"topic":"a","clientId":"example"}'
如梦技术's avatar
如梦技术 已提交
178

179
{"code":1}
如梦技术's avatar
如梦技术 已提交
180 181 182 183 184 185 186 187 188 189 190 191
```

## 消息批量发布

### POST /api/v1/mqtt/publish/batch

批量发布 MQTT 消息。

**Parameters (json):**

| Name         | Type    | Required | Default | Description                                                 |
| ------------ | ------- | -------- | ------- | ----------------------------------------------------------- |
如梦技术's avatar
如梦技术 已提交
192 193
| [0].topic    | String  | Required |         | 主题                                                         |
| [0].clientId | String  | Required |         | 客户端标识符                                                |
如梦技术's avatar
如梦技术 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207
| [0].payload  | String  | Required |         | 消息正文                                                    |
| [0].encoding | String  | Optional | plain   | 消息正文使用的编码方式,目前仅支持 `plain``hex``base64` |
| [0].qos      | Integer | Optional | 0       | QoS 等级                                                    |
| [0].retain   | Boolean | Optional | false   | 是否为保留消息                                              |

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

```bash
如梦技术's avatar
如梦技术 已提交
208
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/publish/batch" -d '[{"topic":"a/b/c","payload":"Hello World","qos":1,"retain":false,"clientId":"example"},{"topic":"a/b/c","payload":"Hello World Again","qos":0,"retain":false,"clientId":"example"}]'
如梦技术's avatar
如梦技术 已提交
209

210
{"code":1}
如梦技术's avatar
如梦技术 已提交
211 212 213 214 215 216 217 218 219 220 221 222
```

## 主题批量订阅

### POST /api/v1/mqtt/subscribe/batch

批量订阅 MQTT 主题。

**Parameters (json):**

| Name         | Type    | Required | Default | Description                                           |
| ------------ | ------- | -------- | ------- | ----------------------------------------------------- |
如梦技术's avatar
如梦技术 已提交
223 224
| [0].topic    | String  | Required |         | 主题                                                  |
| [0].clientId | String  | Required |         | 客户端标识符                                          |
如梦技术's avatar
如梦技术 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237
| [0].qos      | Integer | Optional | 0       | QoS 等级                                              |

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

一次性订阅 `a`, `b`, `c` 三个主题

```bash
如梦技术's avatar
如梦技术 已提交
238
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/subscribe/batch" -d '[{"topic":"a","qos":1,"clientId":"example"},{"topic":"b","qos":1,"clientId":"example"},{"topic":"c","qos":1,"clientId":"example"}]'
如梦技术's avatar
如梦技术 已提交
239

240
{"code":1}
如梦技术's avatar
如梦技术 已提交
241 242 243 244 245 246 247 248 249 250 251
```

### POST /api/v1/mqtt/unsubscribe/batch

批量取消订阅。

**Parameters (json):**

| Name         | Type   | Required | Default | Description  |
| ------------ | ------ | -------- | ------- | ------------ |
| [0].topic    | String | Required |         | 主题         |
如梦技术's avatar
如梦技术 已提交
252
| [0].clientId | String | Required |         | 客户端标识符 |
如梦技术's avatar
如梦技术 已提交
253 254 255 256 257 258 259 260 261 262 263 264

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

一次性取消订阅 `a`, `b` 主题

```bash
265
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/mqtt/unsubscribe/batch" -d '[{"topic":"a","clientId":"example"},{"topic":"b","clientId":"example"}]'
如梦技术's avatar
如梦技术 已提交
266

267
{"code":1}
如梦技术's avatar
如梦技术 已提交
268
```
浅梦2013's avatar
浅梦2013 已提交
269 270 271

## 踢除指定客户端

272
### POST /api/v1/clients/delete
浅梦2013's avatar
浅梦2013 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

踢除指定客户端。注意踢除客户端操作会将连接与会话一并终结。

**Query Parameters:**

| Name     | Type   | Required | Description |
| -------- | ------ | -------- | ----------- |
| clientId | String | True     | ClientID    |

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- | ------- | ----------- |
| code | Integer | 0           |

**Examples:**

踢除指定客户端

```bash
$ curl -i --basic -u mica:mica -X POST "http://localhost:8083/api/v1/clients/delete?clientId=123"

295
{"code":1}
浅梦2013's avatar
浅梦2013 已提交
296 297
```

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
## 获取客户端订阅情况

### GET /api/v1/client/subscriptions

获取指定客户端订阅详情。

**Query Parameters:**

| Name     | Type   | Required | Description |
| -------- | ------ | -------- | ----------- |
| clientId | String | True     | ClientID    |

**Success Response Body (JSON):**

| Name | Type    | Description |
| ---- |---------|-------------|
| code | Integer | 0           |
| data | Array   | []          |
| topicFilter | String   |             |
| clientId | String  |             |
| mqttQoS | Integer | 0           |

**Examples:**

踢除指定客户端

```bash
$ curl -i --basic -u mica:mica "http://127.0.0.1:8083/api/v1/client/subscriptions?clientId=123"

{
  "code": 1,
  "data": [
    {
      "clientId": "123",
      "mqttQoS": 0,
      "topicFilter": "#"
    },
    {
      "clientId": "123",
      "mqttQoS": 0,
      "topicFilter": "testtopic/#"
    }
  ]
}
```