From 7ae997c3521d7fad8adc841d74808cc40d5b5e1d Mon Sep 17 00:00:00 2001 From: Douglas Thor Date: Wed, 27 Mar 2019 11:16:50 -0700 Subject: [PATCH] Moved the /data routes to a flask MethodView in routes.py --- CHANGELOG.md | 1 + src/trendlines/routes.py | 78 ++++++++++++++++++++-------------------- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d285696..2989964 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ + Replaced all instances of port 9999 with port 2003. (#104) + The `missing_required_key` error response now accepts lists or tuples. (#137) ++ The `/data` API routes have been moved to a MethodView class. (#138) ## 0.5.0 (2019-02-28) diff --git a/src/trendlines/routes.py b/src/trendlines/routes.py index 51b04a4..1924756 100644 --- a/src/trendlines/routes.py +++ b/src/trendlines/routes.py @@ -85,55 +85,57 @@ def plot(metric=None): return render_template('trendlines/plot.html', name=metric, data=data) -@api.route("/api/v1/data", methods=['POST']) -def post_datapoint(): - """ - Add a new value and possibly a metric if needed. +@api.route("/api/v1/data") +class Data(MethodView): + def post(self): + """ + Add a new value and possibly a metric if needed. - Expected JSON payload has the following key/value pairs:: + Expected JSON payload has the following key/value pairs:: - metric: string - value: numeric - time: integer or missing - """ - data = request.get_json() - logger.debug("Received POST /api/v1/data: {}".format(data)) + metric: string + value: numeric + time: integer or missing + """ + data = request.get_json() + logger.debug("Received POST /api/v1/data: {}".format(data)) - try: - metric = data['metric'] - value = data['value'] - except KeyError: - logger.warning("Missing JSON keys 'metric' or 'value'.") - return "Missing required key. Required keys are:", 400 + try: + metric = data['metric'] + value = data['value'] + except KeyError: + logger.warning("Missing JSON keys 'metric' or 'value'.") + return "Missing required key. Required keys are:", 400 - time = data.get('time', None) + time = data.get('time', None) - db.add_metric(metric) - new = db.insert_datapoint(metric, value, time) + db.add_metric(metric) + new = db.insert_datapoint(metric, value, time) - msg = "Added DataPoint to Metric {}\n".format(new.metric) - logger.info("Added value %s to metric '%s'" % (value, metric)) - return msg, 201 + msg = "Added DataPoint to Metric {}\n".format(new.metric) + logger.info("Added value %s to metric '%s'" % (value, metric)) + return msg, 201 -@api.route("/api/v1/data/", methods=["GET"]) -def get_data_as_json(metric_name): - """ - Return data for a given metric as JSON. - """ - logger.debug("API: get '%s'" % metric_name) - try: - raw_data = db.get_data(metric_name) - units = db.get_units(metric_name) - except DoesNotExist: - return ErrorResponse.metric_not_found(metric_name) +@api.route("/api/v1/data/") +class DataByName(MethodView): + def get(self, metric_name): + """ + Return data for a given metric as JSON. + """ + logger.debug("API: get '%s'" % metric_name) + try: + raw_data = db.get_data(metric_name) + units = db.get_units(metric_name) + except DoesNotExist: + return ErrorResponse.metric_not_found(metric_name) - if len(raw_data) == 0: - return ErrorResponse.metric_has_no_data(metric_name) + if len(raw_data) == 0: + return ErrorResponse.metric_has_no_data(metric_name) - data = utils.format_data(raw_data, units) + data = utils.format_data(raw_data, units) - return jsonify(data) + return jsonify(data) @api_datapoint.route("/api/v1/datapoint")