Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit d4f1ebd

Browse files
committed
fixed logger lint issue, added add() for cleaner command loading, added html response support for madia
1 parent 2988312 commit d4f1ebd

1 file changed

Lines changed: 86 additions & 27 deletions

File tree

CloudFlare/cloudflare.py

Lines changed: 86 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,18 @@ def _network(self, method, headers, parts,
121121
""" Cloudflare v4 API"""
122122

123123
if self.logger:
124-
self.logger.debug('Call: %s,%s,%s,%s,%s,%s' % (str(parts[0]),
125-
str(identifier1),
126-
str(parts[1]),
127-
str(identifier2),
128-
str(parts[2]),
129-
str(identifier3)))
130-
self.logger.debug('Call: optional params and data %s %s' % (str(params),
131-
str(data)))
124+
self.logger.debug('Call: %s,%s,%s,%s,%s,%s',
125+
str(parts[0]),
126+
str(identifier1),
127+
str(parts[1]),
128+
str(identifier2),
129+
str(parts[2]),
130+
str(identifier3))
131+
self.logger.debug('Call: optional params and data %s %s',
132+
str(params),
133+
str(data))
132134
if files:
133-
self.logger.debug('Call: upload file %r' % (files))
135+
self.logger.debug('Call: upload file %r', files)
134136

135137
if (method is None) or (parts[0] is None):
136138
# should never happen
@@ -164,8 +166,8 @@ def _network(self, method, headers, parts,
164166
url += '/' + identifier3
165167

166168
if self.logger:
167-
self.logger.debug('Call: method and url %s %s' % (str(method), str(url)))
168-
self.logger.debug('Call: headers %s' % str(sanitize_secrets(headers)))
169+
self.logger.debug('Call: method and url %s %s', str(method), str(url))
170+
self.logger.debug('Call: headers %s', str(sanitize_secrets(headers)))
169171

170172
method = method.upper()
171173

@@ -259,7 +261,8 @@ def _network(self, method, headers, parts,
259261
response_data = response_data.decode("utf-8")
260262

261263
if self.logger:
262-
self.logger.debug('Response: %d, %s, %s' % (response_code, response_type, response_data))
264+
self.logger.debug('Response: %d, %s, %s',
265+
response_code, response_type, response_data)
263266

264267
if response_code >= 500 and response_code <= 599:
265268
# 500 Internal Server Error
@@ -310,7 +313,9 @@ def _raw(self, method, headers, parts,
310313

311314
[response_type, response_code, response_data] = self._network(method,
312315
headers, parts,
313-
identifier1, identifier2, identifier3,
316+
identifier1,
317+
identifier2,
318+
identifier3,
314319
params, data, files)
315320

316321
if response_type == 'application/json':
@@ -325,13 +330,16 @@ def _raw(self, method, headers, parts,
325330
# This should really be 'null' but it isn't. Even then, it's wrong!
326331
if response_code == requests.codes.ok:
327332
# 200 ok
328-
response_data = {'success': True, 'result': None}
333+
response_data = {'success': True,
334+
'result': None}
329335
else:
330336
# 3xx & 4xx errors
331-
response_data = {'success': False, 'code': response_code, 'result': None}
337+
response_data = {'success': False,
338+
'code': response_code,
339+
'result': None}
332340
else:
333341
# While this should not happen; it's always possible
334-
self.logger.debug('Response data not JSON: %r' % (response_data))
342+
self.logger.debug('Response data not JSON: %r', response_data)
335343
raise CloudFlareAPIError(0, 'JSON parse failed - report to Cloudflare.')
336344

337345
if response_code == requests.codes.ok:
@@ -356,24 +364,44 @@ def _raw(self, method, headers, parts,
356364
response_data = {'success': True, 'result': str(response_data)}
357365
else:
358366
# 3xx & 4xx errors
359-
response_data = {'success': False, 'code': response_code, 'result': str(response_data)}
367+
response_data = {'success': False,
368+
'code': response_code,
369+
'result': str(response_data)}
360370
elif response_type == 'text/javascript' or response_type == 'application/javascript':
361371
# used by Cloudflare workers
362372
if response_code == requests.codes.ok:
363373
# 200 ok
364-
response_data = {'success': True, 'result': str(response_data)}
374+
response_data = {'success': True,
375+
'result': str(response_data)}
365376
else:
366377
# 3xx & 4xx errors
367-
response_data = {'success': False, 'code': response_code, 'result': str(response_data)}
378+
response_data = {'success': False,
379+
'code': response_code,
380+
'result': str(response_data)}
381+
elif response_type == 'text/html':
382+
# used by media for preview
383+
if response_code == requests.codes.ok:
384+
# 200 ok
385+
response_data = {'success': True,
386+
'result': str(response_data)}
387+
else:
388+
# 3xx & 4xx errors
389+
response_data = {'success': False,
390+
'code': response_code,
391+
'result': str(response_data)}
392+
368393
else:
369394
# Assuming nothing - but continuing anyway
370395
# A single value is returned (vs an array or object)
371396
if response_code == requests.codes.ok:
372397
# 200 ok
373-
response_data = {'success': True, 'result': str(response_data)}
398+
response_data = {'success': True,
399+
'result': str(response_data)}
374400
else:
375401
# 3xx & 4xx errors
376-
response_data = {'success': False, 'code': response_code, 'result': str(response_data)}
402+
response_data = {'success': False,
403+
'code': response_code,
404+
'result': str(response_data)}
377405

378406
# it would be nice to return the error code and content type values; but not quite yet
379407
return response_data
@@ -417,22 +445,25 @@ def _call(self, method, headers, parts,
417445
message = errors['error']
418446
else:
419447
message = ''
448+
if 'messages' in response_data:
449+
errors['error_chain'] = response_data['messages']
420450
if 'error_chain' in errors:
421451
error_chain = errors['error_chain']
422452
for error in error_chain:
423453
if self.logger:
424-
self.logger.debug('Response: error %d %s - chain' %
425-
(error['code'], error['message']))
454+
self.logger.debug('Response: error %d %s - chain',
455+
error['code'],
456+
error['message'])
426457
if self.logger:
427-
self.logger.debug('Response: error %d %s' % (code, message))
458+
self.logger.debug('Response: error %d %s', code, message)
428459
raise CloudFlareAPIError(code, message, error_chain)
429460
else:
430461
if self.logger:
431-
self.logger.debug('Response: error %d %s' % (code, message))
462+
self.logger.debug('Response: error %d %s', code, message)
432463
raise CloudFlareAPIError(code, message)
433464

434465
if self.logger:
435-
self.logger.debug('Response: %s' % (response_data['result']))
466+
self.logger.debug('Response: %s', response_data['result'])
436467
if self.raw:
437468
result = {}
438469
# theres always a result value
@@ -455,7 +486,7 @@ def _call_unwrapped(self, method, headers, parts,
455486
identifier1, identifier2, identifier3,
456487
params, data, files)
457488
if self.logger:
458-
self.logger.debug('Response: %s' % (response_data))
489+
self.logger.debug('Response: %s', response_data)
459490
result = response_data
460491
return result
461492

@@ -716,6 +747,34 @@ def delete(self, identifier1=None, identifier2=None, identifier3=None, params=No
716747
identifier1, identifier2, identifier3,
717748
params, data)
718749

750+
def add(self, t, branch, api_call_part1, api_call_part2=None, api_call_part3=None):
751+
"""add api call to class"""
752+
if api_call_part3:
753+
name = api_call_part3.rsplit('/', 1)[-1]
754+
elif api_call_part2:
755+
name = api_call_part2.rsplit('/', 1)[-1]
756+
elif api_call_part1:
757+
name = api_call_part1.rsplit('/', 1)[-1]
758+
else:
759+
# should never happen
760+
raise CloudFlareAPIError(0, 'api load name failed')
761+
762+
if t == 'VOID':
763+
f = self._add_unused(self._base, api_call_part1, api_call_part2, api_call_part3)
764+
elif t == 'OPEN':
765+
f = self._add_noauth(self._base, api_call_part1, api_call_part2, api_call_part3)
766+
elif t == 'AUTH':
767+
f = self._add_with_auth(self._base, api_call_part1, api_call_part2, api_call_part3)
768+
elif t == 'CERT':
769+
f = self._add_with_cert_auth(self._base, api_call_part1, api_call_part2, api_call_part3)
770+
elif t == 'AUTH_UNWRAPPED':
771+
f = self._add_with_auth_unwrapped(self._base, api_call_part1, api_call_part2, api_call_part3)
772+
else:
773+
# should never happen
774+
raise CloudFlareAPIError(0, 'api load type mismatch')
775+
776+
setattr(branch, name, f)
777+
719778
def api_list(self, m=None, s=''):
720779
"""recursive walk of the api tree returning a list of api calls"""
721780
if m is None:

0 commit comments

Comments
 (0)