Skip to content

Commit

Permalink
fix post and delete + add purge_cache
Browse files Browse the repository at this point in the history
  • Loading branch information
fawaf committed Aug 30, 2015
1 parent c37584d commit efefa62
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions cloudflare_v4/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def __init__(self, email, token, debug):
self.TOKEN = token
self.logger = logger.Logger(debug).getLogger()

def call(self, method, main_endpoint, endpoint=None, params=None):
def call(self, method, main_endpoint, endpoint=None, params=None, data=None):
headers = { "X-Auth-Email": self.EMAIL, "X-Auth-Key": self.TOKEN }
if endpoint is not None:
url = BASE_URL + '/' + main_endpoint + '/' + params + '/' + endpoint
Expand All @@ -27,6 +27,7 @@ def call(self, method, main_endpoint, endpoint=None, params=None):
self.logger.debug("method type is: " + method)
self.logger.debug("url endpoint is: " + url)
self.logger.debug("optional params is: " + str(params))
self.logger.debug("optional data is: " + str(data))
if (method is None) or (main_endpoint is None):
raise CloudFlareError('You must specify a method and endpoint') # should never happen
else:
Expand All @@ -35,9 +36,9 @@ def call(self, method, main_endpoint, endpoint=None, params=None):
response = requests.get(url, headers=headers, params=params)
elif method == 'POST':
headers['Content-Type'] = 'application/json'
response = requests.post(url, headers=headers, json=params)
response = requests.post(url, headers=headers, json=data)
elif method == 'DELETE':
response = requests.delete(url, headers=headers, json=params)
response = requests.delete(url, headers=headers, json=data)
data = response.text
self.logger.debug("data received: " + data)
try:
Expand All @@ -51,6 +52,9 @@ def call(self, method, main_endpoint, endpoint=None, params=None):

class DynamicClient:
def __init__(self, base_client, main_endpoint, endpoint=None):
base_client.logger.debug("base client is: " + str(base_client))
base_client.logger.debug("main endpoint is: " + str(main_endpoint))
base_client.logger.debug("endpoint is: " + str(endpoint))
self.base_client = base_client
self.main_endpoint = main_endpoint
self.endpoint = endpoint
Expand All @@ -59,17 +63,20 @@ def get(self, params=None):
return self.base_client.call('GET', self.main_endpoint,
self.endpoint, params)

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

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

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"))
setattr(zones, "dns_records", self.DynamicClient(self.base_client,
"zones", "dns_records"))
setattr(zones, "purge_cache", self.DynamicClient(self.base_client,
"zones", "purge_cache"))

0 comments on commit efefa62

Please sign in to comment.