From 894ae11788a2b1997e4f58895d205d1f74ab16f7 Mon Sep 17 00:00:00 2001 From: Martin Levy Date: Fri, 9 Dec 2016 16:22:51 -0800 Subject: [PATCH] Moved walk into CloudFlare class - much cleaner --- CloudFlare/cloudflare.py | 89 +++++++++++++++++++++++++--------------- cli4/cli4.py | 21 +--------- 2 files changed, 58 insertions(+), 52 deletions(-) diff --git a/CloudFlare/cloudflare.py b/CloudFlare/cloudflare.py index 65b6aec..6e6c96e 100644 --- a/CloudFlare/cloudflare.py +++ b/CloudFlare/cloudflare.py @@ -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""" @@ -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""" @@ -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""" @@ -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""" diff --git a/cli4/cli4.py b/cli4/cli4.py index e73aefd..8d28946 100644 --- a/cli4/cli4.py +++ b/cli4/cli4.py @@ -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"""