-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
90 lines (68 loc) · 2.25 KB
/
api.py
File metadata and controls
90 lines (68 loc) · 2.25 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
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
from flask import Flask
from flask_restful import reqparse, abort, Api, Resource
app = Flask(__name__)
api = Api(app)
DATOS = {
'coche1': {
'speed': 70,
'engine': 'stop',
'sensor':'disable',
'status': 'Stopped',
},
'coche2': {
'speed': 30,
'engine': 'stop',
'sensor':'disable',
'status': 'Stopped',
},
'coche3': {
'speed': 10,
'engine': 'stop',
'sensor':'disable',
'status': 'Stopped',
},
'coche4': {
'speed': 0,
'engine': 'forward',
'sensor':'disable',
'status': 'Stopped',
}
}
def abort_if_todo_doesnt_exist(coche_id):
if coche_id not in DATOS:
abort(404, message="Conohe {} doesn't exist".format(coche_id))
parser = reqparse.RequestParser()
parser.add_argument('engine')
parser.add_argument('sensor')
parser.add_argument('speed')
parser.add_argument('status')
class Todo(Resource):
def get(self, coche_id):
abort_if_todo_doesnt_exist(coche_id)
return DATOS[coche_id]
def delete(self, coche_id):
abort_if_todo_doesnt_exist(coche_id)
del DATOS[coche_id]
return 'Realizado con exito.', 204
def put(self, coche_id):
args = parser.parse_args()
DATOS[coche_id] = {'engine': args['engine'],'sensor': args['sensor'],'speed': args['speed'],'status': args['status']}
return 'Ralizado con exito.', 201
# TodoList
# shows a list of all DATOS, and lets you POST to add new tasks
class TodoList(Resource):
def get(self):
return DATOS
def post(self):
args = parser.parse_args()
coche_id = int(max(DATOS.keys()).lstrip('coche')) + 1
coche_id = 'coche%i' % coche_id
DATOS[coche_id] = {'engine': args['engine'],'sensor': args['sensor'],'speed': args['speed'],'status': args['status']}
return DATOS[coche_id], 201
##
## Actually setup the Api resource routing here
##
api.add_resource(TodoList, '/coches')
api.add_resource(Todo, '/coches/<coche_id>')
if __name__ == '__main__':
app.run(debug=True, port=5000)