-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshipping_service.py
More file actions
37 lines (28 loc) · 1.5 KB
/
shipping_service.py
File metadata and controls
37 lines (28 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import uuid
from datetime import datetime
from appkernel import date_now_generator, NotEmpty, Role, create_custom_error
from appkernel import Model, Property
from appkernel.model import resource
from checkout.core.models import Address
from checkout.core.util import get_client_proxy
client = inventory_client = get_client_proxy('inventory')
class Shipping(Model):
reservation_id = Property(str, required=True, validators=NotEmpty)
order_date = Property(datetime, required=True, generator=date_now_generator)
delivery_address = Property(Address, required=True)
class ShippingService(object):
def ship(self, address, products):
print(f'request shipping to: {address} | products: {products}')
return str(uuid.uuid4())
@resource(method='POST', path='./', require=[Role('user')])
def shipping_request(self, request: Shipping):
code, reservation = client.wrap(f'/reservations/{request.reservation_id}/commit').patch()
if code == 200:
tracking_id = self.ship(request.delivery_address, reservation.products)
code, reservation = client.wrap(f'/reservations/{request.reservation_id}/execute').patch(
{'tracking_id': tracking_id})
return reservation
else:
msg = reservation.get('message') if hasattr(reservation,
'message') else 'Error while calling reservation service.'
return create_custom_error(code, msg, upstream_service='ShippingService')