8000 Added support for python 3 · torrelasley/wc-api-python@8552142 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8552142

Browse files
Added support for python 3
1 parent b3f4d1d commit 8552142

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

woocommerce/api.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
__author__ = "Claudio Sanches @ WooThemes"
1010
__license__ = "MIT"
1111

12-
import requests
13-
import json
12+
from requests import request
13+
from json import dumps as jsonencode
1414
from woocommerce.oauth import OAuth
1515

1616

@@ -66,9 +66,9 @@ def __request(self, method, endpoint, data):
6666
url = self.__get_oauth_url(url, method)
6767

6868
if data is not None:
69-
data = json.dumps(data, ensure_ascii=False)
69+
data = jsonencode(data, ensure_ascii=False)
7070

71-
return requests.request(
71+
return request(
7272
method=method,
7373
url=url,
7474
verify=self.verify_ssl,

woocommerce/oauth.py

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@
99
__author__ = "Claudio Sanches @ WooThemes"
1010
__license__ = "MIT"
1111

12-
import urlparse
13-
import time
14-
import random
15-
import hmac
16-
import urllib
17-
import hashlib
18-
import collections
12+
from time import time
13+
from random import randint
14+
from hmac import new as HMAC
15+
from hashlib import sha1, sha256
16+
from collections import OrderedDict
17+
from base64 import b64encode
18+
19+
try:
20+
from urllib.parse import urlparse
21+
except ImportError:
22+
from urlparse import urlparse
23+
24+
try:
25+
from urllib import urlencode, quote, unquote
26+
except ImportError:
27+
from urllib.parse import urlencode, quote, unquote
1928

2029

2130
class OAuth(object):
@@ -40,13 +49,16 @@ def get_oauth_url(self):
4049
url = self.url
4150

4251
params["oauth_consumer_key"] = self.consumer_key
43-
params["oauth_timestamp"] < 10000 span class=pl-c1>= int(time.time())
44-
params["oauth_nonce"] = hmac.new(
45-
str(time.time() + random.randint(0, 99999)), "SHA1").hexdigest()
52+
params["oauth_timestamp"] = int(time())
53+
params["oauth_nonce"] = HMAC(
54+
str(time() + randint(0, 99999)).encode(),
55+
"secret".encode(),
56+
sha1
57+
).hexdigest()
4658
params["oauth_signature_method"] = "HMAC-SHA256"
4759
params["oauth_signature"] = self.generate_oauth_signature(params, url)
4860

49-
query_string = urllib.urlencode(params)
61+
query_string = urlencode(params)
5062

5163
return "%s?%s" % (url, query_string)
5264

@@ -55,9 +67,9 @@ def generate_oauth_signature(self, params, url):
5567
if "oauth_signature" in params.keys():
5668
del params["oauth_signature"]
5769

58-
base_request_uri = urllib.quote(url, "")
70+
base_request_uri = quote(url, "")
5971
params = self.normalize_parameters(params)
60-
params = collections.OrderedDict(sorted(params.items()))
72+
params = OrderedDict(sorted(params.items()))
6173
query_params = ["{param_key}%3D{param_value}".format(param_key=key, param_value=value)
6274
for key, value in params.items()]
6375

@@ -68,12 +80,13 @@ def generate_oauth_signature(self, params, url):
6880
if self.version == "v3":
6981
consumer_secret += "&"
7082

71-
hash_signature = hmac.new(
72-
consumer_secret,
73-
str(string_to_sign),
74-
getattr(hashlib, "sha256")).digest()
83+
hash_signature = HMAC(
84+
consumer_secret.encode(),
85+
str(string_to_sign).encode(),
86+
sha256
87+
).digest()
7588

76-
return hash_signature.encode("base64").replace("\n", "")
89+
return b64encode(hash_signature).decode("utf-8").replace("\n", "")
7790

7891
@staticmethod
7992
def normalize_parameters(params):
@@ -82,8 +95,13 @@ def normalize_parameters(params):
8295
normalized_parameters = {}
8396

8497
def get_value_like_as_php(val):
85-
""" Prepare value for urllib.quote """
86-
if isinstance(val, basestring):
98+
""" Prepare value for quote """
99+
try:
100+
base = basestring
101+
except NameError:
102+
base = (str, bytes)
103+
104+
if isinstance(val, base):
87105
return val
88106
elif isinstance(val, bool):
89107
return "1" if val else ""
@@ -96,8 +114,8 @@ def get_value_like_as_php(val):
96114

97115
for key, value in params.items():
98116
value = get_value_like_as_php(value)
99-
key = urllib.quote(urllib.unquote(str(key))).replace("%", "%25")
100-
value = urllib.quote(urllib.unquote(str(value))).replace("%", "%25")
117+
key = quote(unquote(str(key))).replace("%", "%25")
118+
value = quote(unquote(str(value))).replace("%", "%25")
101119
normalized_parameters[key] = value
102120

103121
return normalized_parameters

0 commit comments

Comments
 (0)
0