Skip to content

Commit 894ae11

Browse files
committed
Moved walk into CloudFlare class - much cleaner
1 parent 752ccb4 commit 894ae11

2 files changed

Lines changed: 58 additions & 52 deletions

File tree

CloudFlare/cloudflare.py

Lines changed: 56 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,9 @@ def __init__(self, base, api_call_part1, api_call_part2=None, api_call_part3=Non
228228
""" Cloudflare v4 API"""
229229

230230
self._base = base
231-
self.api_call_part1 = api_call_part1
232-
self.api_call_part2 = api_call_part2
233-
self.api_call_part3 = api_call_part3
231+
# self.api_call_part1 = api_call_part1
232+
# self.api_call_part2 = api_call_part2
233+
# self.api_call_part3 = api_call_part3
234234

235235
def get(self, identifier1=None, identifier2=None, params=None, data=None):
236236
""" Cloudflare v4 API"""
@@ -272,11 +272,11 @@ def get(self, identifier1=None, identifier2=None, params=None, data=None):
272272
""" Cloudflare v4 API"""
273273

274274
return self._base.call_with_no_auth('GET',
275-
self.api_call_part1,
276-
self.api_call_part2,
277-
self.api_call_part3,
278-
identifier1, identifier2,
279-
params, data)
275+
self.api_call_part1,
276+
self.api_call_part2,
277+
self.api_call_part3,
278+
identifier1, identifier2,
279+
params, data)
280280

281281
def patch(self, identifier1=None, identifier2=None, params=None, data=None):
282282
""" Cloudflare v4 API"""
@@ -313,51 +313,51 @@ def get(self, identifier1=None, identifier2=None, params=None, data=None):
313313
""" Cloudflare v4 API"""
314314

315315
return self._base.call_with_auth('GET',
316-
self.api_call_part1,
317-
self.api_call_part2,
318-
self.api_call_part3,
319-
identifier1, identifier2,
320-
params, data)
316+
self.api_call_part1,
317+
self.api_call_part2,
318+
self.api_call_part3,
319+
identifier1, identifier2,
320+
params, data)
321321

322322
def patch(self, identifier1=None, identifier2=None, params=None, data=None):
323323
""" Cloudflare v4 API"""
324324

325325
return self._base.call_with_auth('PATCH',
326-
self.api_call_part1,
327-
self.api_call_part2,
328-
self.api_call_part3,
329-
identifier1, identifier2,
330-
params, data)
326+
self.api_call_part1,
327+
self.api_call_part2,
328+
self.api_call_part3,
329+
identifier1, identifier2,
330+
params, data)
331331

332332
def post(self, identifier1=None, identifier2=None, params=None, data=None):
333333
""" Cloudflare v4 API"""
334334

335335
return self._base.call_with_auth('POST',
336-
self.api_call_part1,
337-
self.api_call_part2,
338-
self.api_call_part3,
339-
identifier1, identifier2,
340-
params, data)
336+
self.api_call_part1,
337+
self.api_call_part2,
338+
self.api_call_part3,
339+
identifier1, identifier2,
340+
params, data)
341341

342342
def put(self, identifier1=None, identifier2=None, params=None, data=None):
343343
""" Cloudflare v4 API"""
344344

345345
return self._base.call_with_auth('PUT',
346-
self.api_call_part1,
347-
self.api_call_part2,
348-
self.api_call_part3,
349-
identifier1, identifier2,
350-
params, data)
346+
self.api_call_part1,
347+
self.api_call_part2,
348+
self.api_call_part3,
349+
identifier1, identifier2,
350+
params, data)
351351

352352
def delete(self, identifier1=None, identifier2=None, params=None, data=None):
353353
""" Cloudflare v4 API"""
354354

355355
return self._base.call_with_auth('DELETE',
356-
self.api_call_part1,
357-
self.api_call_part2,
358-
self.api_call_part3,
359-
identifier1, identifier2,
360-
params, data)
356+
self.api_call_part1,
357+
self.api_call_part2,
358+
self.api_call_part3,
359+
identifier1, identifier2,
360+
params, data)
361361

362362
class _add_with_cert_auth(object):
363363
""" Cloudflare v4 API"""
@@ -420,6 +420,29 @@ def delete(self, identifier1=None, identifier2=None, params=None, data=None):
420420
identifier1, identifier2,
421421
params, data)
422422

423+
def api_list(self, m=None, s=''):
424+
"""recursive walk of the api tree returning a list of api calls"""
425+
if m == None:
426+
m = self
427+
w = []
428+
for n in sorted(dir(m)):
429+
if n[0] == '_':
430+
# internal
431+
continue
432+
if n in ['delete', 'get', 'patch', 'post', 'put']:
433+
# gone too far
434+
continue
435+
a = getattr(m, n)
436+
d = dir(a)
437+
if '_base' in d:
438+
# it's a known api call - lets show the result and continue down the tree
439+
if 'delete' in d or 'get' in d or 'patch' in d or 'post' in d or 'put' in d:
440+
# only show the result if a call exists for this part
441+
if 'api_call_part1' in d:
442+
w.append(s + '/' + n)
443+
w = w + self.api_list(a, s + '/' + n)
444+
return w
445+
423446
def __init__(self, email=None, token=None, certtoken=None, debug=False, raw=False):
424447
""" Cloudflare v4 API"""
425448

cli4/cli4.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,27 +148,10 @@ def convert_load_balancers_map_regions(cf, region_name):
148148
return region_name
149149
exit('cli4: %s - no region found' % (region_name))
150150

151-
def walk(m, s):
152-
"""recursive walk of the tree"""
153-
for n in sorted(dir(m)):
154-
if n[0] == '_':
155-
# internal
156-
continue
157-
if n in ['delete', 'get', 'patch', 'post', 'put']:
158-
# gone too far
159-
continue
160-
a = getattr(m, n)
161-
d = dir(a)
162-
if '_base' in d:
163-
# it's a known api call - lets show the result and continue down the tree
164-
if 'delete' in d or 'get' in d or 'patch' in d or 'post' in d or 'put' in d:
165-
# only show the result if a call exists for this part
166-
print(s + '/' + n)
167-
walk(a, s + '/' + n)
168-
169151
def dump_commands(cf):
170152
"""dump a tree of all the known API commands"""
171-
walk(cf, '')
153+
w = cf.api_list()
154+
print('\n'.join(w))
172155

173156
def cli4(args):
174157
"""Cloudflare API via command line"""

0 commit comments

Comments
 (0)