Browse Source

In urls.py, route "api/v1/checks/<sha1:unique_key>" directly to the hc.api.views.get_check_by_unique_key view.

Minor API documentation edits.
pull/380/head
Pēteris Caune 4 years ago
parent
commit
cdafc06c65
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
5 changed files with 30 additions and 17 deletions
  1. +1
    -1
      CHANGELOG.md
  2. +6
    -4
      hc/api/urls.py
  3. +7
    -6
      hc/api/views.py
  4. +10
    -3
      templates/docs/api.html
  5. +6
    -3
      templates/docs/api.md

+ 1
- 1
CHANGELOG.md View File

@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file.
### Improvements ### Improvements
- Paused ping handling can be controlled via API (#376) - Paused ping handling can be controlled via API (#376)
- Add "Get a list of checks's logged pings" API call (#371) - Add "Get a list of checks's logged pings" API call (#371)
- Allowed the /api/v1/checks/ endpoint to receive a UUID or `unique_key` (#370)
- The /api/v1/checks/ endpoint now accepts either UUID or `unique_key` (#370)
### Bug Fixes ### Bug Fixes


+ 6
- 4
hc/api/urls.py View File

@ -5,7 +5,7 @@ from hc.api import views
class QuoteConverter: class QuoteConverter:
regex = "[\w%~_.-]+"
regex = r"[\w%~_.-]+"
def to_python(self, value): def to_python(self, value):
return unquote(value) return unquote(value)
@ -13,7 +13,8 @@ class QuoteConverter:
def to_url(self, value): def to_url(self, value):
return quote(value, safe="") return quote(value, safe="")
class UniqueKeyConverter:
class SHA1Converter:
regex = "[A-z0-9]{40}" regex = "[A-z0-9]{40}"
def to_python(self, value): def to_python(self, value):
@ -22,8 +23,9 @@ class UniqueKeyConverter:
def to_url(self, value): def to_url(self, value):
return value return value
register_converter(QuoteConverter, "quoted") register_converter(QuoteConverter, "quoted")
register_converter(UniqueKeyConverter, "unique_key")
register_converter(SHA1Converter, "sha1")
urlpatterns = [ urlpatterns = [
path("ping/<uuid:code>/", views.ping, name="hc-ping-slash"), path("ping/<uuid:code>/", views.ping, name="hc-ping-slash"),
@ -32,7 +34,7 @@ urlpatterns = [
path("ping/<uuid:code>/start", views.ping, {"action": "start"}, name="hc-start"), path("ping/<uuid:code>/start", views.ping, {"action": "start"}, name="hc-start"),
path("api/v1/checks/", views.checks), path("api/v1/checks/", views.checks),
path("api/v1/checks/<uuid:code>", views.single, name="hc-api-single"), path("api/v1/checks/<uuid:code>", views.single, name="hc-api-single"),
path("api/v1/checks/<unique_key:code>", views.single, name="hc-api-single"),
path("api/v1/checks/<sha1:unique_key>", views.get_check_by_unique_key),
path("api/v1/checks/<uuid:code>/pause", views.pause, name="hc-api-pause"), path("api/v1/checks/<uuid:code>/pause", views.pause, name="hc-api-pause"),
path("api/v1/notifications/<uuid:code>/bounce", views.bounce, name="hc-api-bounce"), path("api/v1/notifications/<uuid:code>/bounce", views.bounce, name="hc-api-bounce"),
path("api/v1/checks/<uuid:code>/pings/", views.pings, name="hc-api-pings"), path("api/v1/checks/<uuid:code>/pings/", views.pings, name="hc-api-pings"),


+ 7
- 6
hc/api/views.py View File

@ -191,15 +191,19 @@ def get_check(request, code):
return JsonResponse(check.to_dict(readonly=request.readonly)) return JsonResponse(check.to_dict(readonly=request.readonly))
@cors("GET")
@csrf_exempt
@validate_json() @validate_json()
@authorize_read @authorize_read
def get_check_unique(request, code):
def get_check_by_unique_key(request, unique_key):
checks = Check.objects.filter(project=request.project.id) checks = Check.objects.filter(project=request.project.id)
for check in checks: for check in checks:
if check.unique_key == code:
if check.unique_key == unique_key:
return JsonResponse(check.to_dict(readonly=request.readonly)) return JsonResponse(check.to_dict(readonly=request.readonly))
return HttpResponseNotFound() return HttpResponseNotFound()
@validate_json(schemas.check) @validate_json(schemas.check)
@authorize @authorize
def update_check(request, code): def update_check(request, code):
@ -236,10 +240,7 @@ def single(request, code):
if request.method == "DELETE": if request.method == "DELETE":
return delete_check(request, code) return delete_check(request, code)
if type(code) == uuid.UUID:
return get_check(request, code)
else:
return get_check_unique(request, code)
return get_check(request, code)
@cors("POST") @cors("POST")


+ 10
- 3
templates/docs/api.html View File

@ -19,6 +19,10 @@ checks in user's account.</p>
<td><code>GET SITE_ROOT/api/v1/checks/&lt;uuid&gt;</code></td> <td><code>GET SITE_ROOT/api/v1/checks/&lt;uuid&gt;</code></td>
</tr> </tr>
<tr> <tr>
<td><a href="#get-check">Get a check by its unique_key</a></td>
<td><code>GET SITE_ROOT/api/v1/checks/&lt;unique_key&gt;</code></td>
</tr>
<tr>
<td><a href="#create-check">Create a new check</a></td> <td><a href="#create-check">Create a new check</a></td>
<td><code>POST SITE_ROOT/api/v1/checks/</code></td> <td><code>POST SITE_ROOT/api/v1/checks/</code></td>
</tr> </tr>
@ -152,7 +156,7 @@ specified value.</p>
<p>When using the read-only API key, the following fields are omitted: <p>When using the read-only API key, the following fields are omitted:
<code>ping_url</code>, <code>update_url</code>, <code>pause_url</code>, <code>channels</code>. An extra <code>unique_key</code> field <code>ping_url</code>, <code>update_url</code>, <code>pause_url</code>, <code>channels</code>. An extra <code>unique_key</code> field
is added. This identifier is stable across API calls. Example:</p>
is added which can be used <a href="#get-check">to <code>GET</code> a check</a> in place of the <code>UUID</code>. The <code>unique_key</code> identifier is stable across API calls. Example:</p>
<div class="highlight"><pre><span></span><code><span class="p">{</span> <div class="highlight"><pre><span></span><code><span class="p">{</span>
<span class="nt">&quot;checks&quot;</span><span class="p">:</span> <span class="p">[</span> <span class="nt">&quot;checks&quot;</span><span class="p">:</span> <span class="p">[</span>
<span class="p">{</span> <span class="p">{</span>
@ -188,8 +192,11 @@ is added. This identifier is stable across API calls. Example:</p>
<h2 class="rule" id="get-check">Get a Single Check</h2> <h2 class="rule" id="get-check">Get a Single Check</h2>
<p><code>GET SITE_ROOT/api/v1/checks/&lt;uuid&gt;</code></p>
<p>Returns a JSON representation of a single check.</p>
<p><code>GET SITE_ROOT/api/v1/checks/&lt;uuid&gt;</code><br>
<code>GET SITE_ROOT/api/v1/checks/&lt;unique_key&gt;</code></p>
<p>Returns a JSON representation of a single check. Accepts either check's UUID or
the <code>unique_key</code> (a field derived from UUID, and returned by API responses when
using the read-only API key) as an identifier.</p>
<h3>Response Codes</h3> <h3>Response Codes</h3>
<dl> <dl>
<dt>200 OK</dt> <dt>200 OK</dt>


+ 6
- 3
templates/docs/api.md View File

@ -9,7 +9,7 @@ Endpoint Name | Endpoint Address
------------------------------------------------------|------- ------------------------------------------------------|-------
[Get a list of existing checks](#list-checks) | `GET SITE_ROOT/api/v1/checks/` [Get a list of existing checks](#list-checks) | `GET SITE_ROOT/api/v1/checks/`
[Get a single check](#get-check) | `GET SITE_ROOT/api/v1/checks/<uuid>` [Get a single check](#get-check) | `GET SITE_ROOT/api/v1/checks/<uuid>`
[Get a single check (using Read Only API)](#get-check) | `GET SITE_ROOT/api/v1/checks/<unique_key>`
[Get a check by its unique_key](#get-check) | `GET SITE_ROOT/api/v1/checks/<unique_key>`
[Create a new check](#create-check) | `POST SITE_ROOT/api/v1/checks/` [Create a new check](#create-check) | `POST SITE_ROOT/api/v1/checks/`
[Update an existing check](#update-check) | `POST SITE_ROOT/api/v1/checks/<uuid>` [Update an existing check](#update-check) | `POST SITE_ROOT/api/v1/checks/<uuid>`
[Pause monitoring of a check](#pause-check) | `POST SITE_ROOT/api/v1/checks/<uuid>/pause` [Pause monitoring of a check](#pause-check) | `POST SITE_ROOT/api/v1/checks/<uuid>/pause`
@ -166,9 +166,12 @@ is added which can be used [to `GET` a check](#get-check) in place of the `UUID`
``` ```
## Get a Single Check {: #get-check .rule } ## Get a Single Check {: #get-check .rule }
`GET SITE_ROOT/api/v1/checks/<uuid>` OR `GET SITE_ROOT/api/v1/checks/<unique_key>`
`GET SITE_ROOT/api/v1/checks/<uuid>`<br>
`GET SITE_ROOT/api/v1/checks/<unique_key>`
Returns a JSON representation of a single check. Can take either the UUID or the `unique_key` (see [information above](#list-checks)) as the identifier of the check to return.
Returns a JSON representation of a single check. Accepts either check's UUID or
the `unique_key` (a field derived from UUID, and returned by API responses when
using the read-only API key) as an identifier.
### Response Codes ### Response Codes


Loading…
Cancel
Save