-
Notifications
You must be signed in to change notification settings - Fork 42
/
rest_api.py
executable file
·86 lines (69 loc) · 2.46 KB
/
rest_api.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
#!/usr/bin/env micropython
"""
MIT license
(C) Konstantin Belyalov 2017-2018
"""
import tinyweb
# Init our customers DB with some fake values
db = {'1': {'firstname': 'Alex', 'lastname': 'River'},
'2': {'firstname': 'Lannie', 'lastname': 'Fox'}}
next_id = 3
# If you're familiar with FLaskRestful - you're almost all set! :)
# Tinyweb have only basic functionality comparing to Flask due to
# environment where it intended to run on.
class CustomersList():
def get(self, data):
"""Return list of all customers"""
return db
def post(self, data):
"""Add customer"""
global next_id
db[str(next_id)] = data
next_id += 1
# Return message AND set HTTP response code to "201 Created"
return {'message': 'created'}, 201
# Simple helper to return message and error code
def not_found():
# Return message and HTTP response "404 Not Found"
return {'message': 'no such customer'}, 404
# Detailed information about given customer
class Customer():
def not_exists(self):
return {'message': 'no such customer'}, 404
def get(self, data, user_id):
"""Get detailed information about given customer"""
if user_id not in db:
return not_found()
return db[user_id]
def put(self, data, user_id):
"""Update given customer"""
if user_id not in db:
return not_found()
db[user_id] = data
return {'message': 'updated'}
def delete(self, data, user_id):
"""Delete customer"""
if user_id not in db:
return not_found()
del db[user_id]
return {'message': 'successfully deleted'}
def run():
# Create web server application
app = tinyweb.webserver()
# Add our resources
app.add_resource(CustomersList, '/customers')
app.add_resource(Customer, '/customers/<user_id>')
app.run(host='0.0.0.0', port=8081)
if __name__ == '__main__':
run()
# To test your server run in terminal:
# - Get all customers:
# curl http://localhost:8081/customers
# - Get detailed information about particular customer:
# curl http://localhost:8081/customers/1
# - Add customer:
# curl http://localhost:8081/customers -X POST -d "firstname=Maggie&lastname=Stone"
# - Update customer:
# curl http://localhost:8081/customers/2 -X PUT -d "firstname=Margo"
# - Delete customer:
# curl http://localhost:8081/customers/1 -X DELETE