Skip to content

Commit fe31c47

Browse files
committed
FIX api endpoints
1 parent 4928b41 commit fe31c47

File tree

3 files changed

+82
-61
lines changed

3 files changed

+82
-61
lines changed

prototipo/projeto/ext/api/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
from flask_restful import Api
22

33
from .views import (ApiEstacao, ApiEstacaoId, ApiEstacaoIdSensor,
4-
ApiEstacaoIdSensorId, ApiSensorIdParam,
4+
ApiSensorId, ApiSensorIdParam,
55
ApiSensorIdParamLast)
66

77
api = Api()
88
api.add_resource(ApiEstacao, "/api/v1.1/estacao")
99
api.add_resource(ApiEstacaoId, "/api/v1.1/estacao/<int:estacao_id>")
1010
api.add_resource(ApiEstacaoIdSensor,
1111
"/api/v1.1/estacao/<int:estacao_id>/sensor")
12-
api.add_resource(ApiEstacaoIdSensorId,
13-
"/api/v1.1/estacao/<int:estacao_id>/sensor/<int:sensor_id>")
12+
api.add_resource(ApiSensorId,
13+
"/api/v1.1/sensor/<int:sensor_id>")
1414
api.add_resource(ApiSensorIdParam,
1515
"/api/v1.1/sensor/<int:sensor_id>/<string:param>")
1616
api.add_resource(ApiSensorIdParamLast,

prototipo/projeto/ext/api/views.py

Lines changed: 78 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from flask_jwt import jwt_required
44
from flask_restful import Resource, reqparse
55

6+
from sqlalchemy.exc import IntegrityError
7+
68
from projeto.ext.db import db
79

810
from .models import Estacao, Sensor, Leitura
@@ -40,17 +42,17 @@ def post(self):
4042

4143
db.session.add(estacao)
4244
db.session.commit()
43-
4445
return {"created": estacao.json()}
4546

4647

4748
class ApiEstacaoId(Resource):
4849
def get(self, estacao_id):
49-
estacao = Estacao.query.get(estacao_id)
50-
51-
if estacao:
50+
try:
51+
estacao = Estacao.query.get(estacao_id)
5252
return {"resource": estacao.json()}
53-
return {"error": "Recurso inexistente!"}
53+
54+
except AttributeError:
55+
return {"error": "Recurso inexistente!"}
5456

5557
# @jwt_required()
5658
def put(self, estacao_id):
@@ -60,9 +62,10 @@ def put(self, estacao_id):
6062
parser.add_argument("longitude", type=str)
6163

6264
data = parser.parse_args()
63-
estacao = Estacao.query.get(estacao_id)
6465

65-
if estacao:
66+
try:
67+
estacao = Estacao.query.get(estacao_id)
68+
6669
estacao.local = data["local"] if data["local"] else estacao.local
6770
estacao.latitude = (data["latitude"]
6871
if data["latitude"] else estacao.latitude)
@@ -71,30 +74,33 @@ def put(self, estacao_id):
7174

7275
db.session.add(estacao)
7376
db.session.commit()
74-
7577
return {"updated": estacao.json()}
76-
return {"error": "Recurso inexistente!"}
78+
79+
except (AttributeError, IntegrityError):
80+
return {"error": "Recurso inexistente!"}
7781

7882
# @jwt_required()
7983
def delete(self, estacao_id):
80-
estacao = Estacao.query.get(estacao_id)
81-
82-
if estacao:
84+
try:
85+
estacao = Estacao.query.get(estacao_id)
8386
db.session.delete(estacao)
8487
db.session.commit()
85-
8688
return {"deleted": estacao.json()}
87-
return {"error": "Recurso inexistente!"}
89+
90+
except IntegrityError:
91+
return {"error": "Recurso inexistente!"}
8892

8993

9094
class ApiEstacaoIdSensor(Resource):
9195
def get(self, estacao_id):
92-
estacao = Estacao.query.get(estacao_id)
93-
if estacao:
96+
try:
97+
estacao = Estacao.query.get(estacao_id)
9498
sensores = estacao.sensores
9599
data = [sensor.json() for sensor in sensores]
96100
return {"resources": data}
97-
return {"error": "Recurso inexistente!"}
101+
102+
except AttributeError:
103+
return {"error": "Recurso inexistente!"}
98104

99105
# @jwt_required()
100106
def post(self, estacao_id):
@@ -114,37 +120,42 @@ def post(self, estacao_id):
114120

115121
data = parser.parse_args()
116122

117-
sensor = Sensor(tipo=data["tipo"],
118-
descricao=data["descricao"],
119-
params=data["params"],
120-
estacao_id=estacao_id)
123+
try:
124+
sensor = Sensor(tipo=data["tipo"],
125+
descricao=data["descricao"],
126+
params=data["params"],
127+
estacao_id=estacao_id)
121128

122-
db.session.add(sensor)
123-
db.session.commit()
129+
db.session.add(sensor)
130+
db.session.commit()
131+
132+
return {"created": sensor.json()}
124133

125-
return {"created": sensor.json()}
134+
except IntegrityError:
135+
return {"error": "Recurso inexistente!"}
126136

127137

128-
class ApiEstacaoIdSensorId(Resource):
129-
def get(self, estacao_id, sensor_id):
130-
estacao = Estacao.query.get(estacao_id)
131-
if estacao:
132-
sensor = estacao.get_sensor(sensor_id)
138+
class ApiSensorId(Resource):
139+
def get(self, sensor_id):
140+
try:
141+
sensor = Sensor.query.get(sensor_id)
133142
return {"resource": sensor.json()}
134-
return {"error": "Recurso inexistente!"}
143+
144+
except AttributeError:
145+
return {"error": "Recurso inexistente!"}
135146

136147
# @jwt_required()
137-
def put(self, estacao_id, sensor_id):
148+
def put(self, sensor_id):
138149
parser = reqparse.RequestParser()
139150
parser.add_argument("tipo", type=str)
140151
parser.add_argument("descricao", type=str)
141152
parser.add_argument("params", type=str)
142153

143154
data = parser.parse_args()
144-
estacao = Estacao.query.get(estacao_id)
145155

146-
if estacao:
147-
sensor = estacao.get_sensor(sensor_id)
156+
try:
157+
sensor = Sensor.query.get(sensor_id)
158+
148159
sensor.tipo = data["tipo"] if data["tipo"] else sensor.tipo
149160
sensor.descricao = (data["descricao"]
150161
if data["descricao"] else sensor.descricao)
@@ -155,31 +166,34 @@ def put(self, estacao_id, sensor_id):
155166
db.session.commit()
156167

157168
return {"updated": sensor.json()}
158-
return {"error": "Recurso inexistente!"}
159169

160-
# @jwt_required()
161-
def delete(self, estacao_id, sensor_id):
162-
estacao = Estacao.query.get(estacao_id)
170+
except (AttributeError, IntegrityError):
171+
return {"error": "Recurso inexistente!"}
163172

164-
if estacao:
165-
sensor = estacao.get_sensor(sensor_id)
173+
# @jwt_required()
174+
def delete(self, sensor_id):
175+
try:
176+
sensor = Sensor.query.get(sensor_id)
166177
db.session.delete(sensor)
167178
db.session.commit()
168-
169179
return {"deleted": sensor.json()}
170-
return {"error": "Recurso inexistente!"}
180+
181+
except IntegrityError:
182+
return {"error": "Recurso inexistente!"}
171183

172184

173185
class ApiSensorIdParam(Resource):
174186
def get(self, sensor_id, param):
175-
sensor = Sensor.query.get(sensor_id)
176-
if sensor:
187+
try:
188+
sensor = Sensor.query.get(sensor_id)
177189
leituras = [
178190
leitura.json() for leitura in sensor.leituras
179191
if leitura.param == param
180192
]
181193
return {"resource": leituras}
182-
return {"error": "Recurso inexistente!"}
194+
195+
except AttributeError:
196+
return {"error": "Recurso inexistente!"}
183197

184198
# @jwt_required()
185199
def post(self, sensor_id, param):
@@ -197,25 +211,32 @@ def post(self, sensor_id, param):
197211

198212
datahora = datetime.fromtimestamp(int(data["datahora"]))
199213

200-
leitura = Leitura(datahora=datahora,
201-
valor=data["valor"],
202-
param=param,
203-
sensor_id=sensor_id)
214+
try:
215+
leitura = Leitura(datahora=datahora,
216+
valor=data["valor"],
217+
param=param,
218+
sensor_id=sensor_id)
204219

205-
db.session.add(leitura)
206-
db.session.commit()
220+
db.session.add(leitura)
221+
db.session.commit()
222+
return {"created": leitura.json()}
207223

208-
return {"created": leitura.json()}
224+
except IntegrityError:
225+
return {"error": "Recurso inexistente!"}
209226

210227

211228
class ApiSensorIdParamLast(Resource):
212229
def get(self, sensor_id, param, qty):
213-
sensor = Sensor.query.get(sensor_id)
214-
if sensor:
230+
try:
231+
sensor = Sensor.query.get(sensor_id)
215232
leituras = [
216233
leitura.json() for leitura in sensor.leituras
217234
if leitura.param == param
218235
]
219-
if leituras:
220-
return {"resources": leituras[-qty:]}
221-
return {"error": "Recurso inexistente!"}
236+
llen = len(leituras)
237+
if llen > 0:
238+
return {"resources": leituras[-min([qty, llen]):]}
239+
return {"resources": []}
240+
241+
except AttributeError:
242+
return {"error": "Recurso inexistente!"}

prototipo/projeto/static/visualizacoes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ $(function () {
4242
* selecionado.
4343
*/
4444
async function requestParams(estacao_id, sensor_id) {
45-
const result = await fetch(`${URL_BASE}/estacao/${estacao_id}/sensor/${sensor_id}`);
45+
const result = await fetch(`${URL_BASE}/sensor/${sensor_id}`);
4646

4747
if (result.ok) {
4848
const data = await result.json();

0 commit comments

Comments
 (0)