From d2464a206a7e7733889cc9bfe965988218199674 Mon Sep 17 00:00:00 2001 From: Christopher Gooley Date: Wed, 8 Apr 2015 14:21:15 -0700 Subject: [PATCH] added helper for metrics_bulk API endpoint --- config.py | 1 + example_metrics.py | 22 ++++++++++++++++++++++ preact.py | 10 ++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 example_metrics.py diff --git a/config.py b/config.py index 45fa818..b0433c5 100644 --- a/config.py +++ b/config.py @@ -2,3 +2,4 @@ API_SECRET = "API_SECRET" API_URL = "https://api.preact.io/api/v2/" EVENTS_ENDPOINT = "events" +METRICS_BULK_ENDPOINT = "metrics/bulk" \ No newline at end of file diff --git a/example_metrics.py b/example_metrics.py new file mode 100644 index 0000000..85cbda9 --- /dev/null +++ b/example_metrics.py @@ -0,0 +1,22 @@ +from preact import log_metrics_bulk + +metricsdata = { + 'data': [ + { + 'account_id': '517eda23c82561f72a000005', + 'name': 'total-gigabytes-utilized', + 'value': 691.751, + 'ts': 1428527783 + }, + { + 'account_id': '533dd27f4443aec9e4000001', + 'name': 'total-gigabytes-utilized', + 'value': 913.751, + 'ts': 1428527783 + } + ] +} + +resp = log_metrics_bulk(metricsdata) + +print resp diff --git a/preact.py b/preact.py index 9f4ebe9..f059f6f 100644 --- a/preact.py +++ b/preact.py @@ -1,7 +1,13 @@ import config, requests, json def log_event(data): - url = config.API_URL + config.EVENTS_ENDPOINT + return post_data(data, config.EVENTS_ENDPOINT) + +def log_metrics_bulk(data): + return post_data(data, config.METRICS_BULK_ENDPOINT) + +def post_data(data, endpoint): + url = config.API_URL + endpoint auth = (config.PROJECT_CODE, config.API_SECRET) headers = {'Content-type': 'application/json', 'Accept': 'text/plain'} @@ -9,4 +15,4 @@ def log_event(data): json_data = json.dumps(data) resp = requests.post(url, data=json_data, headers=headers, auth=auth) - return resp + return resp \ No newline at end of file