diff --git a/src/trendlines/routes.py b/src/trendlines/routes.py index 6b18a99..92b9937 100644 --- a/src/trendlines/routes.py +++ b/src/trendlines/routes.py @@ -185,93 +185,94 @@ def delete(self, datapoint_id): return "", 204 -@api.route("/api/v1/metric", methods=["GET"]) -def api_get_metrics(): - """ - Return a list of all metrics in the database. - """ - logger.debug("api: GET all metrics") - raw_data = db.get_metrics() - if len(raw_data) == 0: - # do a thing. - return ErrorResponse.no_data() +@api.route("/api/v1/metric") +class Metrics(MethodView): + def get(self): + """ + Return a list of all metrics in the database. + """ + logger.debug("api: GET all metrics") + raw_data = db.get_metrics() + if len(raw_data) == 0: + # do a thing. + return ErrorResponse.no_data() - data = [model_to_dict(m) for m in raw_data] + data = [model_to_dict(m) for m in raw_data] - return jsonify(data) + return jsonify(data) + def post(self): + """ + Create a new metric. -@api.route("/api/v1/metric/", methods=["GET"]) -def get_metric_as_json(metric_name): - """ - Return metric information as JSON - """ - logger.debug("API: get metric '%s'" % metric_name) + Accepts JSON data with the following format: - try: - raw_data = db.Metric.get(db.Metric.name == metric_name) - except DoesNotExist: - return ErrorResponse.metric_not_found(metric_name) + .. code-block::json + { + "name": "your.metric_name.here", + "units": string, optional, + "upper_limit": {float, optional}, + "lower_limit": {float, optional}, + } - data = model_to_dict(raw_data) + Returns ``201`` on success, ``400`` on malformed JSON data (such as when + ``name`` is missing), or ``409`` if the metric already exists. - return jsonify(data) + See Also + -------- + :func:`routes.get_metric_as_json` + :func:`routes.delete_metric` + """ + data = request.get_json() + try: + metric = data['name'] + except KeyError: + return ErrorResponse.missing_required_key('name') -@api.route("/api/v1/metric", methods=["POST"]) -def post_metric(): - """ - Create a new metric. + try: + exists = db.Metric.get(db.Metric.name == metric) is not None + if exists: + return ErrorResponse.metric_already_exists(metric) + except DoesNotExist: + logger.debug("Metric does not exist. Able to create.") - Accepts JSON data with the following format: + units = data.get('units', None) + lower_limit = data.get('lower_limit', None) + upper_limit = data.get('upper_limit', None) - .. code-block::json - { - "name": "your.metric_name.here", - "units": string, optional, - "upper_limit": {float, optional}, - "lower_limit": {float, optional}, - } + new = db.add_metric(metric, units=units, lower_limit=lower_limit, + upper_limit=upper_limit) - Returns ``201`` on success, ``400`` on malformed JSON data (such as when - ``name`` is missing), or ``409`` if the metric already exists. + # Our `db.add_metric` fuction doesn't pull the new metric_id, so we + # grab that separately. + new.metric_id = db.Metric.get(db.Metric.name == new.name).metric_id - See Also - -------- - :func:`routes.get_metric_as_json` - :func:`routes.delete_metric` - """ - data = request.get_json() + msg = "Added Metric '{}'".format(metric) + body = { + "message": msg, + "metric": model_to_dict(new) + } + return jsonify(body), 201 - try: - metric = data['name'] - except KeyError: - return ErrorResponse.missing_required_key('name') + +@api.route("/api/v1/metric/", methods=["GET"]) +def get_metric_as_json(metric_name): + """ + Return metric information as JSON + """ + logger.debug("API: get metric '%s'" % metric_name) try: - exists = db.Metric.get(db.Metric.name == metric) is not None - if exists: - return ErrorResponse.metric_already_exists(metric) + raw_data = db.Metric.get(db.Metric.name == metric_name) except DoesNotExist: - logger.debug("Metric does not exist. Able to create.") + return ErrorResponse.metric_not_found(metric_name) - units = data.get('units', None) - lower_limit = data.get('lower_limit', None) - upper_limit = data.get('upper_limit', None) + data = model_to_dict(raw_data) - new = db.add_metric(metric, units=units, lower_limit=lower_limit, - upper_limit=upper_limit) + return jsonify(data) - # Our `db.add_metric` fuction doesn't pull the new metric_id, so we - # grab that separately. - new.metric_id = db.Metric.get(db.Metric.name == new.name).metric_id - msg = "Added Metric '{}'".format(metric) - body = { - "message": msg, - "metric": model_to_dict(new) - } - return jsonify(body), 201 @api.route("/api/v1/metric/", methods=["DELETE"])