From 52dda2ededa1aa8fd21e7bc2dff2c1f2d2c58a9a Mon Sep 17 00:00:00 2001 From: Martin Levy Date: Sun, 11 Dec 2016 11:22:02 -0800 Subject: [PATCH] sanatize the returned results - just in case API is messed up --- CHANGELOG.md | 1 + CloudFlare/cloudflare.py | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1de7ccf..ff5e999 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # Change Log + - 2016-12-10 15:39:29 -0800 [080733b](https://github.com/cloudflare/python-cloudflare/commit/080733b58e144670116d7f3ccadf369d599bfc61) CHANGELOG.md pushed to github - 2016-12-10 15:39:01 -0800 [f3d6377](https://github.com/cloudflare/python-cloudflare/commit/f3d637727d74acecd8faf4c9b6d652ee2cff183f) 1.4.1 release - 2016-12-10 15:38:10 -0800 [8c66d32](https://github.com/cloudflare/python-cloudflare/commit/8c66d3253b6f257b61b0116600fb2b3d77e8f0fb) cleanup of yaml print output if yaml package isnt installed - 2016-12-09 16:22:51 -0800 [894ae11](https://github.com/cloudflare/python-cloudflare/commit/894ae11788a2b1997e4f58895d205d1f74ab16f7) Moved walk into CloudFlare class - much cleaner diff --git a/CloudFlare/cloudflare.py b/CloudFlare/cloudflare.py index 6e6c96e..29e57ea 100644 --- a/CloudFlare/cloudflare.py +++ b/CloudFlare/cloudflare.py @@ -77,7 +77,7 @@ def call_with_certauth(self, method, params=None, data=None): """ Cloudflare v4 API""" - if self.certtoken is '': + if self.certtoken is '' or self.certtoken is None: raise CloudFlareAPIError(0, 'no cert token defined') headers = { 'X-Auth-User-Service-Key': self.certtoken, @@ -184,6 +184,27 @@ def _call(self, method, headers, api_call_part1, api_call_part2, api_call_part3, identifier1, identifier2, params, data) + + # Sanatize the returned results - just in case API is messed up + if 'success' not in response_data: + if 'errors' in response_data: + if self.logger: + self.logger.debug('Response: missing success value - assuming success = "False"') + response_data['success'] = False + else: + if 'result' not in response_data: + # Only happens on /certificates call - and should be fixed in /certificates API + if self.logger: + self.logger.debug('Response: missing success and error value - assuming success = "False"') + r = response_data + response_data['errors'] = [] + response_data['errors'].append(r) + response_data['success'] = False + else: + if self.logger: + self.logger.debug('Response: missing success value - assuming success = "True"') + response_data['success'] = True + if response_data['success'] is False: errors = response_data['errors'][0] code = errors['code']