diff --git a/.travis.yml b/.travis.yml index 8e31262..b869767 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ language: python python: - "2.7" + - "3.4" install: - pip install flake8 - python setup.py install diff --git a/__init__.py b/__init__.py index e9f5233..f3a3772 100644 --- a/__init__.py +++ b/__init__.py @@ -14,16 +14,10 @@ try: import simplejson as json - from simplejson import JSONDecodeError except ImportError: import json - class JSONDecodeError(Exception): - pass - # Ugly: No alternative because this exception class doesnt seem to be there - # in the standard python module -import urllib -import urllib2 +import requests import warnings import logging @@ -98,15 +92,16 @@ def get_access_token(self): :return: The access token to be used with subsequent requests """ - args = urllib.urlencode({ + args = { 'client_id': self.client_id, 'client_secret': self.client_secret, 'scope': self.scope, 'grant_type': self.grant_type - }) - response = json.loads(urllib.urlopen( - 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13', args - ).read()) + } + response = requests.post( + 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13', + data=args + ).json() self.logger.debug(response) @@ -123,18 +118,19 @@ def call(self, url, params): if not self.access_token: self.access_token = self.get_access_token() - request = urllib2.Request( - "%s?%s" % (url, urllib.urlencode(params)), + resp = requests.get( + "%s" % url, + params=params, headers={'Authorization': 'Bearer %s' % self.access_token} ) - response = urllib2.urlopen(request).read() - rv = json.loads(response.decode("UTF-8-sig")) + resp.encoding = 'UTF-8-sig' + rv = resp.json() - if isinstance(rv, basestring) and \ + if isinstance(rv, str) and \ rv.startswith("ArgumentOutOfRangeException"): raise ArgumentOutOfRangeException(rv) - if isinstance(rv, basestring) and \ + if isinstance(rv, str) and \ rv.startswith("TranslateApiException"): raise TranslateApiException(rv) diff --git a/setup.py b/setup.py index d007718..523aacf 100644 --- a/setup.py +++ b/setup.py @@ -51,13 +51,18 @@ def read(fname): url="http://openlabs.co.in/", include_package_data=True, classifiers=[ - "Development Status :: 4 - Beta", + "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Natural Language :: English", "Operating System :: OS Independent", "Topic :: Software Development :: Internationalization", - "Topic :: Utilities" + "Topic :: Utilities", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 2", ], test_suite="microsofttranslator.test.test_all", + install_requires=[ + 'requests >= 1.2.3', + ] )