-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.py
77 lines (51 loc) · 2.2 KB
/
api.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'''
This file implements functions for interacting with the porkbun API.
See here for documentation:
https://porkbun.com/api/json/v3/documentation#Overview
'''
import requests
requests.packages.urllib3.util.connection.HAS_IPV6 = False
BASE_URL = 'https://api.porkbun.com'
class PorkbunAPIError(Exception):
pass
def format_url(path):
return '{}/{}'.format(BASE_URL, path)
def get_response(argv):
try:
endpoint = argv['endpoint']
argv.pop('endpoint')
response = requests.post(endpoint, json=argv)
except Exception as e:
raise PorkbunAPIError("Oh no! Could not get response from '{}'! {}".format(endpoint, e))
if response.status_code != 200:
raise PorkbunAPIError("Oh no! Got {} response from '{}'!".format(response.status_code, endpoint))
data = response.json()
if data['status'] != 'SUCCESS':
raise PorkbunAPIError("Oh no! An API error occurred:\n{} - {}".format(data['status'], data['message']))
return data
def ping(secretapikey, apikey):
endpoint = format_url('api/json/v3/ping')
args = {k: v for k, v in locals().items() if v is not None}
return get_response(args)
def create_record(domain, secretapikey, apikey, name, type, content, ttl, prio):
endpoint = format_url('api/json/v3/dns/create/{}'.format(domain))
args = {k: v for k, v in locals().items() if v is not None}
args.pop('domain')
return get_response(args)
def edit_record(domain, id, secretapikey, apikey, name, type, content, ttl, prio):
endpoint = format_url('api/json/v3/dns/edit/{}/{}'.format(domain, id))
args = {k: v for k, v in locals().items() if v is not None}
args.pop('domain')
args.pop('id')
return get_response(args)
def delete_record(domain, id, secretapikey, apikey):
endpoint = format_url('api/json/v3/dns/delete/{}/{}'.format(domain, id))
args = {k: v for k, v in locals().items() if v is not None}
args.pop('domain')
args.pop('id')
return get_response(args)
def retrieve_records(domain, secretapikey, apikey):
endpoint = format_url('api/json/v3/dns/retrieve/{}'.format(domain))
args = {k: v for k, v in locals().items() if v is not None}
args.pop('domain')
return get_response(args)