From e60a3f1892bb685fcbe6ebbc9ed10602c9549028 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Sat, 12 Oct 2019 20:25:55 +0200 Subject: Add and document health API. Fixes #202 --- docs/index.rst | 1 + mastodon/Mastodon.py | 7 +++++++ tests/cassettes/test_health.yaml | 30 ++++++++++++++++++++++++++++++ tests/test_instance.py | 4 ++++ 4 files changed, 42 insertions(+) create mode 100644 tests/cassettes/test_health.yaml diff --git a/docs/index.rst b/docs/index.rst index 6e234d0..f65fe1e 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -843,6 +843,7 @@ current instance. .. automethod:: Mastodon.instance .. automethod:: Mastodon.instance_activity .. automethod:: Mastodon.instance_peers +.. automethod:: Mastodon.instance_health Reading data: Timelines ----------------------- diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index 308c428..f0063e5 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -654,6 +654,13 @@ class Mastodon: """ return self.__api_request('GET', '/api/v1/instance/peers') + @api_version("3.0.0", "3.0.0", "3.0.0") + def instance_health(self): + """ + Basic health check. Returns True if healthy, False if not. + """ + return self.__api_request('GET', '/health', parse=False).decode("utf-8") == "success" + ### # Reading data: Timelines ## diff --git a/tests/cassettes/test_health.yaml b/tests/cassettes/test_health.yaml new file mode 100644 index 0000000..ff8e97d --- /dev/null +++ b/tests/cassettes/test_health.yaml @@ -0,0 +1,30 @@ +interactions: +- request: + body: null + headers: + Accept: ['*/*'] + Accept-Encoding: ['gzip, deflate'] + Authorization: [Bearer __MASTODON_PY_TEST_ACCESS_TOKEN] + Connection: [keep-alive] + User-Agent: [python-requests/2.18.4] + method: GET + uri: http://localhost:3000/health + response: + body: {string: success} + headers: + Cache-Control: ['max-age=0, private, must-revalidate'] + Content-Type: [text/plain; charset=utf-8] + Last-Modified: ['Sat, 12 Oct 2019 18:23:49 GMT'] + Referrer-Policy: [strict-origin-when-cross-origin] + Transfer-Encoding: [chunked] + Vary: [Accept-Encoding] + X-Content-Type-Options: [nosniff] + X-Download-Options: [noopen] + X-Frame-Options: [SAMEORIGIN] + X-Permitted-Cross-Domain-Policies: [none] + X-Request-Id: [13fff315-deaa-47c3-9138-d61d02c5063e] + X-Runtime: ['0.311003'] + X-XSS-Protection: [1; mode=block] + content-length: ['7'] + status: {code: 200, message: OK} +version: 1 diff --git a/tests/test_instance.py b/tests/test_instance.py index 0eebc57..a304585 100644 --- a/tests/test_instance.py +++ b/tests/test_instance.py @@ -32,3 +32,7 @@ def test_low_version(api_low_version): @pytest.mark.vcr() def test_emoji(api): assert len(api.custom_emojis()) == 0 + +@pytest.mark.vcr() +def test_health(api): + assert api.instance_health() == True -- cgit v1.2.3