Redis 企业软件 REST API 快速入门
Redis 企业软件 REST API 快速入门
Redis 企业软件 |
---|
Redis Enterprise Software 包括一个 REST API,允许您自动执行某些任务。本文介绍如何向 Redis Enterprise Software REST API 发送请求。
基础
无论您使用哪种方法发送 API 请求,都需要记住一些常见概念。
类型 | 描述 |
---|---|
认证 | 将 Basic Auth 与集群用户名 (电子邮件) 和密码一起使用 |
港口 | 默认情况下,所有调用都对端口 9443 进行 |
版本 | 在请求 URI 中指定版本 |
头 | Accept 和Content-Type 应该是application/json |
响应类型和错误代码 | 响应200 OK 意味着成功;否则,请求会因错误而失败 |
有关更多信息,请参阅 Redis Enterprise Software REST API。
cURL 示例请求
cURL 是一个命令行工具,允许您从终端发送 HTTP 请求。
您可以使用以下选项构建 cURL 请求:
选择 | 描述 |
---|---|
-X | 方法(GET、PUT、PATCH、POST 或 DELETE) |
-H | 请求头,可多次指定 |
-u | 用户名和密码信息 |
-d | PUT 或 POST 请求的 JSON 数据 |
-F | PUT 或 POST 请求的表单数据,例如POST /v1/modules 或POST /v2/modules 端点 |
-k | 关闭 SSL 验证 |
-我 | 显示标头和状态代码以及响应正文 |
有关更多信息,请参阅 cURL 文档。
GET 请求
使用以下 cURL 命令获取获取/v1/bdbs/
端点。
$ curl -X GET -H "accept: application/json" \
-u "[username]:[password]" \
https://[host][:port]/v1/bdbs -k -i
HTTP/1.1 200 OK
server: envoy
date: Tue, 14 Jun 2022 19:24:30 GMT
content-type: application/json
content-length: 2833
cluster-state-id: 42
x-envoy-upstream-service-time: 25
[
{
...
"name": "tr01",
...
"uid": 1,
"version": "6.0.16",
"wait_command": true
}
]
In the response body, the
uid
is the database ID. You can use the database ID to view or update the database using the API.
For more information about the fields returned by GET /v1/bdbs/
, see the bdbs
object.
PUT request
Once you have the database ID, you can use PUT /v1/bdbs/
to update the configuration of the database.
For example, you can pass the database uid
1 as a URL parameter and use the -d
option to specify the new name
when you send the request. This changes the database's name
from tr01
to database1
:
$ curl -X PUT -H "accept: application/json" \
-H "content-type: application/json" \
-u "cameron.bates@redis.com:test123" \
https://[host]:[port]/v1/bdbs/1 \
-d '{ "name": "database1" }' -k -i
HTTP/1.1 200 OK
server: envoy
date: Tue, 14 Jun 2022 20:00:25 GMT
content-type: application/json
content-length: 2933
cluster-state-id: 43
x-envoy-upstream-service-time: 159
{
...
"name" : "database1",
...
"uid" : 1,
"version" : "6.0.16",
"wait_command" : true
}
For more information about the fields you can update with PUT /v1/bdbs/
, see the bdbs
object.
Client examples
You can also use client libraries to make API requests in your preferred language.
To follow these examples, you need:
- A Redis Enterprise Software node
- Python 3 and the requests Python library
- node.js and node-fetch
Python
import json
import requests
# Required connection information - replace with your host, port, username, and password
host = "[host]"
port = "[port]"
username = "[username]"
password = "[password]"
# Get the list of databases using GET /v1/bdbs
bdbs_uri = "https://{}:{}/v1/bdbs".format(host, port)
print("GET {}".format(bdbs_uri))
get_resp = requests.get(bdbs_uri,
auth = (username, password),
headers = { "accept" : "application/json" },
verify = False)
print("{} {}".format(get_resp.status_code, get_resp.reason))
for header in get_resp.headers.keys():
print("{}: {}".format(header, get_resp.headers[header]))
print("\n" + json.dumps(get_resp.json(), indent=4))
# Rename all databases using PUT /v1/bdbs
for bdb in get_resp.json():
uid = bdb["uid"] # Get the database ID from the JSON response
put_uri = "{}/{}".format(bdbs_uri, uid)
new_name = "database{}".format(uid)
put_data = { "name" : new_name }
print("PUT {} {}".format(put_uri, json.dumps(put_data)))
put_resp = requests.put(put_uri,
data = json.dumps(put_data),
auth = (username, password),
headers = { "content-type" : "application/json" },
verify = False)
print("{} {}".format(put_resp.status_code, put_resp.reason))
for header in put_resp.headers.keys():
print("{}: {}".format(header, put_resp.headers[header]))
print("\n" + json.dumps(put_resp.json(), indent=4))
See the Python requests library documentation for more information.
Output
$ python rs_api.py
python rs_api.py
GET https://[host]:[port]/v1/bdbs
InsecureRequestWarning: Unverified HTTPS request is being made to host '[host]'.
Adding certificate verification is strongly advised.
See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
warnings.warn(
200 OK
server: envoy
date: Wed, 15 Jun 2022 15:49:43 GMT
content-type: application/json
content-length: 2832
cluster-state-id: 89
x-envoy-upstream-service-time: 27
[
{
...
"name": "tr01",
...
"uid": 1,
"version": "6.0.16",
"wait_command": true
}
]
PUT https://[host]:[port]/v1/bdbs/1 {"name": "database1"}
InsecureRequestWarning: Unverified HTTPS request is being made to host '[host]'.
Adding certificate verification is strongly advised.
See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings
warnings.warn(
200 OK
server: envoy
date: Wed, 15 Jun 2022 15:49:43 GMT
content-type: application/json
content-length: 2933
cluster-state-id: 90
x-envoy-upstream-service-time: 128
{
...
"name" : "database1",
...
"uid" : 1,
"version" : "6.0.16",
"wait_command" : true
}
node.js
import fetch, { Headers } from 'node-fetch';
import * as https from 'https';
const HOST = '[host]';
const PORT = '[port]';
const USERNAME = '[username]';
const PASSWORD = '[password]';
// Get the list of databases using GET /v1/bdbs
const BDBS_URI = `https://${HOST}:${PORT}/v1/bdbs`;
const USER_CREDENTIALS = Buffer.from(`${USERNAME}:${PASSWORD}`).toString('base64');
const AUTH_HEADER = `Basic ${USER_CREDENTIALS}`;
console.log(`GET ${BDBS_URI}`);
const HTTPS_AGENT = new https.Agent({
rejectUnauthorized: false
});
const response = await fetch(BDBS_URI, {
method: 'GET',
headers: {
'Accept': 'application/json',
'Authorization': AUTH_HEADER
},
agent: HTTPS_AGENT
});
const responseObject = await response.json();
console.log(`${response.status}: ${response.statusText}`);
console.log(responseObject);
// Rename all databases using PUT /v1/bdbs
for (const database of responseObject) {
const DATABASE_URI = `${BDBS_URI}/${database.uid}`;
const new_name = `database${database.uid}`;
console.log(`PUT ${DATABASE_URI}`);
const response = await fetch(DATABASE_URI, {
method: 'PUT',
headers: {
'Authorization': AUTH_HEADER,
'Content-Type': 'application/json'
},
body: JSON.stringify({
'name': new_name
}),
agent: HTTPS_AGENT
});
console.log(`${response.status}: ${response.statusText}`);
console.log(await(response.json()));
}
See the node-fetch documentation for more info.
Output
$ node rs_api.js
GET https://[host]:[port]/v1/bdbs
200: OK
[
{
...
"name": "tr01",
...
"slave_ha" : false,
...
"uid": 1,
"version": "6.0.16",
"wait_command": true
}
]
PUT https://[host]:[port]/v1/bdbs/1
200: OK
{
...
"name" : "tr01",
...
"slave_ha" : true,
...
"uid" : 1,
"version" : "6.0.16",
"wait_command" : true
}
More info
On this page