Skip to content

Commit

Permalink
support second level urls
Browse files Browse the repository at this point in the history
  • Loading branch information
fawaf committed Aug 30, 2015
1 parent 9b895b1 commit c37584d
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions cloudflare_v4/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# all the exceptions
from exceptions import CloudFlareError, CloudFlareAPIError
from . import util
from . import CloudFlareError, CloudFlareAPIError
from .exceptions import CloudFlareError, CloudFlareAPIError
from . import logger

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

def call(self, method, endpoint, params=None):
def call(self, method, main_endpoint, endpoint=None, params=None):
headers = { "X-Auth-Email": self.EMAIL, "X-Auth-Key": self.TOKEN }
url = BASE_URL + '/' + endpoint
if endpoint is not None:
url = BASE_URL + '/' + main_endpoint + '/' + params + '/' + endpoint
else:
url = BASE_URL + '/' + main_endpoint
method = method.upper()
self.logger.debug("EMAIL is: " + str(self.EMAIL))
self.logger.debug("TOKEN is: " + str(self.TOKEN))
self.logger.debug("method type is: " + method)
self.logger.debug("url endpoint is: " + url)
self.logger.debug("optional params is: " + str(params))
if (method is None) or (endpoint is None):
raise CloudFlareError('You must specify a method and endpoint')
if (method is None) or (main_endpoint is None):
raise CloudFlareError('You must specify a method and endpoint') # should never happen
else:
self.logger.debug("headers being sent: " + str(headers))
if method == 'GET':
self.logger.debug("headers being sent: " + str(headers))
response = requests.get(url, headers=headers, params=params)
elif method == 'POST':
headers['Content-Type'] = 'application/json'
self.logger.debug("headers being sent: " + str(headers))
response = requests.post(url, headers=headers, json=params)
elif method == 'DELETE':
self.logger.debug("headers being sent: " + str(headers))
response = requests.delete(url, headers=headers, json=params)
data = response.text
self.logger.debug("data received: " + data)
try:
data = json.loads(data)
return data
if data['success'] is False:
raise CloudFlareAPIError(data['errors'][0]['message'])
else:
return data
except ValueError:
raise CloudFlareAPIError('JSON parse failed.')
if data['result'] == 'error':
raise CloudFlareAPIError(data['msg'])

class DynamicClient:
def __init__(self, base_client, url_type):
def __init__(self, base_client, main_endpoint, endpoint=None):
self.base_client = base_client
self.url_type = url_type
self.main_endpoint = main_endpoint
self.endpoint = endpoint

def get(self, params=None):
return self.base_client.call('GET', self.url_type, params)
return self.base_client.call('GET', self.main_endpoint,
self.endpoint, params)

def post(self, params=None):
return self.base_client.call('POST', self.url_type, params)
return self.base_client.call('POST', self.main_endpoint,
self.endpoint, params)

def delete(self, params=None):
return self.base_client.call('DELETE', self.url_type, params)
return self.base_client.call('DELETE', self.main_endpoint,
self.endpoint, params)

def __init__(self, email, token, debug):
self.base_client = self.BaseClient(email, token, debug)
setattr(self, "zones", self.DynamicClient(self.base_client, "zones"))
setattr(self, "user", self.DynamicClient(self.base_client, "user"))
zones = getattr(self, "zones")
setattr(zones, "dns_records", self.DynamicClient(self.base_client, "zones", "dns_records"))

0 comments on commit c37584d

Please sign in to comment.