Skip to content

Commit 84f57b4

Browse files
committed
handled accounts/:account_identifier/storage/kv/namespaces/:namespace_identifier/values/:key_name return as binary data
1 parent 355ae5d commit 84f57b4

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

CloudFlare/cloudflare.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def _network(self, method, headers, parts,
296296
response_type = 'application/octet-stream'
297297
response_code = response.status_code
298298
response_data = response.content
299-
if not isinstance(response_data, str):
299+
if not isinstance(response_data, (str, bytes, bytearray)):
300300
response_data = response_data.decode("utf-8")
301301

302302
if self.logger:
@@ -364,6 +364,9 @@ def _raw(self, method, headers, parts,
364364
response_data = response_data.decode('utf-8')
365365
try:
366366
response_data = json.loads(response_data)
367+
if not isinstance(response_data, (dict)):
368+
response_data = {'success': True,
369+
'result': response_data}
367370
except ValueError:
368371
if response_data == '':
369372
# This should really be 'null' but it isn't. Even then, it's wrong!
@@ -397,23 +400,64 @@ def _raw(self, method, headers, parts,
397400
# 3xx & 4xx errors - we should report that somehow - but not quite yet
398401
# response_data['code'] = response_code
399402
pass
403+
elif response_type == 'application/octet-stream' and isinstance(response_data, (int, float)):
404+
# It's binary data
405+
if response_code == requests.codes.ok:
406+
# 200 ok
407+
response_data = {'success': True,
408+
'result': response_data}
409+
else:
410+
# 3xx & 4xx errors
411+
response_data = {'success': False,
412+
'code': response_code,
413+
'result': response_data}
414+
elif response_type == 'application/octet-stream' and isinstance(response_data, (bytes, bytearray)):
415+
# API says it's text; but maybe it's actually JSON? - should be fixed in API
416+
if hasattr(response_data, 'decode'):
417+
response_data = response_data.decode('utf-8')
418+
try:
419+
response_data = json.loads(response_data)
420+
if not isinstance(response_data, (dict)) or 'success' not in response_data:
421+
if response_code == requests.codes.ok:
422+
# 200 ok
423+
response_data = {'success': True,
424+
'result': response_data}
425+
else:
426+
# 3xx & 4xx errors
427+
response_data = {'success': False,
428+
'code': response_code,
429+
'result': response_data}
430+
except ValueError:
431+
# So it wasn't JSON - moving on as if it's text!
432+
# A single value is returned (vs an array or object)
433+
if response_code == requests.codes.ok:
434+
# 200 ok
435+
response_data = {'success': True, 'result': response_data}
436+
else:
437+
# 3xx & 4xx errors
438+
response_data = {'success': False,
439+
'code': response_code,
440+
'result': response_data}
400441
elif response_type == 'text/plain' or response_type == 'application/octet-stream':
401442
# API says it's text; but maybe it's actually JSON? - should be fixed in API
402443
if hasattr(response_data, 'decode'):
403444
response_data = response_data.decode('utf-8')
404445
try:
405446
response_data = json.loads(response_data)
447+
if not isinstance(response_data, (dict)):
448+
response_data = {'success': True,
449+
'result': response_data}
406450
except ValueError:
407451
# So it wasn't JSON - moving on as if it's text!
408452
# A single value is returned (vs an array or object)
409453
if response_code == requests.codes.ok:
410454
# 200 ok
411-
response_data = {'success': True, 'result': str(response_data)}
455+
response_data = {'success': True, 'result': response_data}
412456
else:
413457
# 3xx & 4xx errors
414458
response_data = {'success': False,
415459
'code': response_code,
416-
'result': str(response_data)}
460+
'result': response_data}
417461
elif response_type == 'text/javascript' or response_type == 'application/javascript':
418462
# used by Cloudflare workers
419463
if response_code == requests.codes.ok:

0 commit comments

Comments
 (0)