Skip to content

Commit 77098c5

Browse files
committed
Update __init__.py
1 parent 1ffb490 commit 77098c5

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

oauth2/__init__.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def escape(s):
181181
try:
182182
return urllib.quote(s.encode('utf-8'), safe='~')
183183
except AttributeError:
184-
return urllib.parse.quote(s.encode('utf-8'), safe='~')
184+
return urlparse.quote(s.encode('utf-8'), safe='~')
185185

186186
def generate_timestamp():
187187
"""Get seconds since epoch (UTC)."""
@@ -231,8 +231,10 @@ def __init__(self, key, secret):
231231
def __str__(self):
232232
data = {'oauth_consumer_key': self.key,
233233
'oauth_consumer_secret': self.secret}
234-
235-
return urllib.urlencode(data)
234+
try:
235+
return urllib.urlencode(data)
236+
except AttributeError:
237+
return urlparse.urlencode(data)
236238

237239

238240
class Token(object):
@@ -300,7 +302,10 @@ def to_string(self):
300302

301303
if self.callback_confirmed is not None:
302304
data['oauth_callback_confirmed'] = self.callback_confirmed
303-
return urllib.urlencode(data)
305+
try:
306+
return urllib.urlencode(data)
307+
except AttributeError:
308+
return urlparse.urlencode(data)
304309

305310
@staticmethod
306311
def from_string(s):
@@ -434,7 +439,10 @@ def to_postdata(self):
434439
# tell urlencode to deal with sequence values and map them correctly
435440
# to resulting querystring. for example self["k"] = ["v1", "v2"] will
436441
# result in 'k=v1&k=v2' and not k=%5B%27v1%27%2C+%27v2%27%5D
437-
return urllib.urlencode(d, True).replace('+', '%20')
442+
try:
443+
return urllib.urlencode(d, True).replace('+', '%20')
444+
except AttributeError:
445+
return urlparse.urlencode(d, True).replace('+', '%20')
438446

439447
def to_url(self):
440448
"""Serialize as a URL for a GET request."""
@@ -462,9 +470,14 @@ def to_url(self):
462470
params = base_url[3]
463471
fragment = base_url[5]
464472

465-
url = (scheme, netloc, path, params,
466-
urllib.urlencode(query, True), fragment)
467-
return urlparse.urlunparse(url)
473+
try:
474+
url = (scheme, netloc, path, params,
475+
urllib.urlencode(query, True), fragment)
476+
return urllib.urlunparse(url)
477+
except AttributeError:
478+
url = (scheme, netloc, path, params,
479+
urlparse.urlencode(query, True), fragment)
480+
return urlparse.urlunparse(url)
468481

469482
def get_parameter(self, parameter):
470483
ret = self.get(parameter)
@@ -512,7 +525,10 @@ def get_normalized_parameters(self):
512525
items.extend(url_items)
513526

514527
items.sort()
515-
encoded_str = urllib.urlencode(items)
528+
try:
529+
encoded_str = urllib.urlencode(items)
530+
except AttributeError:
531+
encoded_str = urlparse.urlencode(items)
516532
# Encode signature parameters per Oauth Core 1.0 protocol
517533
# spec draft 7, section 3.6
518534
# (http://tools.ietf.org/html/draft-hammer-oauth-07#section-3.6)
@@ -641,7 +657,7 @@ def _split_header(header):
641657
try:
642658
params[param_parts[0]] = urllib.unquote(param_parts[1].strip('\"'))
643659
except AttributeError:
644-
params[param_parts[0]] = urllib.parse.unquote(param_parts[1].strip('\"'))
660+
params[param_parts[0]] = urlparse.unquote(param_parts[1].strip('\"'))
645661
return params
646662

647663
@staticmethod
@@ -652,7 +668,7 @@ def _split_url_string(param_str):
652668
try:
653669
parameters[k] = urllib.unquote(v[0])
654670
except AttributeError:
655-
parameters[k] = urllib.parse.unquote(v[0])
671+
parameters[k] = urlparse.unquote(v[0])
656672
return parameters
657673

658674

@@ -704,13 +720,19 @@ def request(self, uri, method="GET", body='', headers=None,
704720
parameters=parameters, body=body, is_form_encoded=is_form_encoded)
705721

706722
req.sign_request(self.method, self.consumer, self.token)
707-
708-
schema, rest = urllib.splittype(uri)
723+
724+
try:
725+
schema, rest = urllib.splittype(uri)
726+
except AttributeError:
727+
schema, rest = urlparse.splittype(uri)
709728
if rest.startswith('//'):
710729
hierpart = '//'
711730
else:
712731
hierpart = ''
713-
host, rest = urllib.splithost(rest)
732+
try:
733+
host, rest = urllib.splithost(rest)
734+
except AttributeError:
735+
host, rest = urlparse.splithost(rest)
714736

715737
realm = schema + ':' + hierpart + host
716738

0 commit comments

Comments
 (0)