Skip to content
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

Use the json module as fallback for simplejson, and #4

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,11 @@ The following example shows you how to import an image from somewhere on the int

result = client.request(**params)
print result


## Installing on Python 2.6 or older
The package depends on the ``json`` module added in Python 2.7, but we fall
back to importing ``simplejson`` if ``json`` is not available. To make
``python-transloadit`` to work in Python 2.6 or older, install ``simplejson``:

$ pip install simplejson
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
from setuptools import setup, find_packages

setup(name="transloadit",
version="0.0.1",
version="0.0.5",
description="Library for interfacing with Transloadit's API",
author="Joe Stump",
author_email="[email protected]",
url="http://github.com/joestump/python-transloadit",
packages = find_packages(),
license = "MIT License",
install_requires=['simplejson>=2.1.6'],
keywords="transloadit",
zip_safe = True,
tests_require=['nose', 'coverage'])
17 changes: 14 additions & 3 deletions transloadit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@


try:
from django.utils import simplejson as json
except ImportError:
# Should be (manually) installed if using Python 2.6 or older,
# or if you want the speedup for raw bytestrings provided by
# simplejson.
import simplejson as json
except ImportError:
import json # Works with python 2.7+


BASE_API = 'http://api2.transloadit.com/assemblies'
Expand Down Expand Up @@ -45,7 +48,15 @@ def _send_request(self, files, **fields):
req.endheaders()
req.send(body)
errcode, errmsg, headers = req.getreply()
return json.loads(req.file.read())
output = req.file.read()
try:
return json.loads(output)
except json.JSONDecodeError:
# Workaround for https://github.com/joestump/python-transloadit/issues/5
output = '{' + output.split('{', 1)[-1]
output = output.rsplit('}', 1)[0] + '}'
return json.loads(output)


def _encode_request(self, fields, files):
body = StringIO()
Expand Down