From e55650c781dd1c519d1c18434eee8387a1583b61 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Thu, 14 Apr 2016 15:01:41 +0300 Subject: [PATCH] Syntax highlighting for API examples. --- hc/front/management/commands/pygmentize.py | 30 ++++++++++++------- templates/front/docs_api.html | 26 +++++----------- templates/front/snippets/bash.html | 4 +-- templates/front/snippets/browser.html | 2 +- .../front/snippets/create_check_request.html | 4 +++ .../front/snippets/create_check_request.txt | 3 ++ .../front/snippets/create_check_response.html | 4 +++ .../front/snippets/create_check_response.txt | 3 ++ templates/front/snippets/crontab.html | 2 +- .../front/snippets/list_checks_request.html | 4 +++ .../front/snippets/list_checks_request.txt | 3 ++ .../front/snippets/list_checks_response.html | 19 ++++++++++++ .../front/snippets/list_checks_response.txt | 18 +++++++++++ templates/front/snippets/node.html | 2 +- templates/front/snippets/php.html | 2 +- templates/front/snippets/powershell.html | 2 +- templates/front/snippets/python.html | 8 ++--- 17 files changed, 95 insertions(+), 41 deletions(-) create mode 100644 templates/front/snippets/create_check_request.html create mode 100644 templates/front/snippets/create_check_request.txt create mode 100644 templates/front/snippets/create_check_response.html create mode 100644 templates/front/snippets/create_check_response.txt create mode 100644 templates/front/snippets/list_checks_request.html create mode 100644 templates/front/snippets/list_checks_request.txt create mode 100644 templates/front/snippets/list_checks_response.html create mode 100644 templates/front/snippets/list_checks_response.txt diff --git a/hc/front/management/commands/pygmentize.py b/hc/front/management/commands/pygmentize.py index 99d67b8a..b64bc695 100644 --- a/hc/front/management/commands/pygmentize.py +++ b/hc/front/management/commands/pygmentize.py @@ -1,13 +1,15 @@ from django.core.management.base import BaseCommand -def _process(fin, fout, lexer): +def _process(name, lexer): from pygments import highlight from pygments.formatters import HtmlFormatter - source = open("templates/front/snippets/" + fin).read() + source = open("templates/front/snippets/%s.txt" % name).read() processed = highlight(source, lexer, HtmlFormatter()) processed = processed.replace("PING_URL", "{{ ping_url }}") - with open("templates/front/snippets/" + fout, "w") as out: + processed = processed.replace("SITE_ROOT", "{{ SITE_ROOT }}") + processed = processed.replace("PING_ENDPOINT", "{{ PING_ENDPOINT }}") + with open("templates/front/snippets/%s.html" % name, "w") as out: out.write(processed) @@ -24,11 +26,17 @@ class Command(BaseCommand): self.stdout.write(" pip install Pygments\n\n") return - _process("bash.txt", "bash.html", lexers.BashLexer()) - _process("browser.txt", "browser.html", lexers.JavascriptLexer()) - _process("crontab.txt", "crontab.html", lexers.BashLexer()) - _process("python.txt", "python.html", lexers.PythonLexer()) - _process("php.txt", "php.html", lexers.PhpLexer()) - _process("powershell.txt", "powershell.html", - lexers.shell.PowerShellLexer()) - _process("node.txt", "node.html", lexers.JavascriptLexer()) + # Invocation examples + _process("bash", lexers.BashLexer()) + _process("browser", lexers.JavascriptLexer()) + _process("crontab", lexers.BashLexer()) + _process("python", lexers.PythonLexer()) + _process("php", lexers.PhpLexer()) + _process("powershell", lexers.shell.PowerShellLexer()) + _process("node", lexers.JavascriptLexer()) + + # API examples + _process("list_checks_request", lexers.BashLexer()) + _process("list_checks_response", lexers.JsonLexer()) + _process("create_check_request", lexers.BashLexer()) + _process("create_check_response", lexers.JsonLexer()) diff --git a/templates/front/docs_api.html b/templates/front/docs_api.html index 67098f4b..3a02830e 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 resource for listing/creating checks. +one API resource for listing and creating checks.

Authentication

@@ -47,20 +47,14 @@ The response may contain a JSON document with additional data.
GET {{ SITE_ROOT }}/api/v1/checks/

- Returns a list of checks + Returns a list of checks. This API call takes no parameters and returns + a JSON document with all checks in user's account.

Example Request

- -
-curl {{ SITE_ROOT }}/api/v1/checks/ \
-    -X GET \
-    -d '{"api_key": "your-api-key"}'
-
+{% include "front/snippets/list_checks_request.html" %}

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"}]}
-
+{% include "front/snippets/list_checks_response.html" %}

Create a check

@@ -119,15 +113,9 @@ curl {{ SITE_ROOT }}/api/v1/checks/ \

Example Request

-
-curl {{ SITE_ROOT }}/api/v1/checks/ \
-    -X POST \
-    -d '{"api_key": "your-api-key", "name": "Backups", "tags": "prod www", "timeout": 3600, "grace": 60}'
-
+{% include "front/snippets/create_check_request.html" %}

Example Response

-
-{"ping_url": "{{ PING_ENDPOINT }}20f2d3d0-efe4-4cc1-a114-a186a225de50"}
-
+{% include "front/snippets/create_check_response.html" %} {% endblock %} diff --git a/templates/front/snippets/bash.html b/templates/front/snippets/bash.html index f3238c16..eedbea88 100644 --- a/templates/front/snippets/bash.html +++ b/templates/front/snippets/bash.html @@ -1,6 +1,6 @@ -
# using curl:
+
# using curl:
 curl --retry 3 {{ ping_url }}
 
-# using wget:
+# using wget:
 wget {{ ping_url }} -O /dev/null
 
diff --git a/templates/front/snippets/browser.html b/templates/front/snippets/browser.html index 6f053e28..1f030cbd 100644 --- a/templates/front/snippets/browser.html +++ b/templates/front/snippets/browser.html @@ -1,4 +1,4 @@ -
var xhr = new XMLHttpRequest();
+
var xhr = new XMLHttpRequest();
 xhr.open('GET', '{{ ping_url }}', true);
 xhr.send(null);
 
diff --git a/templates/front/snippets/create_check_request.html b/templates/front/snippets/create_check_request.html new file mode 100644 index 00000000..ce58ecf2 --- /dev/null +++ b/templates/front/snippets/create_check_request.html @@ -0,0 +1,4 @@ +
curl http://localhost:8000/api/v1/checks/ \
+    -X POST \
+    -d '{"api_key": "your-api-key", "name": "Backups", "tags": "prod www", "timeout": 3600, "grace": 60}'
+
diff --git a/templates/front/snippets/create_check_request.txt b/templates/front/snippets/create_check_request.txt new file mode 100644 index 00000000..92c077d0 --- /dev/null +++ b/templates/front/snippets/create_check_request.txt @@ -0,0 +1,3 @@ +curl http://localhost:8000/api/v1/checks/ \ + -X POST \ + -d '{"api_key": "your-api-key", "name": "Backups", "tags": "prod www", "timeout": 3600, "grace": 60}' \ No newline at end of file diff --git a/templates/front/snippets/create_check_response.html b/templates/front/snippets/create_check_response.html new file mode 100644 index 00000000..0d76255d --- /dev/null +++ b/templates/front/snippets/create_check_response.html @@ -0,0 +1,4 @@ +
{
+  "ping_url": "{{ PING_ENDPOINT }}20f2d3d0-efe4-4cc1-a114-a186a225de50"
+}
+
diff --git a/templates/front/snippets/create_check_response.txt b/templates/front/snippets/create_check_response.txt new file mode 100644 index 00000000..2dda1808 --- /dev/null +++ b/templates/front/snippets/create_check_response.txt @@ -0,0 +1,3 @@ +{ + "ping_url": "PING_ENDPOINT20f2d3d0-efe4-4cc1-a114-a186a225de50" +} \ No newline at end of file diff --git a/templates/front/snippets/crontab.html b/templates/front/snippets/crontab.html index 07afc5d4..bd8e3fdf 100644 --- a/templates/front/snippets/crontab.html +++ b/templates/front/snippets/crontab.html @@ -1,3 +1,3 @@ -
# m h dom mon dow   command
+
# m h dom mon dow   command
   8 6 *   *   *     /home/user/backup.sh && curl -fsS --retry 3 {{ ping_url }} > /dev/null
 
diff --git a/templates/front/snippets/list_checks_request.html b/templates/front/snippets/list_checks_request.html new file mode 100644 index 00000000..0998b652 --- /dev/null +++ b/templates/front/snippets/list_checks_request.html @@ -0,0 +1,4 @@ +
curl {{ SITE_ROOT }}/api/v1/checks/ \
+    -X GET \
+    -d '{"api_key": "your-api-key"}'
+
diff --git a/templates/front/snippets/list_checks_request.txt b/templates/front/snippets/list_checks_request.txt new file mode 100644 index 00000000..18f91aca --- /dev/null +++ b/templates/front/snippets/list_checks_request.txt @@ -0,0 +1,3 @@ +curl SITE_ROOT/api/v1/checks/ \ + -X GET \ + -d '{"api_key": "your-api-key"}' \ No newline at end of file diff --git a/templates/front/snippets/list_checks_response.html b/templates/front/snippets/list_checks_response.html new file mode 100644 index 00000000..d9b0da8c --- /dev/null +++ b/templates/front/snippets/list_checks_response.html @@ -0,0 +1,19 @@ +
{
+  "checks": [
+    {
+      "ping_url": "{{ PING_ENDPOINT }}848f3002-266b-482a-89ad-9d66a11aa2fb",
+      "grace": 900,
+      "name": "API test 1",
+      "timeout": 3600,
+      "tags": "foo"
+    },
+    {
+      "ping_url": "{{ PING_ENDPOINT }}20324f81-5966-4e75-9734-8440df52ed75",
+      "grace": 60,
+      "name": "API test 2",
+      "timeout": 60,
+      "tags": "bar,baz"
+    }
+  ]
+}
+
diff --git a/templates/front/snippets/list_checks_response.txt b/templates/front/snippets/list_checks_response.txt new file mode 100644 index 00000000..08f62fdc --- /dev/null +++ b/templates/front/snippets/list_checks_response.txt @@ -0,0 +1,18 @@ +{ + "checks": [ + { + "ping_url": "PING_ENDPOINT848f3002-266b-482a-89ad-9d66a11aa2fb", + "grace": 900, + "name": "API test 1", + "timeout": 3600, + "tags": "foo" + }, + { + "ping_url": "PING_ENDPOINT20324f81-5966-4e75-9734-8440df52ed75", + "grace": 60, + "name": "API test 2", + "timeout": 60, + "tags": "bar,baz" + } + ] +} \ No newline at end of file diff --git a/templates/front/snippets/node.html b/templates/front/snippets/node.html index f3b216a6..c2d2e703 100644 --- a/templates/front/snippets/node.html +++ b/templates/front/snippets/node.html @@ -1,3 +1,3 @@ -
var http = require('http');
+
var http = require('http');
 http.get("{{ ping_url }}");
 
diff --git a/templates/front/snippets/php.html b/templates/front/snippets/php.html index e30dec60..586b4af8 100644 --- a/templates/front/snippets/php.html +++ b/templates/front/snippets/php.html @@ -1,2 +1,2 @@ -
file_get_contents('{{ ping_url }}');
+
file_get_contents('{{ ping_url }}');
 
diff --git a/templates/front/snippets/powershell.html b/templates/front/snippets/powershell.html index f238c946..a2b1a2ea 100644 --- a/templates/front/snippets/powershell.html +++ b/templates/front/snippets/powershell.html @@ -1,2 +1,2 @@ -
Invoke-RestMethod {{ ping_url }}
+
Invoke-RestMethod {{ ping_url }}
 
diff --git a/templates/front/snippets/python.html b/templates/front/snippets/python.html index b60e49af..f75fd980 100644 --- a/templates/front/snippets/python.html +++ b/templates/front/snippets/python.html @@ -1,8 +1,8 @@ -
>>> # using urllib2:
+
>>> # using urllib2:
 >>> import urllib2
->>> urllib2.urlopen("{{ ping_url }}")
+>>> urllib2.urlopen("{{ ping_url }}")
 
->>> # using requests:
+>>> # using requests:
 >>> import requests
->>> requests.get("{{ ping_url }}")
+>>> requests.get("{{ ping_url }}")