From ae30eec1d0e1ce00805b63303346eed9186a7470 Mon Sep 17 00:00:00 2001
From: Spencer Sun
Date: Mon, 4 Apr 2016 13:13:49 -0700
Subject: [PATCH] Add GET /api/v1/checks/ to the API
---
hc/api/tests/test_list_checks.py | 48 ++++++++++++++++++++++++++++
hc/api/views.py | 55 ++++++++++++++++++++------------
templates/front/docs_api.html | 24 ++++++++++++--
3 files changed, 104 insertions(+), 23 deletions(-)
create mode 100644 hc/api/tests/test_list_checks.py
diff --git a/hc/api/tests/test_list_checks.py b/hc/api/tests/test_list_checks.py
new file mode 100644
index 00000000..1e3907cf
--- /dev/null
+++ b/hc/api/tests/test_list_checks.py
@@ -0,0 +1,48 @@
+import json
+from datetime import timedelta as td
+
+from hc.api.models import Check, User
+from hc.test import BaseTestCase
+from hc.accounts.models import Profile
+
+class ListChecksTestCase(BaseTestCase):
+
+ def setUp(self):
+ super(ListChecksTestCase, self).setUp()
+ self.profile = Profile(user=self.alice, api_key="abc")
+ self.profile.save()
+ self.checks = [
+ Check(user=self.alice, name="Alice 1", timeout=td(seconds=3600), grace=td(seconds=900)),
+ Check(user=self.alice, name="Alice 2", timeout=td(seconds=86400), grace=td(seconds=3600)),
+ ]
+ for check in self.checks:
+ check.save()
+
+ def get(self, url, data):
+ return self.client.generic('GET', url, json.dumps(data), 'application/json')
+
+ def test_it_works(self):
+ r = self.get("/api/v1/checks/", { "api_key": "abc" })
+
+ self.assertEqual(r.status_code, 200)
+ self.assertTrue("checks" in r.json())
+ self.assertEqual(len(r.json()["checks"]), 2)
+
+ checks = { check["name"]: check for check in r.json()["checks"] }
+ self.assertEqual(checks["Alice 1"]["timeout"], 3600)
+ self.assertEqual(checks["Alice 1"]["grace"], 900)
+ self.assertEqual(checks["Alice 1"]["url"], self.checks[0].url())
+ self.assertEqual(checks["Alice 2"]["timeout"], 86400)
+ self.assertEqual(checks["Alice 2"]["grace"], 3600)
+ self.assertEqual(checks["Alice 2"]["url"], self.checks[1].url())
+
+ def test_it_shows_only_users_checks(self):
+ bob = User(username="bob", email="bob@example.com")
+ bob.save()
+ bob_check = Check(user=bob, name="Bob 1")
+
+ r = self.get("/api/v1/checks/", { "api_key": "abc" })
+
+ self.assertEqual(len(r.json()["checks"]), 2)
+ checks = { check["name"]: check for check in r.json()["checks"] }
+ self.assertNotIn("Bob 1", checks)
diff --git a/hc/api/views.py b/hc/api/views.py
index 0de24b27..92725864 100644
--- a/hc/api/views.py
+++ b/hc/api/views.py
@@ -80,26 +80,39 @@ def handle_email(request):
@check_api_key
@validate_json(schemas.check)
def create_check(request):
- if request.method != "POST":
+ if request.method == "GET":
+ code = 200
+ response = {
+ "checks": [{
+ "name": check.name,
+ "ping_url" : check.url(),
+ "tags": check.tags,
+ "timeout": int(check.timeout.total_seconds()),
+ "grace": int(check.grace.total_seconds()),
+ # "channels": check.channels,
+ } for check in Check.objects.filter(user=request.user)]
+ }
+ elif request.method == "POST":
+ check = Check(user=request.user)
+ check.name = str(request.json.get("name", ""))
+ check.tags = str(request.json.get("tags", ""))
+ if "timeout" in request.json:
+ check.timeout = td(seconds=request.json["timeout"])
+ if "grace" in request.json:
+ check.grace = td(seconds=request.json["grace"])
+
+ check.save()
+
+ # This needs to be done after saving the check, because of
+ # the M2M relation between checks and channels:
+ if request.json.get("channels") == "*":
+ check.assign_all_channels()
+
+ code = 201
+ response = {
+ "ping_url": check.url()
+ }
+ else:
return HttpResponse(status=405)
- check = Check(user=request.user)
- check.name = str(request.json.get("name", ""))
- check.tags = str(request.json.get("tags", ""))
- if "timeout" in request.json:
- check.timeout = td(seconds=request.json["timeout"])
- if "grace" in request.json:
- check.grace = td(seconds=request.json["grace"])
-
- check.save()
-
- # This needs to be done after saving the check, because of
- # the M2M relation between checks and channels:
- if request.json.get("channels") == "*":
- check.assign_all_channels()
-
- response = {
- "ping_url": check.url()
- }
-
- return JsonResponse(response, status=201)
+ return JsonResponse(response, status=code)
diff --git a/templates/front/docs_api.html b/templates/front/docs_api.html
index 524d0611..67098f4b 100644
--- a/templates/front/docs_api.html
+++ b/templates/front/docs_api.html
@@ -8,7 +8,7 @@
REST API
This is early days for healtchecks.io REST API. For now, there's just
-one API call: for creating a new check.
+one API resource for listing/creating checks.
Authentication
@@ -42,6 +42,26 @@ and 5xx indicates a server error.
The response may contain a JSON document with additional data.
+List checks
+
+GET {{ SITE_ROOT }}/api/v1/checks/
+
+
+ Returns a list of checks
+
+Example Request
+
+
+curl {{ SITE_ROOT }}/api/v1/checks/ \
+ -X GET \
+ -d '{"api_key": "your-api-key"}'
+
+
+Example Response
+
+{"checks": [{"url": "{{ PING_ENDPOINT }}848f3002-266b-482a-89ad-9d66a11aa2fb", "grace": 900, "name": "API test 1", "timeout": 3600, "tags": "foo"}, {"url": "{{ PING_ENDPOINT }}20324f81-5966-4e75-9734-8440df52ed75", "grace": 60, "name": "API test 2", "timeout": 60, "tags": "bar,baz"}]}
+
+
Create a check
POST {{ SITE_ROOT }}/api/v1/checks/
@@ -110,4 +130,4 @@ curl {{ SITE_ROOT }}/api/v1/checks/ \
{"ping_url": "{{ PING_ENDPOINT }}20f2d3d0-efe4-4cc1-a114-a186a225de50"}
-{% endblock %}
\ No newline at end of file
+{% endblock %}