Skip to content

Commit

Permalink
Merge pull request #131 from dougthor42/pluggable-views-part-2
Browse files Browse the repository at this point in the history
Move the /metric api to pluggable views in routes.py
  • Loading branch information
dougthor42 committed Mar 21, 2019
2 parents 34d6394 + 1262e35 commit 30cd0c8
Showing 1 changed file with 67 additions and 66 deletions.
133 changes: 67 additions & 66 deletions src/trendlines/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<metric_name>", 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/<metric_name>", 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/<metric_name>", methods=["DELETE"])
Expand Down

0 comments on commit 30cd0c8

Please sign in to comment.