Skip to content
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

Added api/healthy command #121

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions aptly_api/parts/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,21 @@ def version(self) -> str:
return cast(str, resp.json()["Version"])
else:
raise AptlyAPIException("Aptly server didn't return a valid response object:\n%s" % resp.text)

def _do_get_healthy_clear_404(self) -> requests.Response:
try:
return self.do_get("api/healthy")
except AptlyAPIException as error:
if error.status_code == HTTP_CODE_404:
msg = "The Healthy API is not yet supported"
raise NotImplementedError(msg) from error
raise

def healthy(self) -> str:
resp = self._do_get_healthy_clear_404()

if "Status" not in resp.json():
msg = f"Aptly server didn't return a valid response object:\n{resp.text}"
raise AptlyAPIException(msg)

return cast(str, resp.json()["Status"])
14 changes: 14 additions & 0 deletions aptly_api/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,17 @@ def test_version_error(self, *, rmock: requests_mock.Mocker) -> None:
rmock.get("http://test/api/version", text='{"droenk": "blah"}')
with self.assertRaises(AptlyAPIException):
self.mapi.version()

def test_healthy(self, *, rmock: requests_mock.Mocker) -> None:
rmock.get("http://test/api/healthy", text='{"Status":"Aptly is healthy"}')
self.assertEqual(self.mapi.healthy(), "Aptly is healthy")

def test_healthy_error(self, *, rmock: requests_mock.Mocker) -> None:
rmock.get("http://test/api/healthy", text='{"dronek": "blah"}')
with self.assertRaises(AptlyAPIException):
self.mapi.healthy()

def test_healthy_aptly_to_old(self, *, rmock: requests_mock.Mocker) -> None:
rmock.register_uri("GET", "http://test/api/healthy", status_code=404, text="Not Found")
with pytest.raises(NotImplementedError):
self.mapi.healthy()