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/ready command #120

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion aptly_api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def do_get(self, urlpath: str, params: Optional[Dict[str, str]] = None) -> reque
resp = requests.get(self._make_url(urlpath), params=params, verify=self.ssl_verify,
cert=self.ssl_cert, auth=self.http_auth, timeout=self.timeout)

if resp.status_code < 200 or resp.status_code >= 300:
if resp.status_code < 200 or resp.status_code >= 300 and resp.status_code != 503:
raise AptlyAPIException(self._error_from_response(resp), status_code=resp.status_code)

return resp
Expand Down
22 changes: 22 additions & 0 deletions aptly_api/parts/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from typing import cast

import requests

from aptly_api.base import AptlyAPIException, BaseAPIClient


Expand All @@ -18,3 +20,23 @@ 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_clear_404(self) -> requests.Response:
try:
return self.do_get("api/ready")
except AptlyAPIException as error:
# This is needed to hide the exception masking the 404 error
if error.status_code == 404:
raise NotImplementedError("The Ready API is not yet supported") from error
# 503 is needed by api/ready for returning its unready condition
if error.status_code not in {200, 503}:
raise AptlyAPIException("Aptly server returned an unexpected status_code " + str(error.status_code)) from error
raise

def ready(self) -> str:
resp = self._do_get_clear_404()

if "Status" not in resp.json():
raise AptlyAPIException("Aptly server didn't return a valid response object:\n%s" % resp.text)

return cast(str, resp.json()["Status"])
6 changes: 6 additions & 0 deletions aptly_api/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ def test_error_get(self, *, rmock: requests_mock.Mocker) -> None:
with self.assertRaises(AptlyAPIException):
self.client.files.do_get("mock://test/api")

@requests_mock.Mocker(kw='rmock')
def test_error_get_api_ready_503_code(self, *, rmock: requests_mock.Mocker) -> None:
rmock.register_uri("GET", "mock://test/api/ready", status_code=503, text='{"Status":"Aptly is unavailable"}')
self.assertEqual(self.client.files.do_get("mock://test/api/ready").status_code, 503)
self.assertEqual(self.client.files.do_get("mock://test/api/ready").json()["Status"], "Aptly is unavailable")

@requests_mock.Mocker(kw='rmock')
def test_error_post(self, *, rmock: requests_mock.Mocker) -> None:
rmock.register_uri("POST", "mock://test/api", status_code=400, text='[{"error": "error", "meta": "meta"}]',
Expand Down
23 changes: 23 additions & 0 deletions aptly_api/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,26 @@ 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_ready(self, *, rmock: requests_mock.Mocker) -> None:
rmock.get("http://test/api/ready", text='{"Status":"Aptly is ready"}')
self.assertEqual(self.mapi.ready(), "Aptly is ready")

def test_ready_not_ready(self, *, rmock: requests_mock.Mocker) -> None:
rmock.register_uri("GET", "http://test/api/ready", status_code=503, text='{"Status":"Aptly is unavailable"}')
self.assertEqual(self.mapi.ready(), "Aptly is unavailable")

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

def test_ready_error_bad_status_code(self, *, rmock: requests_mock.Mocker) -> None:
rmock.register_uri("GET", "http://test/api/ready", status_code=999, text='{"Status":"Aptly is ready"}')
with self.assertRaises(AptlyAPIException):
self.mapi.ready()

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