Skip to content

Commit c37584d

Browse files
committed
support second level urls
1 parent 9b895b1 commit c37584d

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

cloudflare_v4/__init__.py

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# all the exceptions
2-
from exceptions import CloudFlareError, CloudFlareAPIError
3-
from . import util
4-
from . import CloudFlareError, CloudFlareAPIError
2+
from .exceptions import CloudFlareError, CloudFlareAPIError
53
from . import logger
64

75
import json
@@ -17,53 +15,61 @@ def __init__(self, email, token, debug):
1715
self.TOKEN = token
1816
self.logger = logger.Logger(debug).getLogger()
1917

20-
def call(self, method, endpoint, params=None):
18+
def call(self, method, main_endpoint, endpoint=None, params=None):
2119
headers = { "X-Auth-Email": self.EMAIL, "X-Auth-Key": self.TOKEN }
22-
url = BASE_URL + '/' + endpoint
20+
if endpoint is not None:
21+
url = BASE_URL + '/' + main_endpoint + '/' + params + '/' + endpoint
22+
else:
23+
url = BASE_URL + '/' + main_endpoint
2324
method = method.upper()
2425
self.logger.debug("EMAIL is: " + str(self.EMAIL))
2526
self.logger.debug("TOKEN is: " + str(self.TOKEN))
2627
self.logger.debug("method type is: " + method)
2728
self.logger.debug("url endpoint is: " + url)
2829
self.logger.debug("optional params is: " + str(params))
29-
if (method is None) or (endpoint is None):
30-
raise CloudFlareError('You must specify a method and endpoint')
30+
if (method is None) or (main_endpoint is None):
31+
raise CloudFlareError('You must specify a method and endpoint') # should never happen
3132
else:
33+
self.logger.debug("headers being sent: " + str(headers))
3234
if method == 'GET':
33-
self.logger.debug("headers being sent: " + str(headers))
3435
response = requests.get(url, headers=headers, params=params)
3536
elif method == 'POST':
3637
headers['Content-Type'] = 'application/json'
37-
self.logger.debug("headers being sent: " + str(headers))
3838
response = requests.post(url, headers=headers, json=params)
3939
elif method == 'DELETE':
40-
self.logger.debug("headers being sent: " + str(headers))
4140
response = requests.delete(url, headers=headers, json=params)
4241
data = response.text
4342
self.logger.debug("data received: " + data)
4443
try:
4544
data = json.loads(data)
46-
return data
45+
if data['success'] is False:
46+
raise CloudFlareAPIError(data['errors'][0]['message'])
47+
else:
48+
return data
4749
except ValueError:
4850
raise CloudFlareAPIError('JSON parse failed.')
49-
if data['result'] == 'error':
50-
raise CloudFlareAPIError(data['msg'])
5151

5252
class DynamicClient:
53-
def __init__(self, base_client, url_type):
53+
def __init__(self, base_client, main_endpoint, endpoint=None):
5454
self.base_client = base_client
55-
self.url_type = url_type
55+
self.main_endpoint = main_endpoint
56+
self.endpoint = endpoint
5657

5758
def get(self, params=None):
58-
return self.base_client.call('GET', self.url_type, params)
59+
return self.base_client.call('GET', self.main_endpoint,
60+
self.endpoint, params)
5961

6062
def post(self, params=None):
61-
return self.base_client.call('POST', self.url_type, params)
63+
return self.base_client.call('POST', self.main_endpoint,
64+
self.endpoint, params)
6265

6366
def delete(self, params=None):
64-
return self.base_client.call('DELETE', self.url_type, params)
67+
return self.base_client.call('DELETE', self.main_endpoint,
68+
self.endpoint, params)
6569

6670
def __init__(self, email, token, debug):
6771
self.base_client = self.BaseClient(email, token, debug)
6872
setattr(self, "zones", self.DynamicClient(self.base_client, "zones"))
6973
setattr(self, "user", self.DynamicClient(self.base_client, "user"))
74+
zones = getattr(self, "zones")
75+
setattr(zones, "dns_records", self.DynamicClient(self.base_client, "zones", "dns_records"))

0 commit comments

Comments
 (0)