forked from GithubBackups/healthchecks
Merge pull request #50 from agaridata/master
Implement GET /api/v1/checks/ for listing existing checks
This commit is contained in:
commit
06074c8f8e
48
hc/api/tests/test_list_checks.py
Normal file
48
hc/api/tests/test_list_checks.py
Normal file
@ -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)
|
@ -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)
|
||||
|
@ -8,7 +8,7 @@
|
||||
<h2>REST API</h2>
|
||||
<p>
|
||||
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.
|
||||
</p>
|
||||
|
||||
<h2 class="rule">Authentication</h2>
|
||||
@ -42,6 +42,26 @@ and 5xx indicates a server error.
|
||||
The response may contain a JSON document with additional data.
|
||||
</p>
|
||||
|
||||
<h2 class="rule">List checks</h2>
|
||||
|
||||
<div class="api-path">GET {{ SITE_ROOT }}/api/v1/checks/</div>
|
||||
|
||||
<p>
|
||||
Returns a list of checks
|
||||
</p>
|
||||
<h3 class="api-section">Example Request</h3>
|
||||
|
||||
<pre>
|
||||
curl {{ SITE_ROOT }}/api/v1/checks/ \
|
||||
-X GET \
|
||||
-d '{"api_key": "your-api-key"}'
|
||||
</pre>
|
||||
|
||||
<h3 class="api-section">Example Response</h3>
|
||||
<pre>
|
||||
{"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"}]}
|
||||
</pre>
|
||||
|
||||
<h2 class="rule">Create a check</h2>
|
||||
|
||||
<div class="api-path">POST {{ SITE_ROOT }}/api/v1/checks/</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user