-
Notifications
You must be signed in to change notification settings - Fork 1
/
cloudflare.py
54 lines (48 loc) · 1.48 KB
/
cloudflare.py
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
import httpx
from .models import Domain
async def cloudflare_create_record(domain: Domain, ip: str):
url = (
"https://api.cloudflare.com/client/v4/zones/"
+ domain.cf_zone_id
+ "/dns_records"
)
header = {
"Authorization": "Bearer " + domain.cf_token,
"Content-Type": "application/json",
}
cf_response = {}
async with httpx.AsyncClient() as client:
try:
r = await client.post(
url,
headers=header,
json={
"type": "CNAME",
"name": domain.domain,
"content": ip,
"ttl": 0,
"proxied": False,
},
timeout=40,
)
cf_response = r.json()
except AssertionError:
cf_response = {"error": "Error occured"}
return cf_response
async def cloudflare_deleterecord(domain: Domain, domain_id: str):
url = (
"https://api.cloudflare.com/client/v4/zones/"
+ domain.cf_zone_id
+ "/dns_records"
)
header = {
"Authorization": "Bearer " + domain.cf_token,
"Content-Type": "application/json",
}
async with httpx.AsyncClient() as client:
try:
r = await client.delete(url + "/" + domain_id, headers=header, timeout=40)
cf_response = r.text
except AssertionError:
cf_response = "Error occured"
return cf_response