Skip to content

Commit

Permalink
Added support for two identifiers per command plus a third argument
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed May 9, 2016
1 parent 77f3218 commit e85e277
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
52 changes: 29 additions & 23 deletions CloudFlare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ def __init__(self, email, token, certtoken, base_url, debug):
else:
self.logger = None

def _call_with_no_auth(self, method, api_call_part1, api_call_part2=None, identifier1=None, identifier2=None, params=None, data=None):
def _call_with_no_auth(self, method, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None):
headers = {}
return self._call(method, headers, api_call_part1, api_call_part2, identifier1, identifier2, params, data)
return self._call(method, headers, api_call_part1, api_call_part2, api_call_part3, identifier1, identifier2, params, data)

def _call_with_auth(self, method, api_call_part1, api_call_part2=None, identifier1=None, identifier2=None, params=None, data=None):
def _call_with_auth(self, method, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None):
if self.EMAIL is '' or self.TOKEN is '':
raise CloudFlareAPIError(0, 'no email and/or token defined')
headers = { "X-Auth-Email": self.EMAIL, "X-Auth-Key": self.TOKEN, 'Content-Type': 'application/json' }
return self._call(method, headers, api_call_part1, api_call_part2, identifier1, identifier2, params, data)
return self._call(method, headers, api_call_part1, api_call_part2, api_call_part3, identifier1, identifier2, params, data)

def _call_with_certauth(self, method, api_call_part1, api_call_part2=None, identifier1=None, identifier2=None, params=None, data=None):
def _call_with_certauth(self, method, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None):
if self.CERTTOKEN is '':
raise CloudFlareAPIError(0, 'no email and/or cert token defined')
headers = { "X-Auth-User-Service-Key": self.CERTTOKEN, 'Content-Type': 'application/json' }
return self._call(method, headers, api_call_part1, api_call_part2, identifier1, identifier2, params, data)
return self._call(method, headers, api_call_part1, api_call_part2, api_call_part3, identifier1, identifier2, params, data)

def _call(self, method, headers, api_call_part1, api_call_part2=None, identifier1=None, identifier2=None, params=None, data=None):
def _call(self, method, headers, api_call_part1, api_call_part2=None, api_call_part3=None, identifier1=None, identifier2=None, params=None, data=None):
if api_call_part2 is not None or (data is not None and method == 'GET'):
if identifier2 is None:
url = self.BASE_URL + '/' + api_call_part1 + '/' + identifier1 + '/' + api_call_part2
Expand All @@ -53,9 +53,11 @@ def _call(self, method, headers, api_call_part1, api_call_part2=None, identifier
url = self.BASE_URL + '/' + api_call_part1
else:
url = self.BASE_URL + '/' + api_call_part1 + '/' + identifier1
if api_call_part3:
url += '/' + api_call_part3

if self.logger:
self.logger.debug("Call: %s,%s,%s,%s" % (str(api_call_part1), str(identifier1), str(api_call_part2), str(identifier2)))
self.logger.debug("Call: %s,%s,%s,%s,%s" % (str(api_call_part1), str(identifier1), str(api_call_part2), str(identifier2), str(api_call_part3)))
self.logger.debug("Call: optional params and data: %s %s" % (str(params), str(data)))
self.logger.debug("Call: url is: %s" % (str(url)))
self.logger.debug("Call: method is: %s" % (str(method)))
Expand Down Expand Up @@ -104,69 +106,73 @@ def _call(self, method, headers, api_call_part1, api_call_part2=None, identifier
return response_data['result']

class _unused:
def __init__(self, base, api_call_part1, api_call_part2=None):
def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None):
#if self.logger:
# self.logger.debug("_unused %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2)))
self.base = base
self.api_call_part1 = api_call_part1
self.api_call_part2 = api_call_part2
self.api_call_part3 = api_call_part3

class _client_noauth:
def __init__(self, base, api_call_part1, api_call_part2=None):
def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None):
#if self.logger:
# self.logger.debug("_client_noauth %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2)))
self.base = base
self.api_call_part1 = api_call_part1
self.api_call_part2 = api_call_part2
self.api_call_part3 = api_call_part3

def get(self, identifier1=None, identifier2=None, params=None, data=None):
return self.base._call_with_no_auth('GET', self.api_call_part1, self.api_call_part2, identifier1, identifier2, params, data)
return self.base._call_with_no_auth('GET', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data)

class _client_with_auth:
def __init__(self, base, api_call_part1, api_call_part2=None):
def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None):
#if self.logger:
# self.logger.debug("_client_with_auth %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2)))
self.base = base
self.api_call_part1 = api_call_part1
self.api_call_part2 = api_call_part2
self.api_call_part3 = api_call_part3

def get(self, identifier1=None, identifier2=None, params=None, data=None):
return self.base._call_with_auth('GET', self.api_call_part1, self.api_call_part2, identifier1, identifier2, params, data)
return self.base._call_with_auth('GET', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data)

def patch(self, identifier1=None, identifier2=None, params=None, data=None):
return self.base._call_with_auth('PATCH', self.api_call_part1, self.api_call_part2, identifier1, identifier2, params, data)
return self.base._call_with_auth('PATCH', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data)

def post(self, identifier1=None, identifier2=None, params=None, data=None):
return self.base._call_with_auth('POST', self.api_call_part1, self.api_call_part2, identifier1, identifier2, params, data)
return self.base._call_with_auth('POST', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data)

def put(self, identifier1=None, identifier2=None, params=None, data=None):
return self.base._call_with_auth('PUT', self.api_call_part1, self.api_call_part2, identifier1, identifier2, params, data)
return self.base._call_with_auth('PUT', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data)

def delete(self, identifier1=None, identifier2=None, params=None, data=None):
return self.base._call_with_auth('DELETE', self.api_call_part1, self.api_call_part2, identifier1, identifier2, params, data)
return self.base._call_with_auth('DELETE', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data)

class _client_with_cert_auth:
def __init__(self, base, api_call_part1, api_call_part2=None):
def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=None):
#if self.logger:
# self.logger.debug("_client_with_cert_auth %s,%s,%s" % (str(base), str(api_call_part1), str(api_call_part2)))
self.base = base
self.api_call_part1 = api_call_part1
self.api_call_part2 = api_call_part2
self.api_call_part3 = api_call_part3

def get(self, identifier1=None, identifier2=None, params=None, data=None):
return self.base._call_with_certauth('GET', self.api_call_part1, self.api_call_part2, identifier1, identifier2, params, data)
return self.base._call_with_certauth('GET', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data)

def patch(self, identifier1=None, identifier2=None, params=None, data=None):
return self.base._call_with_certauth('PATCH', self.api_call_part1, self.api_call_part2, identifier1, identifier2, params, data)
return self.base._call_with_certauth('PATCH', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data)

def post(self, identifier1=None, identifier2=None, params=None, data=None):
return self.base._call_with_certauth('POST', self.api_call_part1, self.api_call_part2, identifier1, identifier2, params, data)
return self.base._call_with_certauth('POST', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data)

def put(self, identifier1=None, identifier2=None, params=None, data=None):
return self.base._call_with_certauth('PUT', self.api_call_part1, self.api_call_part2, identifier1, identifier2, params, data)
return self.base._call_with_certauth('PUT', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data)

def delete(self, identifier1=None, identifier2=None, params=None, data=None):
return self.base._call_with_certauth('DELETE', self.api_call_part1, self.api_call_part2, identifier1, identifier2, params, data)
return self.base._call_with_certauth('DELETE', self.api_call_part1, self.api_call_part2, self.api_call_part3, identifier1, identifier2, params, data)

def __init__(self, email=None, token=None, certtoken=None, debug=False):
base_url = BASE_URL
Expand Down
7 changes: 7 additions & 0 deletions CloudFlare/api_v4.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def api_v4(self):
setattr(zones, "pagerules", self._client_with_auth(self.base, "zones", "pagerules"))
setattr(zones, "purge_cache", self._client_with_auth(self.base, "zones", "purge_cache"))
setattr(zones, "railguns", self._client_with_auth(self.base, "zones", "railguns"))
zones_railguns = getattr(zones, "railguns")
setattr(zones_railguns, "diagnose", self._client_with_auth(self.base, "zones", "railguns", "diagnose"))
setattr(zones, "settings", self._client_with_auth(self.base, "zones", "settings"))
zones_settings = getattr(zones, "settings")
setattr(zones_settings, "advanced_ddos", self._client_with_auth(self.base, "zones", "settings/advanced_ddos"))
Expand Down Expand Up @@ -75,6 +77,9 @@ def api_v4(self):
setattr(zones_firewall, "waf", self._unused(self.base, "zones", "firewall/waf"))
zones_firewall_waf = getattr(zones_firewall, "waf")
setattr(zones_firewall_waf, "packages", self._client_with_auth(self.base, "zones", "firewall/waf/packages"))
zones_firewall_waf_packages = getattr(zones_firewall_waf, "packages")
setattr(zones_firewall_waf_packages, "groups", self._client_with_auth(self.base, "zones", "firewall/waf/packages", "groups"))
setattr(zones_firewall_waf_packages, "rules", self._client_with_auth(self.base, "zones", "firewall/waf/packages", "rules"))
zones_firewall_access_rules = getattr(zones_firewall, "access_rules")
setattr(zones_firewall_access_rules, "rules", self._client_with_auth(self.base, "zones", "firewall/access_rules/rules"))

Expand All @@ -90,6 +95,8 @@ def api_v4(self):
setattr(organizations, "invite", self._client_with_auth(self.base, "organizations", "invite"))
setattr(organizations, "invites", self._client_with_auth(self.base, "organizations", "invites"))
setattr(organizations, "railguns", self._client_with_auth(self.base, "organizations", "railguns"))
organizations_railguns = getattr(organizations, "railguns")
setattr(organizations_railguns, "zones", self._client_with_auth(self.base, "organizations", "railguns", "zones"))
setattr(organizations, "roles", self._client_with_auth(self.base, "organizations", "roles"))
setattr(organizations, "firewall", self._unused(self.base, "organizations", "firewall"))
organizations_firewall = getattr(organizations, "firewall")
Expand Down

0 comments on commit e85e277

Please sign in to comment.