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

Commit 82b5cb7

Browse files
committed
added graphql handling with no results in response, corrected handling when error code is missing
1 parent 4c90fa8 commit 82b5cb7

1 file changed

Lines changed: 39 additions & 10 deletions

File tree

CloudFlare/cloudflare.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -465,9 +465,29 @@ def _call(self, method, headers, parts,
465465
# Sanatize the returned results - just in case API is messed up
466466
if 'success' not in response_data:
467467
if 'errors' in response_data:
468-
if self.logger:
469-
self.logger.debug('Response: assuming success = "False"')
470-
response_data['success'] = False
468+
if response_data['errors'] == None:
469+
# Only happens on /graphql call
470+
if self.logger:
471+
self.logger.debug('Response: assuming success = "True"')
472+
response_data['success'] = True
473+
else:
474+
if self.logger:
475+
self.logger.debug('Response: assuming success = "False"')
476+
# The following only happens on /graphql call
477+
try:
478+
message = response_data['errors'][0]['message']
479+
except:
480+
message = ''
481+
try:
482+
location = str(response_data['errors'][0]['location'])
483+
except:
484+
location = ''
485+
try:
486+
path = '>'.join(response_data['errors'][0]['path'])
487+
except:
488+
path = ''
489+
response_data['errors'] = [{'code': 99999, 'message': message + ' - ' + location + ' - ' + path}]
490+
response_data['success'] = False
471491
else:
472492
if 'result' not in response_data:
473493
# Only happens on /certificates call
@@ -485,7 +505,10 @@ def _call(self, method, headers, parts,
485505

486506
if response_data['success'] is False:
487507
errors = response_data['errors'][0]
488-
code = errors['code']
508+
if 'code' in errors:
509+
code = errors['code']
510+
else:
511+
code = 99998
489512
if 'message' in errors:
490513
message = errors['message']
491514
elif 'error' in errors:
@@ -509,19 +532,25 @@ def _call(self, method, headers, parts,
509532
self.logger.debug('Response: error %d %s', code, message)
510533
raise CloudFlareAPIError(code, message)
511534

512-
if self.logger:
513-
self.logger.debug('Response: %s', response_data['result'])
514535
if self.raw:
515536
result = {}
516-
# theres always a result value
517-
result['result'] = response_data['result']
537+
# theres always a result value - unless it's a graphql query
538+
try:
539+
result['result'] = response_data['result']
540+
except:
541+
result['result'] = response_data
518542
# theres may not be a result_info on every call
519543
if 'result_info' in response_data:
520544
result['result_info'] = response_data['result_info']
521545
# no need to return success, errors, or messages as they return via an exception
522546
else:
523-
# theres always a result value
524-
result = response_data['result']
547+
# theres always a result value - unless it's a graphql query
548+
try:
549+
result = response_data['result']
550+
except:
551+
result = response_data
552+
if self.logger:
553+
self.logger.debug('Response: %s', result)
525554
return result
526555

527556
def _call_unwrapped(self, method, headers, parts,

0 commit comments

Comments
 (0)