@@ -31,7 +31,7 @@ def index(self):
31
31
try :
32
32
return r .json ()
33
33
except Exception :
34
- return { 'code' : r . status_code , 'text' : r . text }
34
+ return self . _error ( r )
35
35
36
36
def repeat (self , data ):
37
37
"""
@@ -44,7 +44,7 @@ def repeat(self, data):
44
44
try :
45
45
return r .json ()
46
46
except Exception :
47
- return { 'code' : r . status_code , 'text' : r . text }
47
+ return self . _error ( r )
48
48
49
49
def profile (self ):
50
50
"""
@@ -55,11 +55,14 @@ def profile(self):
55
55
try :
56
56
return r .json ()
57
57
except Exception :
58
- return { 'code' : r . status_code , 'text' : r . text }
58
+ return self . _error ( r )
59
59
60
60
## helpers
61
61
## ------------
62
62
63
+ def _error (self , r ):
64
+ return {'code' : r .status_code , 'text' : r .text }
65
+
63
66
def _get_server (self ):
64
67
if self .port :
65
68
return str (self .protocol ) + '://' + str (self .server ) + ':' + str (self .port ) + '/'
@@ -161,12 +164,12 @@ def book(self, hash_id=None, isbn=None):
161
164
elif isbn :
162
165
r = requests .get (url = self ._get_version_endpoint ('book' , 'isbn' , isbn ), headers = self ._get_signed_headers ())
163
166
else :
164
- raise Exception ("Please provide the book hash_id or ISBN field." )
167
+ raise ClientException ("Please provide the book hash_id or ISBN field." )
165
168
166
169
try :
167
170
return r .json ()
168
171
except Exception :
169
- return { 'code' : r . status_code , 'text' : r . text }
172
+ return self . _error ( r )
170
173
171
174
def book_search (self , isbn = None , title = None ):
172
175
"""
@@ -182,7 +185,7 @@ def book_search(self, isbn=None, title=None):
182
185
try :
183
186
return r .json ()
184
187
except Exception :
185
- return { 'code' : r . status_code , 'text' : r . text }
188
+ return self . _error ( r )
186
189
187
190
## User endpoints
188
191
## ------------
@@ -204,9 +207,29 @@ def invite_user(self, email=None, first_name=None, last_name=None, label=None):
204
207
try :
205
208
return r .json ()
206
209
except Exception :
207
- return {'code' : r .status_code , 'text' : r .text }
210
+ return self ._error (r )
211
+
212
+ def user (self , username = None , email = None ):
213
+ """
214
+ User endpoint
215
+ GET /v1/user/
216
+
217
+ args: username (str), email (str)
218
+ """
219
+
220
+ if username :
221
+ r = requests .get (url = self ._get_version_endpoint ('user' , username ), headers = self ._get_signed_headers ())
222
+ elif email :
223
+ r = requests .get (url = self ._get_version_endpoint ('user' , 'email' , email ), headers = self ._get_signed_headers ())
224
+ else :
225
+ raise ClientException ("Please provide the username or email address." )
226
+
227
+ try :
228
+ return r .json ()
229
+ except Exception :
230
+ return self ._error (r )
208
231
209
- ## Order / Store endpoints
232
+ ## Order endpoints
210
233
## ------------
211
234
212
235
def create_order (self , username , digital_pricing = [], print_pricing = [], combo_pricing = [], billing_address = {},
@@ -231,7 +254,46 @@ def create_order(self, username, digital_pricing=[], print_pricing=[], combo_pri
231
254
try :
232
255
return r .json ()
233
256
except Exception :
234
- return {'code' : r .status_code , 'text' : r .text }
257
+ return self ._error (r )
258
+
259
+ def order (self , id ):
260
+ """
261
+ Order endpoint
262
+ GET /v1/order/
263
+
264
+ args: id (int)
265
+ """
266
+ r = requests .get (url = self ._get_version_endpoint ('order' , id ), headers = self ._get_signed_headers ())
267
+
268
+ try :
269
+ return r .json ()
270
+ except Exception :
271
+ return self ._error (r )
272
+
273
+ ## Store endpoints
274
+ ## ------------
275
+
276
+ def create_cart (self , username = None , digital_pricing = [], print_pricing = [], combo_pricing = [], label = None ):
277
+ """
278
+ Cart creation endpoint
279
+ POST /v1/cart/
280
+
281
+ args: username (string) <opt>, digital_pricing (list <pricing_id>), print_pricing (list <print_option_id>) <opt>,
282
+ combo_pricing (list <print_option_id>) <opt>, label (string) <opt>
283
+
284
+ returns: token for the new cart.
285
+ user can be sent to redshelf.com/cart/?t=<token> to assign the cart or if username is provided the
286
+ cart will be assigned automatically
287
+ """
288
+
289
+ payload = {'username' : username , 'digital_pricing' : digital_pricing }
290
+ request_data = self ._get_request_data (payload )
291
+ r = requests .post (url = self ._get_version_endpoint ('cart' ), data = request_data , headers = self ._get_signed_headers (payload ))
292
+
293
+ try :
294
+ return r .json ()
295
+ except Exception :
296
+ return self ._error (r )
235
297
236
298
## Misc / help endpoints
237
299
## ------------
@@ -246,7 +308,7 @@ def describe(self):
246
308
try :
247
309
return r .json ()
248
310
except Exception :
249
- return { 'code' : r . status_code , 'text' : r . text }
311
+ return self . _error ( r )
250
312
251
313
252
314
## #################
0 commit comments