diff --git a/aptly_api/parts/misc.py b/aptly_api/parts/misc.py index a1f21b1..07a6d7a 100644 --- a/aptly_api/parts/misc.py +++ b/aptly_api/parts/misc.py @@ -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"]) diff --git a/aptly_api/tests/test_misc.py b/aptly_api/tests/test_misc.py index 18040a2..123a0fe 100644 --- a/aptly_api/tests/test_misc.py +++ b/aptly_api/tests/test_misc.py @@ -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()