def metrics_lines(api_key): if authenticator.valid(api_key): metrics = bottle.request.body.readlines() if type(metrics) == list: for line in metrics: try: parts = line.split() if len(parts) != 3: continue else: metric = { 'metric': parts[0], 'value': parts[1], 'timestamp': parts[2] } m = Metric(metric) m.enqueue(q) except ValueError: bottle.abort(400, "Invalid Metric Structure: %s" % (str(metric))) except Exception: bottle.abort(500, "Unable to store metric!") else: bottle.abort(400, "Metric structure must be lines of 'metric.path value timestamp'") else: bottle.abort(403, "API Key not valid")
def publish(api_key): if authenticator.valid(api_key): try: body = bottle.request.body.read() metrics = json.loads(body) except: log.error("Request unparsable: %s" % (body)) bottle.abort(400, "Unable to successfully parse JSON") if type(metrics) == list: for metric in metrics: try: m = Metric(metric) m.enqueue(q) except ValueError: bottle.abort(400, "Invalid Metric Structure: %s" % (str(metric))) except Exception: bottle.abort(500, "Unable to store metric!") else: bottle.abort(400, "Metric structure must be <list> containing n <dict> items") else: bottle.abort(403, "API Key not valid")