BigW Consortium Gitlab

Commit d886ba25 by Forest Godfrey

Various fixups to make this work with python3

parent 4eb76b60
from __future__ import with_statement
from contextlib import closing
import simplejson as json
import urllib
from urllib.parse import urlencode
try:
from urllib import urlencode
from urllib import urlopen
except ImportError:
from urllib.parse import urlencode
from urllib.request import urlopen
import os
import re
import tempfile
import time
from ._multipartformdataencode import encode_multipart_formdata
from etsy._multipartformdataencode import encode_multipart_formdata
from .exceptions import EtsyConcurrencyError, EtsyAPILimitError
......@@ -60,8 +66,6 @@ class TypeChecker(object):
return True, value
def check_int(self, value):
if isinstance(value, long): # noqa
return True, value
return isinstance(value, int), value
def check_float(self, value):
......@@ -70,7 +74,7 @@ class TypeChecker(object):
return isinstance(value, float), value
def check_string(self, value):
return isinstance(value, basestring), value # noqa
return isinstance(value, str), value # noqa
class APIMethod(object):
......@@ -279,8 +283,8 @@ class API(object):
def _get_url(self, url, http_method, content_type, body):
self.log("API._get_url: url = %r" % url)
with closing(urllib.urlopen(url)) as f:
return f.read().decode('utf-8')
with closing(urlopen(url)) as f:
return f.read()
def _get(self, http_method, url, **kwargs):
if self.etsy_oauth_client is None:
......@@ -302,14 +306,10 @@ class API(object):
fields.append((name, str(value)))
content_type, body = encode_multipart_formdata(fields, files)
if url.find("inventory") > 0:
self.log("Body = " + str(body))
else:
url = '%s%s' % (self.api_url, url)
body = urlencode(kwargs)
content_type = 'application/x-www-form-urlencoded'
if url.find("inventory") > 0:
self.log("Body = " + str(body))
self.last_url = url
data = self._get_url(url, http_method, content_type, body)
......@@ -320,7 +320,6 @@ class API(object):
try:
self.data = self.decode(data)
except json.JSONDecodeError:
print(data)
if 'being edited by another process' in data:
raise EtsyConcurrencyError(data)
if 'exceeded your quota' in data:
......
......@@ -9,14 +9,18 @@ From http://code.activestate.com/recipes/146306/ (PSF License)
class byteEncoder:
def __init__(self, sep):
self.b = None
self.sep = bytes(sep)
self.sep = bytes(sep, "utf-8")
def append(self, val):
def append(self, v):
if isinstance(v, str):
val = bytes(v, "utf-8")
else:
val = bytes(v)
if self.b is None:
self.b = bytes(val)
self.b = val
return
self.b = self.b + self.sep + bytes(val)
self.b = self.b + self.sep + val
def value(self):
return self.b
......
......@@ -19,8 +19,12 @@ class EtsyV2(API):
def _get_url(self, url, http_method, content_type, body):
print("Getting URL " + str(url))
if self.etsy_oauth_client is not None:
if isinstance(body, str):
b = bytes(body, "utf-8")
else:
b = bytes(body)
return self.etsy_oauth_client.do_oauth_request(url,
http_method,
content_type,
bytes(body, "utf-8"))
b)
return API._get_url(self, url, http_method, content_type, body)
import oauth2 as oauth
import urllib
from cgi import parse_qsl
from urllib.parse import parse_qsl
from etsy.etsy_env import EtsyEnvProduction
EtsyOAuthToken = oauth.Token
......@@ -21,9 +21,10 @@ class EtsyOAuthClient(oauth.Client):
def get_request_token(self, **kwargs):
request_token_url = '%s?%s' % (
self.request_token_url, urllib.urlencode(kwargs))
self.request_token_url, urllib.parse.urlencode(kwargs))
resp, content = self.request(request_token_url, 'GET')
return self._get_token(content)
print("get_request_token: " + str(content))
return self._get_token(content.decode('utf-8'))
def get_signin_url(self, **kwargs):
self.token = self.get_request_token(**kwargs)
......@@ -32,12 +33,12 @@ class EtsyOAuthClient(oauth.Client):
return None
return self.signin_url + '?' + \
urllib.urlencode({'oauth_token': self.token.key})
urllib.parse.urlencode({'oauth_token': self.token.key})
def get_access_token(self, oauth_verifier):
self.token.set_verifier(oauth_verifier)
resp, content = self.request(self.access_token_url, 'GET')
return self._get_token(content)
return self._get_token(content.decode('utf-8'))
def set_oauth_verifier(self, oauth_verifier):
self.token = self.get_access_token(oauth_verifier)
......@@ -48,7 +49,7 @@ class EtsyOAuthClient(oauth.Client):
if self.logger:
self.logger.debug('do_oauth_request: content = %r' % content)
return content
return content.decode('utf-8')
def _get_token(self, content):
d = dict(parse_qsl(content))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment