Skip to content

Commit 794a992

Browse files
committed
Add order, cart, and user endpoints
1 parent 5c21997 commit 794a992

File tree

1 file changed

+72
-10
lines changed

1 file changed

+72
-10
lines changed

client.py

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def index(self):
3131
try:
3232
return r.json()
3333
except Exception:
34-
return {'code': r.status_code, 'text': r.text}
34+
return self._error(r)
3535

3636
def repeat(self, data):
3737
"""
@@ -44,7 +44,7 @@ def repeat(self, data):
4444
try:
4545
return r.json()
4646
except Exception:
47-
return {'code': r.status_code, 'text': r.text}
47+
return self._error(r)
4848

4949
def profile(self):
5050
"""
@@ -55,11 +55,14 @@ def profile(self):
5555
try:
5656
return r.json()
5757
except Exception:
58-
return {'code': r.status_code, 'text': r.text}
58+
return self._error(r)
5959

6060
## helpers
6161
## ------------
6262

63+
def _error(self, r):
64+
return {'code': r.status_code, 'text': r.text}
65+
6366
def _get_server(self):
6467
if self.port:
6568
return str(self.protocol) + '://' + str(self.server) + ':' + str(self.port) + '/'
@@ -161,12 +164,12 @@ def book(self, hash_id=None, isbn=None):
161164
elif isbn:
162165
r = requests.get(url=self._get_version_endpoint('book', 'isbn', isbn), headers=self._get_signed_headers())
163166
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.")
165168

166169
try:
167170
return r.json()
168171
except Exception:
169-
return {'code': r.status_code, 'text': r.text}
172+
return self._error(r)
170173

171174
def book_search(self, isbn=None, title=None):
172175
"""
@@ -182,7 +185,7 @@ def book_search(self, isbn=None, title=None):
182185
try:
183186
return r.json()
184187
except Exception:
185-
return {'code': r.status_code, 'text': r.text}
188+
return self._error(r)
186189

187190
## User endpoints
188191
## ------------
@@ -204,9 +207,29 @@ def invite_user(self, email=None, first_name=None, last_name=None, label=None):
204207
try:
205208
return r.json()
206209
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)
208231

209-
## Order / Store endpoints
232+
## Order endpoints
210233
## ------------
211234

212235
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
231254
try:
232255
return r.json()
233256
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)
235297

236298
## Misc / help endpoints
237299
## ------------
@@ -246,7 +308,7 @@ def describe(self):
246308
try:
247309
return r.json()
248310
except Exception:
249-
return {'code': r.status_code, 'text': r.text}
311+
return self._error(r)
250312

251313

252314
## #################

0 commit comments

Comments
 (0)