-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
108 lines (87 loc) · 2.81 KB
/
run.py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import flask
from flask_restplus import Api, Resource, fields
from werkzeug.contrib.fixers import ProxyFix
app = flask.Flask(__name__)
app.wsgi_app = ProxyFix(app.wsgi_app)
api = Api(app, version='1.0', title='Fast-Food-Fast API',
description='A simple Food API',
)
mac = api.namespace('orders', description='Order operations')
order = api.model('Order', {
"id": fields.Integer(readOnly=True),
"name": fields.String(required=True, description='Order name'),
"status": fields.String(required=True, description='The order status')
})
status = api.model('Status', {
"status": fields.String(required=True, description='The order status')
})
class OrderDAO(object):
"""Parent class"""
def __init__(self):
self.count = 0
self.orders = []
# Get order by id
def get(self, id):
"""GET a single order using a single id"""
for order in self.orders:
if order['id'] == id:
return order
api.abort(404, "Order {} doesn't exist".format(id))
# Create an Order
def create(self, data):
"""POST a new order"""
order = data
order['id'] = self.count = self.count + 1
self.orders.append(order)
return order
# Update an order
def update_order(self, id, status):
"""Update the status of an existing order"""
order = self.get(id)
order.update(status)
return order
# Delete an order by id
def delete_order(self, id):
"""Delete a specific order"""
order = self.get(id)
self.orders.remove(order)
DAO = OrderDAO()
# GET all, POST
@api.route('/v1/orders')
class OrderList(Resource):
"""Shows a list of all orders, and lets you POST to add new orders"""
@mac.doc('list_orders')
@mac.marshal_list_with(order)
def get(self):
"""List all orders"""
return DAO.orders
@mac.doc('create_order')
@mac.expect(order)
@mac.marshal_with(order, code=201)
def post(self):
"""Create a new order"""
return DAO.create(api.payload), 201
# GET by id, POST, PUT
@api.route('/v1/orders/<int:id>')
@mac.response(404, 'Order not found')
@mac.param('id', 'The order id')
class Order(Resource):
"""Show a single order item and lets you delete them"""
@mac.doc('get_order')
@mac.marshal_with(order)
def get(self, id):
"""Fetch a given order id"""
return DAO.get(id)
@mac.doc('delete_order')
@mac.response(204, 'Order deleted')
def delete(self, id):
"""Delete an order by it's id"""
DAO.delete_order(id)
return '', 204
@mac.expect(status)
@mac.marshal_with(order)
def put(self, id):
"""Update an order given its id"""
return DAO.update_order(id, api.payload)
if __name__ == '__main__':
app.run(debug=True)