Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Python 2.7, Python 3.* interoperation-compatibility for json encoding #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions twilio/base/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
from twilio.base.exceptions import TwilioRestException


def json_bytes_str_compatible(func):
def decorator(response_content):
# Python 2.7
if isinstance(response_content, str):
return func(response_content)
# Python 3.*
return func(response_content.decode('utf-8'))
return decorator


@json_bytes_str_compatible
def encode_to_json(content):
return json.loads(content)


class Version(object):
"""
Represents an API version.
Expand Down Expand Up @@ -81,7 +96,7 @@ def fetch(self, method, uri, params=None, data=None, headers=None, auth=None, ti
if response.status_code < 200 or response.status_code >= 300:
raise self.exception(method, uri, response, 'Unable to fetch record')

return json.loads(response.content)
return encode_to_json(response.content)

def update(self, method, uri, params=None, data=None, headers=None, auth=None, timeout=None,
allow_redirects=False):
Expand All @@ -102,7 +117,7 @@ def update(self, method, uri, params=None, data=None, headers=None, auth=None, t
if response.status_code < 200 or response.status_code >= 300:
raise self.exception(method, uri, response, 'Unable to update record')

return json.loads(response.content)
return encode_to_json(response.content)

def delete(self, method, uri, params=None, data=None, headers=None, auth=None, timeout=None,
allow_redirects=False):
Expand Down Expand Up @@ -208,5 +223,4 @@ def create(self, method, uri, params=None, data=None, headers=None, auth=None, t
if response.status_code < 200 or response.status_code >= 300:
raise self.exception(method, uri, response, 'Unable to create record')

return json.loads(response.content)

return encode_to_json(response.content)