From 6bd6e958dd852f38e14dcc79c5127461315a21a1 Mon Sep 17 00:00:00 2001
From: Duncan Macleod <macleoddm@cardiff.ac.uk>
Date: Fri, 7 Feb 2025 16:24:01 +0000
Subject: [PATCH] main: update --ping to print server version

for API v1+ servers
---
 gwdatafind/__main__.py        |  9 ++++++---
 gwdatafind/tests/test_main.py | 16 ++++++++--------
 gwdatafind/tests/test_ui.py   | 10 ++++++++--
 gwdatafind/ui.py              | 13 +++++++------
 4 files changed, 29 insertions(+), 19 deletions(-)

diff --git a/gwdatafind/__main__.py b/gwdatafind/__main__.py
index ceb6df8..570e8ed 100644
--- a/gwdatafind/__main__.py
+++ b/gwdatafind/__main__.py
@@ -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):
diff --git a/gwdatafind/tests/test_main.py b/gwdatafind/tests/test_main.py
index 60e7b0b..914080f 100644
--- a/gwdatafind/tests/test_main.py
+++ b/gwdatafind/tests/test_main.py
@@ -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")
diff --git a/gwdatafind/tests/test_ui.py b/gwdatafind/tests/test_ui.py
index 1b7e86d..c93cb9f 100644
--- a/gwdatafind/tests/test_ui.py
+++ b/gwdatafind/tests/test_ui.py
@@ -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"), (
diff --git a/gwdatafind/ui.py b/gwdatafind/ui.py
index 6b1cc5d..09c6728 100644
--- a/gwdatafind/ui.py
+++ b/gwdatafind/ui.py
@@ -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(
-- 
GitLab