Skip to content

Commit 898a64a

Browse files
author
Sharoon Thomas
committed
Merge branch 'master' of github.com:mbi
2 parents 3c5ffed + 5bae039 commit 898a64a

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

__init__.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,10 @@
1414

1515
try:
1616
import simplejson as json
17-
from simplejson import JSONDecodeError
1817
except ImportError:
1918
import json
2019

21-
class JSONDecodeError(Exception):
22-
pass
23-
# Ugly: No alternative because this exception class doesnt seem to be there
24-
# in the standard python module
25-
import urllib
26-
import urllib2
20+
import requests
2721
import warnings
2822
import logging
2923

@@ -98,15 +92,16 @@ def get_access_token(self):
9892
9993
:return: The access token to be used with subsequent requests
10094
"""
101-
args = urllib.urlencode({
95+
args = {
10296
'client_id': self.client_id,
10397
'client_secret': self.client_secret,
10498
'scope': self.scope,
10599
'grant_type': self.grant_type
106-
})
107-
response = json.loads(urllib.urlopen(
108-
'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13', args
109-
).read())
100+
}
101+
response = requests.post(
102+
'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13',
103+
data=args
104+
).json()
110105

111106
self.logger.debug(response)
112107

@@ -123,18 +118,19 @@ def call(self, url, params):
123118
if not self.access_token:
124119
self.access_token = self.get_access_token()
125120

126-
request = urllib2.Request(
127-
"%s?%s" % (url, urllib.urlencode(params)),
121+
resp = requests.get(
122+
"%s" % url,
123+
params=params,
128124
headers={'Authorization': 'Bearer %s' % self.access_token}
129125
)
130-
response = urllib2.urlopen(request).read()
131-
rv = json.loads(response.decode("UTF-8-sig"))
126+
resp.encoding = 'UTF-8-sig'
127+
rv = resp.json()
132128

133-
if isinstance(rv, basestring) and \
129+
if isinstance(rv, str) and \
134130
rv.startswith("ArgumentOutOfRangeException"):
135131
raise ArgumentOutOfRangeException(rv)
136132

137-
if isinstance(rv, basestring) and \
133+
if isinstance(rv, str) and \
138134
rv.startswith("TranslateApiException"):
139135
raise TranslateApiException(rv)
140136

setup.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,7 @@ def read(fname):
6060
"Topic :: Utilities"
6161
],
6262
test_suite="microsofttranslator.test.test_all",
63+
install_requires=[
64+
'requests >= 1.2.3',
65+
]
6366
)

0 commit comments

Comments
 (0)