Skip to content

Standardize health method #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion meilisearch/client.py
Original file line number Diff line number Diff line change
@@ -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():
"""
Expand Down Expand Up @@ -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.

Expand Down
12 changes: 12 additions & 0 deletions meilisearch/tests/client/test_client_health_meilisearch.py
Original file line number Diff line number Diff line change
@@ -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