dynu.com API文档常用内容整理

"API文档"

Posted by xuhao on May 25, 2024

“省略掉非必需的字,这样读者就会注意到你想保留的。 ”

dynu.com API文档常用内容整理

一、重要功能

序号 功能内容 功能英文名称 请求路径 请求类型
1 获取DNS服务的与列表 get_dns_services_list /dns GET
2 添加新的DNS服务 add_new_dns_service /dns POST
3 根据主机名获取根域名 get_root_domain_by_hostname /dns/getroot/{hostname} GET
4 根据主机名和资源记录类型获取DNS记录 get_dns_record_by_hostname_and_type /dns/getroot/{hostname} GET
5 获取DNS服务域名的详细信息 get_dns_service_domain_details /dns/{id} GET
6 更新现有的DNS服务 update_existing_dns_service /dns/{id} POST
7 从DNS服务中删除域 delete_domain_from_dns_service /dns/{id} DELETE
8 获取DNS服务的DNS记录列表 get_dns_service_records_list /dns/{id}/record GET
9 为DNS服务添加新的DNS记录 add_new_dns_record_to_service /dns/{id}/record POST
10 获取DNS服务的DNS记录的详细信息 get_dns_record_details /dns/{id}/record/{dnsRecordId} GET
11 更新DNS服务的现有DNS记录 update_existing_dns_record /dns/{id}/record/{dnsRecordId} POST
12 从DNS服务中删除DNS记录 delete_dns_record_from_service /dns/{id}/record/{dnsRecordId} DELETE

二、示例脚本

1
curl -X GET "https://api.dynu.com/v2/dns" -H "accept: application/json" -H "API-Key: 363W4fXg2U63eWbU666Vda6556UU4bU3"

等价于

1
2
3
4
5
6
7
8
9
10
11
import requests

url = "https://api.dynu.com/v2/dns"
headers = {
    "accept": "application/json",
    "API-Key": "363W4fXg2U63eWbU666Vda6556UU4bU3"
}

response = requests.get(url, headers=headers)
print(response.json())

1
curl -X POST "https://api.dynu.com/v2/dns" -H "accept: application/json" -H "API-Key: 363W4fXg2U63eWbU666Vda6556UU4bU3" -H "Content-Type: application/json" -d "{\"name\":\"somedomain.com\",\"group\":\"office\",\"ipv4Address\":\"1.2.3.4\",\"ipv6Address\":\"1111:2222:3333::4444\",\"ttl\":90,\"ipv4\":true,\"ipv6\":true,\"ipv4WildcardAlias\":true,\"ipv6WildcardAlias\":true,\"allowZoneTransfer\":false,\"dnssec\":false}"

等价于

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
import requests
import json

url = "https://api.dynu.com/v2/dns"
headers = {
    "accept": "application/json",
    "API-Key": "363W4fXg2U63eWbU666Vda6556UU4bU3",
    "Content-Type": "application/json"
}
data = {
    "name": "somedomain.com",
    "group": "office",
    "ipv4Address": "1.2.3.4",
    "ipv6Address": "1111:2222:3333::4444",
    "ttl": 90,
    "ipv4": True,
    "ipv6": True,
    "ipv4WildcardAlias": True,
    "ipv6WildcardAlias": True,
    "allowZoneTransfer": False,
    "dnssec": False
}

response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.json())

1
curl -X GET "https://api.dynu.com/v2/dns/record/cdn.xuhao.giize.com?recordType=A" -H "accept: application/json" -H "API-Key: 363W4fXg2U63eWbU666Vda6556UU4bU3"

等价于

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
import requests

node_domain_name = 'cfcdn.xuhao.giize.com'
recordtype = 'A'
url = f"https://api.dynu.com/v2/dns/record/{node_domain_name}"
params = {
    "recordType": recordtype
}
headers = {
    "accept": "application/json",
    "API-Key": "363W4fXg2U63eWbU666Vda6556UU4bU3"
}

# 发送 GET 请求
response = requests.get(url, headers=headers, params=params)

# 检查响应状态码
if response.status_code == 200:
    json_response = response.json()
    # 提取 id 和 domainId
    for record in json_response.get("dnsRecords", []):
        record_id = record.get("id")
        domain_id = record.get("domainId")
        print(f"ID: {record_id}, Domain ID: {domain_id}")
else:
    print(f"请求失败,状态码: {response.status_code}, 响应: {response.text}")
1
curl -X GET "https://api.dynu.com/v2/dns/10137145/record/10609556" -H "accept: application/json" -H "API-Key: 363W4fXg2U63eWbU666Vda6556UU4bU3"

等价于

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import requests

# 定义 URL 和请求头
domain_id = 10137145
record_id = 10609556
url = f"https://api.dynu.com/v2/dns/{domain_id}/record/{record_id}"
headers = {
    "accept": "application/json",
    "API-Key": "363W4fXg2U63eWbU666Vda6556UU4bU3"
}

# 发送 GET 请求
response = requests.get(url, headers=headers)

# 检查响应状态码
if response.status_code == 200:
    json_response = response.json()
    # 打印 JSON 响应
    print(json_response)
else:
    print(f"请求失败,状态码: {response.status_code}, 响应: {response.text}")
1
curl -X DELETE "https://api.dynu.com/v2/dns/23/record/14" -H "accept: application/json" -H "API-Key: 363W4fXg2U63eWbU666Vda6556UU4bU3"

等价于

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import requests

# 定义 URL 和请求头
domain_id = 23
record_id = 14
url = f"https://api.dynu.com/v2/dns/{domain_id}/record/{record_id}"
headers = {
    "accept": "application/json",
    "API-Key": "363W4fXg2U63eWbU666Vda6556UU4bU3"
}

# 发送 DELETE 请求
response = requests.delete(url, headers=headers)

# 检查响应状态码
if response.status_code == 200:
    print("记录删除成功")
    json_response = response.json()
    # 打印 JSON 响应(如果有)
    print(json_response)
else:
    print(f"请求失败,状态码: {response.status_code}, 响应: {response.text}")
1
curl -X POST "https://api.dynu.com/v2/dns/12/record/34" -H "accept: application/json" -H "API-Key: 363W4fXg2U63eWbU666Vda6556UU4bU3" -H "Content-Type: application/json" -d "{\"nodeName\":\"mail\",\"recordType\":\"A\",\"ttl\":300,\"state\":true,\"group\":\"\",\"ipv4Address\":\"204.25.79.214\"}"

等价于

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
33
import requests
import json

# 定义 URL 和请求头
domain_id = 12
record_id = 34
url = f"https://api.dynu.com/v2/dns/{domain_id}/record/{record_id}"
headers = {
    "accept": "application/json",
    "API-Key": "363W4fXg2U63eWbU666Vda6556UU4bU3",
    "Content-Type": "application/json"
}

# 定义请求数据
data = {
    "nodeName": "mail",
    "recordType": "A",
    "ttl": 300,
    "state": True,
    "group": "",
    "ipv4Address": "204.25.79.214"
}

# 发送 POST 请求
response = requests.post(url, headers=headers, data=json.dumps(data))

# 检查响应状态码
if response.status_code == 200:
    json_response = response.json()
    # 打印 JSON 响应
    print(json_response)
else:
    print(f"请求失败,状态码: {response.status_code}, 响应: {response.text}")

最终脚本

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
33
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
93
94
95
96
97
import requests
import json
import csv

# 请设置变量
node_domain_name = 'cfcdn.xuhao.giize.com'
nodeName = 'cfcdn' # nodeName
recordtype = 'A'
api_key = '363W4fXg2U63eWbU666Vda6556UU4bU3'

# 读取文件
file_path = 'result_ip_domain.csv'
with open(file_path, mode='r', encoding='utf-8') as file:
    reader = csv.reader(file)
    # 读取第一行(标题行)
    header = next(reader)
    # 读取第一列的第一行的值
    first_row = next(reader)
    first_value = first_row[0]
    print(first_value)
    
ipaddress = first_value

# 1.获取record_id及domainId
node_url = f"https://api.dynu.com/v2/dns/record/{node_domain_name}"
params = {
    "recordType": recordtype
}
headers = {
    "accept": "application/json",
    "API-Key": api_key
}

# 发送 GET 请求
node_response = requests.get(node_url, headers=headers, params=params)

# 检查响应状态码
if node_response.status_code == 200:
    node_json_response = node_response.json()
    # 提取 id 和 domainId
    for record in node_json_response.get("dnsRecords", []):
        record_id = record.get("id")
        domain_id = record.get("domainId")
        print(f"ID: {record_id}, Domain ID: {domain_id}")
else:
    print(f"请求失败,状态码: {node_response.status_code}, 响应: {node_response.text}")
    
# 2.删除相关记录
url = f"https://api.dynu.com/v2/dns/{domain_id}/record/{record_id}"

# 发送 GET 请求
response = requests.get(url, headers=headers)

# 检查响应状态码
if response.status_code == 200:
    json_response = response.json()
    # 打印 JSON 响应
    print(json_response)
    delete_response = requests.delete(url, headers=headers)
    if delete_response.status_code == 200:
        print("记录删除成功")
        delete_json_response = delete_response.json()
        # 打印 JSON 响应(如果有)
        print(delete_json_response)
    else:
        print(f"请求失败,状态码: {delete_response.status_code}, 响应: {delete_response.text}")
else:
    print(f"请求失败,状态码: {response.status_code}, 响应: {response.text}")

# 3.插入相关记录

insert_headers = {
    "accept": "application/json",
    "API-Key": api_key,
    "Content-Type": "application/json"
}

# 定义请求数据
data = {
    "nodeName": nodeName,
    "recordType": recordtype,
    "ttl": 60,
    "state": True,
    "group": "",
    "ipv4Address": ipaddress
}

# 发送 POST 请求
insert_response = requests.post(url, headers=insert_headers, data=json.dumps(data))

# 检查响应状态码
if insert_response.status_code == 200:
    insert_json_response = insert_response.json()
    # 打印 JSON 响应
    print(insert_json_response)
else:
    print(f"请求失败,状态码: {insert_response.status_code}, 响应: {insert_response.text}")