Mercurial > personal > weather-server
diff weather_server/server.py @ 11:52ef21607b31
server: Create endpoint to get some recent readings.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 06 Oct 2019 13:11:11 -0400 |
parents | d54155a199d8 |
children | 9a609bcf0809 |
line wrap: on
line diff
--- a/weather_server/server.py Sun Sep 29 20:42:11 2019 -0400 +++ b/weather_server/server.py Sun Oct 06 13:11:11 2019 -0400 @@ -1,3 +1,4 @@ +import datetime import hmac import sys @@ -48,7 +49,7 @@ return flask.jsonify({'status': 'OK'}) @app.route('/<location>') - def show(location): + def show(location: str): try: loc, logger = locs.get(location) except KeyError: @@ -67,6 +68,30 @@ last_reading=last_reading, date=date) + @app.route('/<location>/recent') + def recent(location: str): + try: + loc, logger = locs.get(location) + except KeyError: + flask.abort(404) + req = flask.request + + try: + seconds = int(req.args['seconds']) + except (KeyError, ValueError): + flask.abort(400) + + start = common.utc_now() - datetime.timedelta(seconds=seconds) + + readings = [ + r.as_dict() for r in logger.data + if start < r.sample_time + ] + resp = flask.Response() + resp.content_type = 'application/json' + resp.data = common.json_dumps(readings) + return resp + return app