Skip to content

Commit

Permalink
Moved walk into CloudFlare class - much cleaner
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Dec 10, 2016
1 parent 752ccb4 commit 894ae11
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 52 deletions.
89 changes: 56 additions & 33 deletions CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=Non
""" Cloudflare v4 API"""

self._base = base
self.api_call_part1 = api_call_part1
self.api_call_part2 = api_call_part2
self.api_call_part3 = api_call_part3
# 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):
""" Cloudflare v4 API"""
Expand Down Expand Up @@ -272,11 +272,11 @@ def get(self, identifier1=None, identifier2=None, params=None, data=None):
""" Cloudflare v4 API"""

return self._base.call_with_no_auth('GET',
self.api_call_part1,
self.api_call_part2,
self.api_call_part3,
identifier1, identifier2,
params, data)
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):
""" Cloudflare v4 API"""
Expand Down Expand Up @@ -313,51 +313,51 @@ def get(self, identifier1=None, identifier2=None, params=None, data=None):
""" Cloudflare v4 API"""

return self._base.call_with_auth('GET',
self.api_call_part1,
self.api_call_part2,
self.api_call_part3,
identifier1, identifier2,
params, data)
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):
""" Cloudflare v4 API"""

return self._base.call_with_auth('PATCH',
self.api_call_part1,
self.api_call_part2,
self.api_call_part3,
identifier1, identifier2,
params, data)
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):
""" Cloudflare v4 API"""

return self._base.call_with_auth('POST',
self.api_call_part1,
self.api_call_part2,
self.api_call_part3,
identifier1, identifier2,
params, data)
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):
""" Cloudflare v4 API"""

return self._base.call_with_auth('PUT',
self.api_call_part1,
self.api_call_part2,
self.api_call_part3,
identifier1, identifier2,
params, data)
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):
""" Cloudflare v4 API"""

return self._base.call_with_auth('DELETE',
self.api_call_part1,
self.api_call_part2,
self.api_call_part3,
identifier1, identifier2,
params, data)
self.api_call_part1,
self.api_call_part2,
self.api_call_part3,
identifier1, identifier2,
params, data)

class _add_with_cert_auth(object):
""" Cloudflare v4 API"""
Expand Down Expand Up @@ -420,6 +420,29 @@ def delete(self, identifier1=None, identifier2=None, params=None, data=None):
identifier1, identifier2,
params, data)

def api_list(self, m=None, s=''):
"""recursive walk of the api tree returning a list of api calls"""
if m == None:
m = self
w = []
for n in sorted(dir(m)):
if n[0] == '_':
# internal
continue
if n in ['delete', 'get', 'patch', 'post', 'put']:
# gone too far
continue
a = getattr(m, n)
d = dir(a)
if '_base' in d:
# it's a known api call - lets show the result and continue down the tree
if 'delete' in d or 'get' in d or 'patch' in d or 'post' in d or 'put' in d:
# only show the result if a call exists for this part
if 'api_call_part1' in d:
w.append(s + '/' + n)
w = w + self.api_list(a, s + '/' + n)
return w

def __init__(self, email=None, token=None, certtoken=None, debug=False, raw=False):
""" Cloudflare v4 API"""

Expand Down
21 changes: 2 additions & 19 deletions cli4/cli4.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,10 @@ def convert_load_balancers_map_regions(cf, region_name):
return region_name
exit('cli4: %s - no region found' % (region_name))

def walk(m, s):
"""recursive walk of the tree"""
for n in sorted(dir(m)):
if n[0] == '_':
# internal
continue
if n in ['delete', 'get', 'patch', 'post', 'put']:
# gone too far
continue
a = getattr(m, n)
d = dir(a)
if '_base' in d:
# it's a known api call - lets show the result and continue down the tree
if 'delete' in d or 'get' in d or 'patch' in d or 'post' in d or 'put' in d:
# only show the result if a call exists for this part
print(s + '/' + n)
walk(a, s + '/' + n)

def dump_commands(cf):
"""dump a tree of all the known API commands"""
walk(cf, '')
w = cf.api_list()
print('\n'.join(w))

def cli4(args):
"""Cloudflare API via command line"""
Expand Down

0 comments on commit 894ae11

Please sign in to comment.