3
3
"""
4
4
Wordpress API Class
5
5
"""
6
-
7
6
__title__ = "wordpress-api"
8
7
9
8
# from requests import request
14
13
from wordpress .helpers import StrUtils , UrlUtils
15
14
from wordpress .transport import API_Requests_Wrapper
16
15
16
+ try :
17
+ UNICODE_EXISTS = bool (type (unicode ))
18
+ except NameError :
19
+ unicode = lambda s : str (s )
20
+
17
21
18
22
class API (object ):
19
23
""" API Class """
@@ -36,7 +40,8 @@ def __init__(self, url, consumer_key, consumer_secret, **kwargs):
36
40
auth_class = OAuth_3Leg
37
41
38
42
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" )
40
45
41
46
self .auth = auth_class (** auth_kwargs )
42
47
@@ -102,17 +107,17 @@ def request_post_mortem(self, response=None):
102
107
103
108
if 'code' in response_json or 'message' in response_json :
104
109
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' ]
106
111
if key in response_json
107
112
])
108
113
109
114
if 'code' == 'rest_user_invalid_email' :
110
115
remedy = "Try checking the email %s doesn't already exist" % \
111
- request_body .get ('email' )
116
+ request_body .get ('email' )
112
117
113
118
elif 'code' == 'json_oauth1_consumer_mismatch' :
114
119
remedy = "Try deleting the cached credentials at %s" % \
115
- self .auth .creds_store
120
+ self .auth .creds_store
116
121
117
122
elif 'code' == 'woocommerce_rest_cannot_view' :
118
123
if not self .auth .query_string_auth :
@@ -145,12 +150,13 @@ def request_post_mortem(self, response=None):
145
150
header_api_url = StrUtils .eviscerate (header_api_url , '/' )
146
151
147
152
if header_api_url and requester_api_url \
148
- and header_api_url != requester_api_url :
153
+ and header_api_url != requester_api_url :
149
154
reason = "hostname mismatch. %s != %s" % (
150
155
header_api_url , requester_api_url
151
156
)
152
157
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 )
154
160
header_url = StrUtils .eviscerate (header_url , '/' )
155
161
remedy = "try changing url to %s" % header_url
156
162
@@ -161,10 +167,12 @@ def request_post_mortem(self, response=None):
161
167
str (response_headers ),
162
168
str (request_body )[:1000 ]
163
169
)
170
+
164
171
if reason :
165
- msg += "\n Because of %s" % reason
172
+ msg += "\n Because of %s" % ( str ( reason . encode ( 'utf8' )))
166
173
if remedy :
167
- msg += "\n %s" % remedy
174
+ msg += "\n %s" % (str (remedy ))
175
+
168
176
raise UserWarning (msg )
169
177
170
178
def __request (self , method , endpoint , data , ** kwargs ):
@@ -174,7 +182,8 @@ def __request(self, method, endpoint, data, **kwargs):
174
182
endpoint_url = self .auth .get_auth_url (endpoint_url , method , ** kwargs )
175
183
auth = self .auth .get_auth ()
176
184
177
- content_type = kwargs .get ('headers' , {}).get ('content-type' , 'application/json' )
185
+ content_type = kwargs .get ('headers' , {}).get (
186
+ 'content-type' , 'application/json' )
178
187
179
188
if data is not None and content_type .startswith ('application/json' ):
180
189
data = jsonencode (data , ensure_ascii = False ).encode ('utf-8' )
0 commit comments