From 775d86b1fd40c7e7773cb151595885dcb4b1fc7b Mon Sep 17 00:00:00 2001 From: alallema Date: Tue, 13 Apr 2021 14:03:22 +0200 Subject: [PATCH] Adding is_healthy method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Clémentine Urquizar --- meilisearch/client.py | 17 ++++++++++++++++- .../client/test_client_health_meilisearch.py | 12 ++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/meilisearch/client.py b/meilisearch/client.py index 4fcbf9e8..0a961be3 100644 --- a/meilisearch/client.py +++ b/meilisearch/client.py @@ -1,7 +1,7 @@ from meilisearch.index import Index from meilisearch.config import Config from meilisearch._httprequests import HttpRequests -from meilisearch.errors import MeiliSearchApiError +from meilisearch.errors import MeiliSearchApiError, MeiliSearchError class Client(): """ @@ -161,6 +161,21 @@ def health(self): """ return self.http.get(self.config.paths.health) + def is_healthy(self): + """Get health of the MeiliSearch server. + + `200` HTTP status response when MeiliSearch is healthy. + + Return + ------ + health: True | False + """ + try: + self.health() + except MeiliSearchError: + return False + return True + def get_keys(self): """Get all keys. diff --git a/meilisearch/tests/client/test_client_health_meilisearch.py b/meilisearch/tests/client/test_client_health_meilisearch.py index ebc2a96c..8d0ee703 100644 --- a/meilisearch/tests/client/test_client_health_meilisearch.py +++ b/meilisearch/tests/client/test_client_health_meilisearch.py @@ -1,5 +1,17 @@ +import meilisearch def test_health(client): """Tests checking the health of the MeiliSearch instance.""" response = client.health() assert response['status'] == 'available' + +def test_is_healthy(client): + """Tests checking if is_healthy return true when MeiliSearch instance is available.""" + response = client.is_healthy() + assert response is True + +def test_is_healthy_bad_route(): + """Tests checking if is_healthy returns false when trying to reach a bad URL.""" + client = meilisearch.Client("http://wrongurl:1234") + response = client.is_healthy() + assert response is False