Skip to content

Commit a7713d0

Browse files
committed
Better py3 support 🔥
- Unicode message in post_mortem (py3) - Unicode concatenation in post_mortem (py2)
1 parent 7d85e70 commit a7713d0

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

wordpress/api.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44
Wordpress API Class
55
"""
6-
76
__title__ = "wordpress-api"
87

98
# from requests import request
@@ -14,6 +13,11 @@
1413
from wordpress.helpers import StrUtils, UrlUtils
1514
from wordpress.transport import API_Requests_Wrapper
1615

16+
try:
17+
UNICODE_EXISTS = bool(type(unicode))
18+
except NameError:
19+
unicode = lambda s: str(s)
20+
1721

1822
class API(object):
1923
""" API Class """
@@ -36,7 +40,8 @@ def __init__(self, url, consumer_key, consumer_secret, **kwargs):
3640
auth_class = OAuth_3Leg
3741

3842
if kwargs.get('version', '').startswith('wc') and kwargs.get('oauth1a_3leg'):
39-
self.logger.warn("WooCommerce JSON Api does not seem to support 3leg")
43+
self.logger.warn(
44+
"WooCommerce JSON Api does not seem to support 3leg")
4045

4146
self.auth = auth_class(**auth_kwargs)
4247

@@ -102,17 +107,17 @@ def request_post_mortem(self, response=None):
102107

103108
if 'code' in response_json or 'message' in response_json:
104109
reason = u" - ".join([
105-
unicode(response_json.get(key)) for key in ['code', 'message', 'data'] \
110+
unicode(response_json.get(key)) for key in ['code', 'message', 'data']
106111
if key in response_json
107112
])
108113

109114
if 'code' == 'rest_user_invalid_email':
110115
remedy = "Try checking the email %s doesn't already exist" % \
111-
request_body.get('email')
116+
request_body.get('email')
112117

113118
elif 'code' == 'json_oauth1_consumer_mismatch':
114119
remedy = "Try deleting the cached credentials at %s" % \
115-
self.auth.creds_store
120+
self.auth.creds_store
116121

117122
elif 'code' == 'woocommerce_rest_cannot_view':
118123
if not self.auth.query_string_auth:
@@ -145,12 +150,13 @@ def request_post_mortem(self, response=None):
145150
header_api_url = StrUtils.eviscerate(header_api_url, '/')
146151

147152
if header_api_url and requester_api_url\
148-
and header_api_url != requester_api_url:
153+
and header_api_url != requester_api_url:
149154
reason = "hostname mismatch. %s != %s" % (
150155
header_api_url, requester_api_url
151156
)
152157
header_url = StrUtils.eviscerate(header_api_url, '/')
153-
header_url = StrUtils.eviscerate(header_url, self.requester.api)
158+
header_url = StrUtils.eviscerate(
159+
header_url, self.requester.api)
154160
header_url = StrUtils.eviscerate(header_url, '/')
155161
remedy = "try changing url to %s" % header_url
156162

@@ -161,10 +167,12 @@ def request_post_mortem(self, response=None):
161167
str(response_headers),
162168
str(request_body)[:1000]
163169
)
170+
164171
if reason:
165-
msg += "\nBecause of %s" % reason
172+
msg += "\nBecause of %s" % (str(reason.encode('utf8')))
166173
if remedy:
167-
msg += "\n%s" % remedy
174+
msg += "\n%s" % (str(remedy))
175+
168176
raise UserWarning(msg)
169177

170178
def __request(self, method, endpoint, data, **kwargs):
@@ -174,7 +182,8 @@ def __request(self, method, endpoint, data, **kwargs):
174182
endpoint_url = self.auth.get_auth_url(endpoint_url, method, **kwargs)
175183
auth = self.auth.get_auth()
176184

177-
content_type = kwargs.get('headers', {}).get('content-type', 'application/json')
185+
content_type = kwargs.get('headers', {}).get(
186+
'content-type', 'application/json')
178187

179188
if data is not None and content_type.startswith('application/json'):
180189
data = jsonencode(data, ensure_ascii=False).encode('utf-8')

0 commit comments

Comments
 (0)