master
Raw Download raw file
 1import requests
 2
 3class HoverException(Exception):
 4    pass
 5
 6
 7class HoverAPI(object):
 8    def __init__(self, username, password):
 9        params = {"username": username, "password": password}
10        r = requests.post("https://www.hover.com/api/login", params=params)
11        if not r.ok or "hoverauth" not in r.cookies:
12            raise HoverException(r)
13        self.cookies = {"hoverauth": r.cookies["hoverauth"]}
14    def call(self, method, resource, data=None):
15        url = "https://www.hover.com/api/{0}".format(resource)
16        r = requests.request(method, url, data=data, cookies=self.cookies)
17        if not r.ok:
18            raise HoverException(r)
19        if r.content:
20            body = r.json()
21            if "succeeded" not in body or body["succeeded"] is not True:
22                raise HoverException(body)
23            return body
24
25
26# connect to the API using your account
27client = HoverAPI("myusername", "mypassword")
28
29# get details of a domains without DNS records
30client.call("get", "domains")
31
32# get all domains and DNS records
33client.call("get", "dns")
34
35
36# notice the "id" field of domains in response to the above calls - that's needed
37# to address the domains individually, like so:
38
39# get details of a specific domain without DNS records
40client.call("get", "domains/dom123456")
41
42# get DNS records of a specific domain:
43client.call("get", "domains/dom123456/dns")
44
45# create a new A record:
46record = {"name": "mysubdomain", "type": "A", "content": "127.0.0.1"}
47client.call("post", "domains/dom123456/dns", record)
48
49# create a new SRV record
50# note that content is "{priority} {weight} {port} {target}"
51record = {"name": "mysubdomain", "type": "SRV", "content": "10 10 123 __service"}
52client.call("post", "domains/dom123456/dns", record)
53
54# create a new MX record
55# note that content is "{priority} {host}"
56record = {"name": "mysubdomain", "type": "MX", "content": "10 mail"}
57client.call("post", "domains/dom123456/dns", record)
58
59
60# notice the "id" field of DNS records in the above calls - that's
61#  needed to address the DNS records individually, like so:
62
63# update an existing DNS record
64client.call("put", "dns/dns1234567", {"content": "127.0.0.1"})
65
66# delete a DNS record:
67client.call("delete", "dns/dns1234567")