Skip to content

Commit

Permalink
dashes vs underscores - finally tamed!
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Jul 20, 2020
1 parent 25ac677 commit f3652ad
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -855,14 +855,20 @@ def add(self, t, p1, p2=None, p3=None):
branch = self
for element in a[0:-1]:
try:
branch = getattr(branch, element)
if '-' in element:
branch = getattr(element, element.replace('-','_'))
else:
branch = getattr(branch, element)
except:
# should never happen
raise CloudFlareAPIError(0, 'api load name failed')
name = a[-1]

try:
f = getattr(branch, name)
if '-' in name:
f = getattr(element, name.replace('-','_'))
else:
f = getattr(branch, name)
# already exists - don't let it overwrite
raise CloudFlareAPIError(0, 'api duplicate name found: %s/**%s**' % ('/'.join(a[0:-1]), name))
except AttributeError:
Expand All @@ -886,7 +892,8 @@ def add(self, t, p1, p2=None, p3=None):
if '-' in name:
# dashes (vs underscores) cause issues in Python and other languages
setattr(branch, name.replace('-','_'), f)
setattr(branch, name, f)
else:
setattr(branch, name, f)

def api_list(self, m=None, s=''):
"""recursive walk of the api tree returning a list of api calls"""
Expand All @@ -907,7 +914,8 @@ def api_list(self, m=None, s=''):
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 '_parts' in d:
w.append(s + '/' + n)
# handle underscores by returning the actual API call vs the method name
w.append(str(a)[1:-1].replace('/:id/','/'))
w = w + self.api_list(a, s + '/' + n)
return w

Expand Down

0 comments on commit f3652ad

Please sign in to comment.