1414
1515try :
1616 import simplejson as json
17- from simplejson import JSONDecodeError
1817except 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
2721import warnings
2822import 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
0 commit comments