-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
136 lines (88 loc) · 3.59 KB
/
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# -*- coding: utf-8 -*-
from flask import Flask, jsonify, url_for, redirect, request
from flask_restful import Api, Resource, reqparse
from flask_sqlalchemy import SQLAlchemy
from settings import DB_URI
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = False
db = SQLAlchemy(app)
""" Modelo de tabla de BD
"""
class ModelSensor(db.Model):
__tablename__ = "sensores"
""" Representación de los campos
"""
id = db.Column(db.Integer, primary_key=True)
descripcion = db.Column(db.String(45), unique=False, nullable=True)
# tipo_sens = Column(Enum(TipoSensores))
valor = db.Column(db.Integer, unique=False, nullable=True)
def as_dict(self):
return {"id": self.id, "descripcion": self.descripcion, "valor": self.valor}
def __repr__(self):
return "<Sensor : {}, {}, {}>".format(self.id, self.descripcion, self.valor)
""" Definition of resource classes: """
class Index(Resource):
def get(self):
""" Redirecciona al repositorio original del proyecto """
return redirect("https://github.com/Kraloz/api-flask", code=302)
class SensorList(Resource):
def get(self):
""" Returns ALL the sensors from the db """
lista_sensores = []
# SELECT * FROM SENSORES
for r in ModelSensor.query.all():
lista_sensores.append(r.as_dict())
return jsonify({"sensores": lista_sensores}),200
class Sensor(Resource):
# FUNCIONA
def get(self, id=None):
""" Returns the requested sensor """
# SELECT * FROM SENSORES WHERE ID = "id";
sensor = ModelSensor.query.get_or_404(id)
return jsonify({"sensor": sensor.as_dict()}),200
def post(self):
# OK
json_data = request.get_json()
# INSERT INTO `sensores` (descripcion, tipo_sens, valor) VALUES (...);
sens = ModelSensor(descripcion=json_data["descripcion"], valor=json_data["valor"])
db.session.add(sens)
db.session.commit()
# Respuesta:
response = jsonify()
response.status_code = 201
# Location header:
response.headers['Location'] = "/sensores/{}".format(sens.id)
response.autocorrect_location_header = False
return response
# WORKING
def put(self, id=None):
""" Updates the "valor" field of the sensor requested """
json_data = request.get_json()
sensor = ModelSensor.query.get_or_404(id)
sensor.valor = json_data["valor"]
db.session.commit()
response = jsonify()
response.status_code = 204
return response
# WORKING
def delete(self, id=None):
""" Deletes the sensor requested from the db """
sensor = ModelSensor.query.get_or_404(id)
# DELETE * FROM SENSORES WHERE ID = ID;
db.session.delete(sensor)
db.session.commit()
response = jsonify()
response.status_code = 204
return response
"""Definition of resource routes: """
api = Api(app)
api.add_resource(Index, "/api/", endpoint="index")
api.add_resource(SensorList, "/api/sensores/", endpoint="sensores")
api.add_resource(Sensor, "/api/sensores/<int:id>", endpoint="sensor")
api.add_resource(Sensor, "/api/sensores/", endpoint="sensorPost")
api.add_resource(Sensor, "/api/sensores/<int:id>/valor", endpoint="sensorUpdate")
api.add_resource(Sensor, "/api/sensores/<int:id>", endpoint="sensorDelete")
if __name__ == "__main__":
app.run(host="0.0.0.0",debug=True)