1212
1313__all__ = ['Translator' , 'TranslateApiException' ]
1414
15- try :
16- import simplejson as json
17- from simplejson import JSONDecodeError
18- except ImportError :
19- import json
20- class JSONDecodeError (Exception ): pass
21- # Ugly: No alternative because this exception class doesnt seem to be there
22- # in the standard python module
23- import urllib
24- import urllib2
15+ import requests
2516import warnings
2617import logging
2718
@@ -96,15 +87,16 @@ def get_access_token(self):
9687
9788 :return: The access token to be used with subsequent requests
9889 """
99- args = urllib . urlencode ( {
90+ args = {
10091 'client_id' : self .client_id ,
10192 'client_secret' : self .client_secret ,
10293 'scope' : self .scope ,
10394 'grant_type' : self .grant_type
104- })
105- response = json .loads (urllib .urlopen (
106- 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13' , args
107- ).read ())
95+ }
96+ response = requests .post (
97+ 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13' ,
98+ data = args
99+ ).json ()
108100
109101 self .logger .debug (response )
110102
@@ -121,18 +113,20 @@ def call(self, url, params):
121113 if not self .access_token :
122114 self .access_token = self .get_access_token ()
123115
124- request = urllib2 .Request (
125- "%s?%s" % (url , urllib .urlencode (params )),
116+ resp = requests .get (
117+ "%s" % url ,
118+ params = params ,
126119 headers = {'Authorization' : 'Bearer %s' % self .access_token }
127120 )
128- response = urllib2 .urlopen (request ).read ()
129- rv = json .loads (response .decode ("UTF-8-sig" ))
121+ resp .encoding = 'UTF-8-sig'
122+ rv = resp .json ()
123+ #rv = json.loads(response.decode("UTF-8-sig"))
130124
131- if isinstance (rv , basestring ) and \
125+ if isinstance (rv , str ) and \
132126 rv .startswith ("ArgumentOutOfRangeException" ):
133127 raise ArgumentOutOfRangeException (rv )
134128
135- if isinstance (rv , basestring ) and \
129+ if isinstance (rv , str ) and \
136130 rv .startswith ("TranslateApiException" ):
137131 raise TranslateApiException (rv )
138132
@@ -170,23 +164,23 @@ def translate_array(self, texts, to_lang, from_lang=None, **options):
170164 """Translates an array of text strings from one language to another.
171165
172166 :param texts: A list containing texts for translation.
173- :param to_lang: A string representing the language code to
167+ :param to_lang: A string representing the language code to
174168 translate the text into.
175- :param from_lang: A string representing the language code of the
176- translation text. If left None the response will include the
169+ :param from_lang: A string representing the language code of the
170+ translation text. If left None the response will include the
177171 result of language auto-detection. (Default: None)
178- :param options: A TranslateOptions element containing the values below.
172+ :param options: A TranslateOptions element containing the values below.
179173 They are all optional and default to the most common settings.
180174
181- Category: A string containing the category (domain) of the
175+ Category: A string containing the category (domain) of the
182176 translation. Defaults to "general".
183- ContentType: The format of the text being translated. The
184- supported formats are "text/plain" and "text/html". Any
177+ ContentType: The format of the text being translated. The
178+ supported formats are "text/plain" and "text/html". Any
185179 HTML needs to be well-formed.
186- Uri: A string containing the content location of this
180+ Uri: A string containing the content location of this
187181 translation.
188182 User: A string used to track the originator of the submission.
189- State: User state to help correlate request and response. The
183+ State: User state to help correlate request and response. The
190184 same contents will be returned in the response.
191185 """
192186 options = {
0 commit comments