mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2024-11-20 02:28:10 +01:00
56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
import json
|
|
|
|
import httpx
|
|
|
|
from lnbits.extensions.lnaddress.models import Domains
|
|
|
|
|
|
async def cloudflare_create_record(domain: Domains, 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 = json.loads(r.text)
|
|
except AssertionError:
|
|
cf_response = "Error occured"
|
|
return cf_response
|
|
|
|
|
|
async def cloudflare_deleterecord(domain: Domains, 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"
|