Skip to content

Commit

Permalink
handled accounts/:account_identifier/storage/kv/namespaces/:namespace…
Browse files Browse the repository at this point in the history
…_identifier/values/:key_name return as binary data
  • Loading branch information
mahtin committed Jun 18, 2020
1 parent 355ae5d commit 84f57b4
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def _network(self, method, headers, parts,
response_type = 'application/octet-stream'
response_code = response.status_code
response_data = response.content
if not isinstance(response_data, str):
if not isinstance(response_data, (str, bytes, bytearray)):
response_data = response_data.decode("utf-8")

if self.logger:
Expand Down Expand Up @@ -364,6 +364,9 @@ def _raw(self, method, headers, parts,
response_data = response_data.decode('utf-8')
try:
response_data = json.loads(response_data)
if not isinstance(response_data, (dict)):
response_data = {'success': True,
'result': response_data}
except ValueError:
if response_data == '':
# This should really be 'null' but it isn't. Even then, it's wrong!
Expand Down Expand Up @@ -397,23 +400,64 @@ def _raw(self, method, headers, parts,
# 3xx & 4xx errors - we should report that somehow - but not quite yet
# response_data['code'] = response_code
pass
elif response_type == 'application/octet-stream' and isinstance(response_data, (int, float)):
# It's binary data
if response_code == requests.codes.ok:
# 200 ok
response_data = {'success': True,
'result': response_data}
else:
# 3xx & 4xx errors
response_data = {'success': False,
'code': response_code,
'result': response_data}
elif response_type == 'application/octet-stream' and isinstance(response_data, (bytes, bytearray)):
# API says it's text; but maybe it's actually JSON? - should be fixed in API
if hasattr(response_data, 'decode'):
response_data = response_data.decode('utf-8')
try:
response_data = json.loads(response_data)
if not isinstance(response_data, (dict)) or 'success' not in response_data:
if response_code == requests.codes.ok:
# 200 ok
response_data = {'success': True,
'result': response_data}
else:
# 3xx & 4xx errors
response_data = {'success': False,
'code': response_code,
'result': response_data}
except ValueError:
# So it wasn't JSON - moving on as if it's text!
# A single value is returned (vs an array or object)
if response_code == requests.codes.ok:
# 200 ok
response_data = {'success': True, 'result': response_data}
else:
# 3xx & 4xx errors
response_data = {'success': False,
'code': response_code,
'result': response_data}
elif response_type == 'text/plain' or response_type == 'application/octet-stream':
# API says it's text; but maybe it's actually JSON? - should be fixed in API
if hasattr(response_data, 'decode'):
response_data = response_data.decode('utf-8')
try:
response_data = json.loads(response_data)
if not isinstance(response_data, (dict)):
response_data = {'success': True,
'result': response_data}
except ValueError:
# So it wasn't JSON - moving on as if it's text!
# A single value is returned (vs an array or object)
if response_code == requests.codes.ok:
# 200 ok
response_data = {'success': True, 'result': str(response_data)}
response_data = {'success': True, 'result': response_data}
else:
# 3xx & 4xx errors
response_data = {'success': False,
'code': response_code,
'result': str(response_data)}
'result': response_data}
elif response_type == 'text/javascript' or response_type == 'application/javascript':
# used by Cloudflare workers
if response_code == requests.codes.ok:
Expand Down

0 comments on commit 84f57b4

Please sign in to comment.