Skip to content
Snippets Groups Projects
Verified Commit 6bd6e958 authored by Duncan Macleod's avatar Duncan Macleod
Browse files

main: update --ping to print server version

for API v1+ servers
parent ea7053ef
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !88. Comments created here will be created in the context of that merge request.
......@@ -327,12 +327,15 @@ def ping(args, out):
exitcode : `int` or `None`
the return value of the action or `None` to indicate success.
"""
ui.ping(
ext=args.extension,
resp = ui.ping(
host=args.server,
api=args.api,
)
print(f"LDRDataFindServer at {args.server} is alive", file=out)
if "version" in resp:
msg = f"GWDataFind Server v{resp['version']} at {args.server} is alive"
else:
msg = f"LDRDataFindServer at {args.server} is alive"
print(msg, file=out)
def show_observatories(args, out):
......
......@@ -115,8 +115,12 @@ def test_sanity_check_fail(clargs):
parser.parse_args(clargs)
@mock.patch("gwdatafind.ui.ping")
def test_ping(mping):
def test_ping(requests_mock):
"""Test the ``--ping`` command-line action."""
requests_mock.get(
"https://test.datafind.com:443/api/version",
json={"version": "1.2.3"},
)
args = argparse.Namespace(
server="test.datafind.com:443",
api="v1",
......@@ -124,14 +128,10 @@ def test_ping(mping):
)
out = StringIO()
main.ping(args, out)
mping.assert_called_with(
host=args.server,
api=args.api,
ext=args.extension,
)
out.seek(0)
assert out.read().rstrip() == (
"LDRDataFindServer at test.datafind.com:443 is alive")
"GWDataFind Server v1.2.3 at test.datafind.com:443 is alive"
)
@mock.patch("gwdatafind.ui.find_observatories")
......
......@@ -95,8 +95,14 @@ def test_url_scheme_handling(in_, url):
def test_ping(requests_mock):
requests_mock.get(_url(api.ping_path()), status_code=200)
ui.ping(api=TEST_API)
"""Test `gwdatafind.ui.ping`."""
requests_mock.get(
_url(api.ping_path()),
status_code=200,
json={"api_versions": ["v1", "ldr"], "version": "1.2.3"}
)
response = ui.ping(api=TEST_API)
assert response["version"] == "1.2.3"
@pytest.mark.parametrize(("match", "result"), (
......
......@@ -177,7 +177,6 @@ def _url(
def ping(
host=None,
api=DEFAULT_API,
ext=DEFAULT_EXT,
session=None,
**request_kw,
):
......@@ -193,9 +192,6 @@ def ping(
api : `str`, optional
The API version to use.
ext : `str`, optional
the file extension for which to search.
session : `requests.Session`, optional
the connection session to use; if not given, a
:class:`igwn_auth_utils.requests.Session` will be
......@@ -212,14 +208,19 @@ def ping(
request_kw
other keywords are passed to :func:`igwn_auth_utils.get`
Returns
-------
version_info : `dict`
The information returned from the `/api/version` API endpoint.
For LDR-era servers this returns an empty `list`.
Raises
------
requests.RequestException
if the request fails for any reason
"""
qurl = _url(host, api, "ping_path")
response = get(qurl, session=session, **request_kw)
response.raise_for_status()
return get_json(qurl, session=session, **request_kw)
def find_observatories(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment