Skip to content

Commit ddea5a8

Browse files
committed
Decode byte-strings (more)
1. Plotly uses 2.7 on the backend so it will return bytes! The json 3.x module expects native string (unicode), so we need to convert this. 2. base64encode requires bytes, so give it bytes and convert after.
1 parent 069a7dc commit ddea5a8

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

plotly/exceptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
"""
1818
import json
19+
import six
1920

2021
## Base Plotly Error ##
2122

@@ -36,7 +37,9 @@ def __init__(self, requests_exception):
3637
if 'json' in content_type:
3738
content = requests_exception.response.content
3839
if content != '':
39-
res_payload = json.loads(requests_exception.response.content)
40+
res_payload = json.loads(
41+
requests_exception.response.content.decode('utf8')
42+
)
4043
if 'detail' in res_payload:
4144
self.message = res_payload['detail']
4245
else:

plotly/plotly/plotly.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,7 @@ def response_handler(cls, response):
913913
'json' in response.headers['content-type'] and
914914
len(response.content) > 0):
915915

916-
response_dict = json.loads(response.content)
916+
response_dict = json.loads(response.content.decode('utf8'))
917917

918918
if 'warnings' in response_dict and len(response_dict['warnings']):
919919
warnings.warn('\n'.join(response_dict['warnings']))
@@ -928,7 +928,10 @@ def api_url(cls, resource):
928928
@classmethod
929929
def headers(cls):
930930
un, api_key = _get_session_username_and_key()
931-
encoded_un_key_pair = base64.b64encode('{}:{}'.format(un, api_key))
931+
encoded_un_key_pair = base64.b64encode(
932+
six.b('{}:{}'.format(un, api_key))
933+
).decode('utf8')
934+
932935
return {
933936
'authorization': 'Basic ' + encoded_un_key_pair,
934937
'plotly-client-platform': 'python {}'.format(version.__version__)

0 commit comments

Comments
 (0)