Browse Source

API call for deleting checks.

pull/140/head
Pēteris Caune 7 years ago
parent
commit
23b237ed96
7 changed files with 112 additions and 21 deletions
  1. +23
    -0
      hc/api/tests/test_delete_check.py
  2. +11
    -3
      hc/api/views.py
  3. +1
    -0
      hc/front/management/commands/pygmentize.py
  4. +8
    -2
      static/css/docs.css
  5. +64
    -16
      templates/front/docs_api.html
  6. +3
    -0
      templates/front/snippets/delete_check_request.html
  7. +2
    -0
      templates/front/snippets/delete_check_request.txt

+ 23
- 0
hc/api/tests/test_delete_check.py View File

@ -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)

+ 11
- 3
hc/api/views.py View File

@ -139,7 +139,6 @@ def checks(request):
@csrf_exempt @csrf_exempt
@require_POST
@uuid_or_400 @uuid_or_400
@check_api_key @check_api_key
@validate_json(schemas.check) @validate_json(schemas.check)
@ -148,8 +147,17 @@ def update(request, code):
if check.user != request.user: if check.user != request.user:
return HttpResponseForbidden() 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 @csrf_exempt


+ 1
- 0
hc/front/management/commands/pygmentize.py View File

@ -48,3 +48,4 @@ class Command(BaseCommand):
_process("create_check_response", lexers.JsonLexer()) _process("create_check_response", lexers.JsonLexer())
_process("pause_check_request", lexers.BashLexer()) _process("pause_check_request", lexers.BashLexer())
_process("pause_check_response", lexers.JsonLexer()) _process("pause_check_response", lexers.JsonLexer())
_process("delete_check_request", lexers.BashLexer())

+ 8
- 2
static/css/docs.css View File

@ -27,8 +27,9 @@
} }
h2.rule { h2.rule {
border-top: 1px solid #ddd;
padding-top: 20px;
border-top: 3px solid #eee;
margin-top: 30px;
padding-top: 30px;
} }
h3.api-section { h3.api-section {
@ -41,6 +42,11 @@ h3.api-section {
font-family: monospace; font-family: monospace;
font-weight: bold; font-weight: bold;
margin-bottom: 1em; margin-bottom: 1em;
background: #f5f5f5;
color: #333;
display: inline-block;
padding: 2px 4px;
border-radius: 2px;
} }
a.section { a.section {


+ 64
- 16
templates/front/docs_api.html View File

@ -6,18 +6,46 @@
{% block docs_content %} {% block docs_content %}
<h2>REST API</h2> <h2>REST API</h2>
<p>
This is early days for healtchecks.io REST API. For now, there's API calls to:
<p>{% site_name %} REST API supports listing, creating,
updating, pausing and deleting checks in user's account.
</p> </p>
<ul>
<li><a href="#list-checks">List existing checks</a></li>
<li><a href="#create-check">Create a new check</a></li>
<li><a href="#update-check">Update an existing check</a></li>
<li><a href="#pause-check">Pause monitoring of a check</a></li>
</ul>
<h2 class="rule">Authentication</h2>
<p>Your requests to healtchecks.io REST API must authenticate using an
<h2>API Endpoints</h2>
<table class="table table-bordered">
<tr>
<td><a href="#list-checks">Get list of existing checks</a></td>
<td>
<code>GET {{ SITE_ROOT }}/api/v1/checks/</code>
</td>
</tr>
<tr>
<td><a href="#create-check">Create a new check</a></td>
<td>
<code>POST {{ SITE_ROOT }}/api/v1/checks/</code>
</td>
</tr>
<tr>
<td><a href="#update-check">Update an existing check</a></td>
<td>
<code>POST {{ SITE_ROOT }}/api/v1/checks/&lt;code&gt;</code>
</td>
</tr>
<tr>
<td><a href="#pause-check">Pause monitoring of a check</a></td>
<td>
<code>POST {{ SITE_ROOT }}/api/v1/checks/&lt;code&gt;/pause</code>
</td>
</tr>
<tr>
<td><a href="#delete-check">Delete check</a></td>
<td>
<code>DELETE {{ SITE_ROOT }}/api/v1/checks/&lt;code&gt;</code>
</td>
</tr>
</table>
<h2>Authentication</h2>
<p>Your requests to {% site_name %} REST API must authenticate using an
API key. By default, an user account on {% site_name %} doesn't have API key. By default, an user account on {% site_name %} doesn't have
an API key. You can create one in the <a href="{% url 'hc-profile' %}">Settings</a> page. an API key. You can create one in the <a href="{% url 'hc-profile' %}">Settings</a> page.
</p> </p>
@ -55,7 +83,7 @@ The response may contain a JSON document with additional data.
<!-- ********************************************************************** /--> <!-- ********************************************************************** /-->
<a class="section" name="list-checks"> <a class="section" name="list-checks">
<h2 class="rule">List checks</h2>
<h2 class="rule">Get List of Existing Checks</h2>
</a> </a>
<div class="api-path">GET {{ SITE_ROOT }}/api/v1/checks/</div> <div class="api-path">GET {{ SITE_ROOT }}/api/v1/checks/</div>
@ -90,7 +118,7 @@ one or more tags.</p>
<!-- ********************************************************************** /--> <!-- ********************************************************************** /-->
<a class="section" name="create-check"> <a class="section" name="create-check">
<h2 class="rule">Create a check</h2>
<h2 class="rule">Create a Check</h2>
</a> </a>
<div class="api-path">POST {{ SITE_ROOT }}/api/v1/checks/</div> <div class="api-path">POST {{ SITE_ROOT }}/api/v1/checks/</div>
@ -228,7 +256,7 @@ To create a "cron" check, specify the "schedule" and "tz" parameters.
<!-- ********************************************************************** /--> <!-- ********************************************************************** /-->
<a class="section" name="update-check"> <a class="section" name="update-check">
<h2 class="rule">Update an existing check</h2>
<h2 class="rule">Update an Existing Check</h2>
</a> </a>
<div class="api-path">POST {{ SITE_ROOT }}/api/v1/checks/&lt;code&gt;</div> <div class="api-path">POST {{ SITE_ROOT }}/api/v1/checks/&lt;code&gt;</div>
@ -338,8 +366,6 @@ To create a "cron" check, specify the "schedule" and "tz" parameters.
<div class="api-path">POST {{ SITE_ROOT }}/api/v1/checks/&lt;uuid&gt;/pause</div> <div class="api-path">POST {{ SITE_ROOT }}/api/v1/checks/&lt;uuid&gt;/pause</div>
<strong></strong>
<p> <p>
Disables monitoring for a check, without removing it. The check goes Disables monitoring for a check, without removing it. The check goes
into a "paused" state. You can resume monitoring of the check by pinging 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.
<h3 class="api-section">Example Response</h3> <h3 class="api-section">Example Response</h3>
{% include "front/snippets/pause_check_response.html" %} {% include "front/snippets/pause_check_response.html" %}
<!-- ********************************************************************** /-->
<a class="section" name="delete-check">
<h2 class="rule">Delete Check</h2>
</a>
<div class="api-path">DELETE {{ SITE_ROOT }}/api/v1/checks/&lt;uuid&gt;</div>
<p>
Permanently deletes the check from user's account. Returns JSON
representation of the check that was just deleted.
</p>
<p>
This API call has no request parameters.
</p>
<h3 class="api-section">Example Request</h3>
{% include "front/snippets/delete_check_request.html" %}
<h3 class="api-section">Example Response</h3>
{% include "front/snippets/create_check_response.html" %}
{% endblock %} {% endblock %}


+ 3
- 0
templates/front/snippets/delete_check_request.html View File

@ -0,0 +1,3 @@
<div class="highlight"><pre><span></span>curl {{ SITE_ROOT }}/api/v1/checks/f618072a-7bde-4eee-af63-71a77c5723bc <span class="se">\</span>
--request DELETE --header <span class="s2">&quot;X-Api-Key: your-api-key&quot;</span>
</pre></div>

+ 2
- 0
templates/front/snippets/delete_check_request.txt View File

@ -0,0 +1,2 @@
curl SITE_ROOT/api/v1/checks/f618072a-7bde-4eee-af63-71a77c5723bc \
--request DELETE --header "X-Api-Key: your-api-key"

Loading…
Cancel
Save