From 23b237ed967b8d24ec0a0f9f6bd0410c75fb3213 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Tue, 12 Sep 2017 13:33:12 +0300 Subject: [PATCH] API call for deleting checks. --- hc/api/tests/test_delete_check.py | 23 ++++++ hc/api/views.py | 14 +++- hc/front/management/commands/pygmentize.py | 1 + static/css/docs.css | 10 ++- templates/front/docs_api.html | 80 +++++++++++++++---- .../front/snippets/delete_check_request.html | 3 + .../front/snippets/delete_check_request.txt | 2 + 7 files changed, 112 insertions(+), 21 deletions(-) create mode 100644 hc/api/tests/test_delete_check.py create mode 100644 templates/front/snippets/delete_check_request.html create mode 100644 templates/front/snippets/delete_check_request.txt diff --git a/hc/api/tests/test_delete_check.py b/hc/api/tests/test_delete_check.py new file mode 100644 index 00000000..ad6fe72f --- /dev/null +++ b/hc/api/tests/test_delete_check.py @@ -0,0 +1,23 @@ +from hc.api.models import Check +from hc.test import BaseTestCase + + +class DeleteCheckTestCase(BaseTestCase): + + def setUp(self): + super(DeleteCheckTestCase, self).setUp() + self.check = Check(user=self.alice) + self.check.save() + + def test_it_works(self): + r = self.client.delete("/api/v1/checks/%s" % self.check.code, + HTTP_X_API_KEY="abc") + self.assertEqual(r.status_code, 200) + + # It should be gone-- + self.assertFalse(Check.objects.filter(code=self.check.code).exists()) + + def test_it_handles_missing_check(self): + url = "/api/v1/checks/07c2f548-9850-4b27-af5d-6c9dc157ec02" + r = self.client.delete(url, HTTP_X_API_KEY="abc") + self.assertEqual(r.status_code, 404) diff --git a/hc/api/views.py b/hc/api/views.py index f577361c..04844c0b 100644 --- a/hc/api/views.py +++ b/hc/api/views.py @@ -139,7 +139,6 @@ def checks(request): @csrf_exempt -@require_POST @uuid_or_400 @check_api_key @validate_json(schemas.check) @@ -148,8 +147,17 @@ def update(request, code): if check.user != request.user: return HttpResponseForbidden() - _update(check, request.json) - return JsonResponse(check.to_dict()) + if request.method == "POST": + _update(check, request.json) + return JsonResponse(check.to_dict()) + + elif request.method == "DELETE": + response = check.to_dict() + check.delete() + return JsonResponse(response) + + # Otherwise, method not allowed + return HttpResponse(status=405) @csrf_exempt diff --git a/hc/front/management/commands/pygmentize.py b/hc/front/management/commands/pygmentize.py index 48c7d986..82055540 100644 --- a/hc/front/management/commands/pygmentize.py +++ b/hc/front/management/commands/pygmentize.py @@ -48,3 +48,4 @@ class Command(BaseCommand): _process("create_check_response", lexers.JsonLexer()) _process("pause_check_request", lexers.BashLexer()) _process("pause_check_response", lexers.JsonLexer()) + _process("delete_check_request", lexers.BashLexer()) diff --git a/static/css/docs.css b/static/css/docs.css index ff2188e0..070c8eec 100644 --- a/static/css/docs.css +++ b/static/css/docs.css @@ -27,8 +27,9 @@ } h2.rule { - border-top: 1px solid #ddd; - padding-top: 20px; + border-top: 3px solid #eee; + margin-top: 30px; + padding-top: 30px; } h3.api-section { @@ -41,6 +42,11 @@ h3.api-section { font-family: monospace; font-weight: bold; margin-bottom: 1em; + background: #f5f5f5; + color: #333; + display: inline-block; + padding: 2px 4px; + border-radius: 2px; } a.section { diff --git a/templates/front/docs_api.html b/templates/front/docs_api.html index be0c5717..1f8379e4 100644 --- a/templates/front/docs_api.html +++ b/templates/front/docs_api.html @@ -6,18 +6,46 @@ {% block docs_content %}

REST API

-

-This is early days for healtchecks.io REST API. For now, there's API calls to: +

{% site_name %} REST API supports listing, creating, + updating, pausing and deleting checks in user's account.

- - -

Authentication

-

Your requests to healtchecks.io REST API must authenticate using an + +

API Endpoints

+ + + + + + + + + + + + + + + + + + + + + +
Get list of existing checks + GET {{ SITE_ROOT }}/api/v1/checks/ +
Create a new check + POST {{ SITE_ROOT }}/api/v1/checks/ +
Update an existing check + POST {{ SITE_ROOT }}/api/v1/checks/<code> +
Pause monitoring of a check + POST {{ SITE_ROOT }}/api/v1/checks/<code>/pause +
Delete check + DELETE {{ SITE_ROOT }}/api/v1/checks/<code> +
+ +

Authentication

+

Your requests to {% site_name %} REST API must authenticate using an API key. By default, an user account on {% site_name %} doesn't have an API key. You can create one in the Settings page.

@@ -55,7 +83,7 @@ The response may contain a JSON document with additional data. -

List checks

+

Get List of Existing Checks

GET {{ SITE_ROOT }}/api/v1/checks/
@@ -90,7 +118,7 @@ one or more tags.

-

Create a check

+

Create a Check

POST {{ SITE_ROOT }}/api/v1/checks/
@@ -228,7 +256,7 @@ To create a "cron" check, specify the "schedule" and "tz" parameters. -

Update an existing check

+

Update an Existing Check

POST {{ SITE_ROOT }}/api/v1/checks/<code>
@@ -338,8 +366,6 @@ To create a "cron" check, specify the "schedule" and "tz" parameters.
POST {{ SITE_ROOT }}/api/v1/checks/<uuid>/pause
- -

Disables monitoring for a check, without removing it. The check goes into a "paused" state. You can resume monitoring of the check by pinging @@ -362,6 +388,28 @@ is sometimes required by some network proxies and web servers.

Example Response

{% include "front/snippets/pause_check_response.html" %} + + + +

Delete Check

+
+ +
DELETE {{ SITE_ROOT }}/api/v1/checks/<uuid>
+ +

+ Permanently deletes the check from user's account. Returns JSON + representation of the check that was just deleted. +

+

+ This API call has no request parameters. +

+ +

Example Request

+ +{% include "front/snippets/delete_check_request.html" %} + +

Example Response

+{% include "front/snippets/create_check_response.html" %} {% endblock %} diff --git a/templates/front/snippets/delete_check_request.html b/templates/front/snippets/delete_check_request.html new file mode 100644 index 00000000..d8c91835 --- /dev/null +++ b/templates/front/snippets/delete_check_request.html @@ -0,0 +1,3 @@ +
curl {{ SITE_ROOT }}/api/v1/checks/f618072a-7bde-4eee-af63-71a77c5723bc \
+    --request DELETE --header "X-Api-Key: your-api-key"
+
diff --git a/templates/front/snippets/delete_check_request.txt b/templates/front/snippets/delete_check_request.txt new file mode 100644 index 00000000..bb873346 --- /dev/null +++ b/templates/front/snippets/delete_check_request.txt @@ -0,0 +1,2 @@ +curl SITE_ROOT/api/v1/checks/f618072a-7bde-4eee-af63-71a77c5723bc \ + --request DELETE --header "X-Api-Key: your-api-key"