Skip to content
Snippets Groups Projects
Verified Commit 43179def authored by Duncan Macleod's avatar Duncan Macleod Committed by Duncan Macleod
Browse files

api.ldr: move default API to LDR

in preparation for supporting multiple APIs
parent 98048619
No related branches found
No related tags found
1 merge request!88Draft: Support multiple APIs
This commit is part of merge request !88. Comments created here will be created in the context of that merge request.
# Copyright (C) 2025 Cardiff University
#
# This file is part of GWDataFind.
#
# GWDataFind is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GWDataFind is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GWDataFind. If not, see <http://www.gnu.org/licenses/>.
"""API definitions for the GWDataFind Client."""
# list of APIs
APIS = (
"ldr",
)
# Copyright (C) 2025 Cardiff University
#
# This file is part of GWDataFind.
#
# GWDataFind is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GWDataFind is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GWDataFind. If not, see <http://www.gnu.org/licenses/>.
"""Common API definitions for the GWDataFind Client.
This module provides API definitions that all APIs support in the
same way.
"""
def version_path():
"""Path for the API version endpoint."""
return "api/version"
File moved
...@@ -23,19 +23,19 @@ from the v1 API for gwdatfind_server. ...@@ -23,19 +23,19 @@ from the v1 API for gwdatfind_server.
import pytest import pytest
from gwdatafind import api from gwdatafind.api import ldr as api_ldr
__author__ = "Duncan Macleod <duncan.macleod@ligo.org>" __author__ = "Duncan Macleod <duncan.macleod@ligo.org>"
def test_ping_path(): def test_ping_path():
"""Test `ping_path()`.""" """Test `ping_path()`."""
assert api.ping_path() == "LDR/services/data/v1/gwf/H/R/1,2" assert api_ldr.ping_path() == "LDR/services/data/v1/gwf/H/R/1,2"
def test_find_observatories_path(): def test_find_observatories_path():
"""Test `find_observatories_path()`.""" """Test `find_observatories_path()`."""
assert api.find_observatories_path() == "LDR/services/data/v1/gwf.json" assert api_ldr.find_observatories_path() == "LDR/services/data/v1/gwf.json"
@pytest.mark.parametrize(("site", "result"), [ @pytest.mark.parametrize(("site", "result"), [
...@@ -45,19 +45,19 @@ def test_find_observatories_path(): ...@@ -45,19 +45,19 @@ def test_find_observatories_path():
]) ])
def test_find_types_path(site, result): def test_find_types_path(site, result):
"""Test `find_types_path()`.""" """Test `find_types_path()`."""
assert api.find_types_path(site) == result assert api_ldr.find_types_path(site) == result
def test_find_times_path(): def test_find_times_path():
"""Test `find_times_path()`.""" """Test `find_times_path()`."""
assert api.find_times_path("X", "TEST", 0, 1) == ( assert api_ldr.find_times_path("X", "TEST", 0, 1) == (
"LDR/services/data/v1/gwf/X/TEST/segments/0,1.json" "LDR/services/data/v1/gwf/X/TEST/segments/0,1.json"
) )
def test_find_url_path(): def test_find_url_path():
"""Test `find_url_path()`.""" """Test `find_url_path()`."""
assert api.find_url_path("/data/X-TEST-0-1.gwf") == ( assert api_ldr.find_url_path("/data/X-TEST-0-1.gwf") == (
"LDR/services/data/v1/gwf/X/TEST/X-TEST-0-1.gwf.json" "LDR/services/data/v1/gwf/X/TEST/X-TEST-0-1.gwf.json"
) )
...@@ -68,7 +68,7 @@ def test_find_url_path(): ...@@ -68,7 +68,7 @@ def test_find_url_path():
]) ])
def test_find_latest_path(urltype, result): def test_find_latest_path(urltype, result):
"""Test `find_latest_path()`.""" """Test `find_latest_path()`."""
assert api.find_latest_path("X", "TEST", urltype) == result assert api_ldr.find_latest_path("X", "TEST", urltype) == result
@pytest.mark.parametrize(("urltype", "match", "result"), [ @pytest.mark.parametrize(("urltype", "match", "result"), [
...@@ -80,7 +80,7 @@ def test_find_latest_path(urltype, result): ...@@ -80,7 +80,7 @@ def test_find_latest_path(urltype, result):
]) ])
def find_urls_path(urltype, match, result): def find_urls_path(urltype, match, result):
"""Test `find_urls_path()`.""" """Test `find_urls_path()`."""
assert api.find_urls_path( assert api_ldr.find_urls_path(
"X", "X",
"TEST", "TEST",
0, 0,
......
...@@ -29,10 +29,8 @@ from unittest import mock ...@@ -29,10 +29,8 @@ from unittest import mock
import igwn_segments as segments import igwn_segments as segments
import pytest import pytest
from gwdatafind import ( from gwdatafind import ui
api, from gwdatafind.api import ldr as api_ldr
ui,
)
__author__ = "Duncan Macleod <duncan.macleod@ligo.org>" __author__ = "Duncan Macleod <duncan.macleod@ligo.org>"
...@@ -137,7 +135,7 @@ def test_url_scheme_handling(in_, url): ...@@ -137,7 +135,7 @@ def test_url_scheme_handling(in_, url):
def test_ping(requests_mock): def test_ping(requests_mock):
"""Test `ping()`.""" """Test `ping()`."""
requests_mock.get(_url(api.ping_path()), status_code=200) requests_mock.get(_url(api_ldr.ping_path()), status_code=200)
ui.ping() ui.ping()
...@@ -148,7 +146,7 @@ def test_ping(requests_mock): ...@@ -148,7 +146,7 @@ def test_ping(requests_mock):
def test_find_observatories(match, result, requests_mock): def test_find_observatories(match, result, requests_mock):
"""Test `find_observatories()`.""" """Test `find_observatories()`."""
requests_mock.get( requests_mock.get(
_url(api.find_observatories_path()), _url(api_ldr.find_observatories_path()),
json=list(TEST_DATA), json=list(TEST_DATA),
) )
assert ui.find_observatories(match=match) == list(set(result)) assert ui.find_observatories(match=match) == list(set(result))
...@@ -181,7 +179,7 @@ def test_find_types(site, match, result, requests_mock): ...@@ -181,7 +179,7 @@ def test_find_types(site, match, result, requests_mock):
else: else:
respdata = [ft for site in TEST_DATA for ft in TEST_DATA[site]] respdata = [ft for site in TEST_DATA for ft in TEST_DATA[site]]
requests_mock.get( requests_mock.get(
_url(api.find_types_path(site=site)), _url(api_ldr.find_types_path(site=site)),
json=respdata, json=respdata,
) )
assert ui.find_types( assert ui.find_types(
...@@ -195,7 +193,7 @@ def test_find_times(requests_mock): ...@@ -195,7 +193,7 @@ def test_find_times(requests_mock):
site = "A" site = "A"
frametype = "A1_TEST" frametype = "A1_TEST"
requests_mock.get( requests_mock.get(
_url(api.find_times_path(site, frametype, 1, 100)), _url(api_ldr.find_times_path(site, frametype, 1, 100)),
json=TEST_DATA[site][frametype], json=TEST_DATA[site][frametype],
) )
assert ui.find_times(site, frametype, 1, 100) == segments.segmentlist([ assert ui.find_times(site, frametype, 1, 100) == segments.segmentlist([
...@@ -212,7 +210,7 @@ def test_find_url(requests_mock): ...@@ -212,7 +210,7 @@ def test_find_url(requests_mock):
"gsiftp://localhost:2811/data/A/A1_TEST/A-A1_TEST-0-1.gwf", "gsiftp://localhost:2811/data/A/A1_TEST/A-A1_TEST-0-1.gwf",
] ]
requests_mock.get( requests_mock.get(
_url(api.find_url_path("A-A1_TEST-0-1.gwf")), _url(api_ldr.find_url_path("A-A1_TEST-0-1.gwf")),
json=urls, json=urls,
) )
assert ui.find_url("/my/data/A-A1_TEST-0-1.gwf") == urls[:1] assert ui.find_url("/my/data/A-A1_TEST-0-1.gwf") == urls[:1]
...@@ -249,7 +247,7 @@ def test_find_url_on_missing(requests_mock, on_missing, ctx): ...@@ -249,7 +247,7 @@ def test_find_url_on_missing(requests_mock, on_missing, ctx):
"""Test `find_url` handling of missing data.""" """Test `find_url` handling of missing data."""
# mock the request # mock the request
requests_mock.get( requests_mock.get(
_url(api.find_url_path("A-A1_TEST-0-1.gwf")), _url(api_ldr.find_url_path("A-A1_TEST-0-1.gwf")),
json=[], json=[],
) )
...@@ -269,7 +267,7 @@ def test_find_latest(requests_mock): ...@@ -269,7 +267,7 @@ def test_find_latest(requests_mock):
"gsiftp://localhost:2811/data/A/A1_TEST/A-A1_TEST-0-1.gwf", "gsiftp://localhost:2811/data/A/A1_TEST/A-A1_TEST-0-1.gwf",
] ]
requests_mock.get( requests_mock.get(
_url(api.find_latest_path("A", "A1_TEST", "file")), _url(api_ldr.find_latest_path("A", "A1_TEST", "file")),
json=urls[:1], json=urls[:1],
) )
assert ui.find_latest("A", "A1_TEST") == urls[:1] assert ui.find_latest("A", "A1_TEST") == urls[:1]
...@@ -284,7 +282,7 @@ def test_find_urls(requests_mock): ...@@ -284,7 +282,7 @@ def test_find_urls(requests_mock):
"""Test `find_urls()`.""" """Test `find_urls()`."""
urls = list(map(_file_url, TEST_DATA["A"]["A1_TEST"][:2])) urls = list(map(_file_url, TEST_DATA["A"]["A1_TEST"][:2]))
requests_mock.get( requests_mock.get(
_url(api.find_urls_path("A", "A1_TEST", 0, 20, "file")), _url(api_ldr.find_urls_path("A", "A1_TEST", 0, 20, "file")),
json=urls, json=urls,
) )
assert ui.find_urls("A", "A1_TEST", 0, 20, on_gaps="error") == urls assert ui.find_urls("A", "A1_TEST", 0, 20, on_gaps="error") == urls
...@@ -317,7 +315,7 @@ def test_find_urls_on_gaps(requests_mock, on_gaps, ctx): ...@@ -317,7 +315,7 @@ def test_find_urls_on_gaps(requests_mock, on_gaps, ctx):
# configure the mock request # configure the mock request
urls = list(map(_file_url, TEST_DATA["A"]["A1_TEST"])) urls = list(map(_file_url, TEST_DATA["A"]["A1_TEST"]))
requests_mock.get( requests_mock.get(
_url(api.find_urls_path("A", "A1_TEST", 0, 100, "file")), _url(api_ldr.find_urls_path("A", "A1_TEST", 0, 100, "file")),
json=urls, json=urls,
) )
......
...@@ -31,7 +31,7 @@ import igwn_segments as segments ...@@ -31,7 +31,7 @@ import igwn_segments as segments
import requests import requests
from igwn_auth_utils.requests import get as _get from igwn_auth_utils.requests import get as _get
from . import api from .api import ldr as api_ldr
from .utils import ( from .utils import (
file_segment, file_segment,
get_default_host, get_default_host,
...@@ -127,7 +127,7 @@ def _url(host, api_func, *args, **kwargs): ...@@ -127,7 +127,7 @@ def _url(host, api_func, *args, **kwargs):
return f"{host.rstrip('/')}/{path}" return f"{host.rstrip('/')}/{path}"
def ping(host=None, ext=api.DEFAULT_EXT, session=None, **request_kw): def ping(host=None, ext=api_ldr.DEFAULT_EXT, session=None, **request_kw):
"""Ping the GWDataFind host to test for life. """Ping the GWDataFind host to test for life.
Parameters Parameters
...@@ -161,7 +161,7 @@ def ping(host=None, ext=api.DEFAULT_EXT, session=None, **request_kw): ...@@ -161,7 +161,7 @@ def ping(host=None, ext=api.DEFAULT_EXT, session=None, **request_kw):
requests.RequestException requests.RequestException
if the request fails for any reason if the request fails for any reason
""" """
qurl = _url(host, api.ping_path, ext=ext) qurl = _url(host, api_ldr.ping_path, ext=ext)
response = get(qurl, session=session, **request_kw) response = get(qurl, session=session, **request_kw)
response.raise_for_status() response.raise_for_status()
...@@ -169,7 +169,7 @@ def ping(host=None, ext=api.DEFAULT_EXT, session=None, **request_kw): ...@@ -169,7 +169,7 @@ def ping(host=None, ext=api.DEFAULT_EXT, session=None, **request_kw):
def find_observatories( def find_observatories(
match=None, match=None,
host=None, host=None,
ext=api.DEFAULT_EXT, ext=api_ldr.DEFAULT_EXT,
session=None, session=None,
**request_kw, **request_kw,
): ):
...@@ -222,7 +222,7 @@ def find_observatories( ...@@ -222,7 +222,7 @@ def find_observatories(
>>> find_observatories(match="H", host="datafind.gwosc.org") >>> find_observatories(match="H", host="datafind.gwosc.org")
['H'] ['H']
""" """
qurl = _url(host, api.find_observatories_path, ext=ext) qurl = _url(host, api_ldr.find_observatories_path, ext=ext)
sites = set(get_json(qurl, session=session, **request_kw)) sites = set(get_json(qurl, session=session, **request_kw))
if match: if match:
match = compile_regex(match).search match = compile_regex(match).search
...@@ -234,7 +234,7 @@ def find_types( ...@@ -234,7 +234,7 @@ def find_types(
site=None, site=None,
match=None, match=None,
host=None, host=None,
ext=api.DEFAULT_EXT, ext=api_ldr.DEFAULT_EXT,
session=None, session=None,
**request_kw, **request_kw,
): ):
...@@ -292,7 +292,7 @@ def find_types( ...@@ -292,7 +292,7 @@ def find_types(
(accurate as of Nov 18 2021) (accurate as of Nov 18 2021)
""" # noqa: E501 """ # noqa: E501
qurl = _url(host, api.find_types_path, site=site, ext=ext) qurl = _url(host, api_ldr.find_types_path, site=site, ext=ext)
types = set(get_json(qurl, session=session, **request_kw)) types = set(get_json(qurl, session=session, **request_kw))
if match: if match:
match = compile_regex(match).search match = compile_regex(match).search
...@@ -306,7 +306,7 @@ def find_times( ...@@ -306,7 +306,7 @@ def find_times(
gpsstart=None, gpsstart=None,
gpsend=None, gpsend=None,
host=None, host=None,
ext=api.DEFAULT_EXT, ext=api_ldr.DEFAULT_EXT,
session=None, session=None,
**request_kw, **request_kw,
): ):
...@@ -377,7 +377,7 @@ def find_times( ...@@ -377,7 +377,7 @@ def find_times(
""" # noqa: E501 """ # noqa: E501
qurl = _url( qurl = _url(
host, host,
api.find_times_path, api_ldr.find_times_path,
site, site,
frametype, frametype,
gpsstart, gpsstart,
...@@ -465,7 +465,7 @@ def find_url( ...@@ -465,7 +465,7 @@ def find_url(
RuntimeError RuntimeError
if no matching URLs are found and ``on_missing="error"`` was given if no matching URLs are found and ``on_missing="error"`` was given
""" """
qurl = _url(host, api.find_url_path, framefile) qurl = _url(host, api_ldr.find_url_path, framefile)
return _get_urls( return _get_urls(
qurl, qurl,
scheme=urltype, scheme=urltype,
...@@ -481,7 +481,7 @@ def find_latest( ...@@ -481,7 +481,7 @@ def find_latest(
urltype="file", urltype="file",
on_missing="error", on_missing="error",
host=None, host=None,
ext=api.DEFAULT_EXT, ext=api_ldr.DEFAULT_EXT,
session=None, session=None,
**request_kw, **request_kw,
): ):
...@@ -549,7 +549,7 @@ def find_latest( ...@@ -549,7 +549,7 @@ def find_latest(
""" # noqa: E501 """ # noqa: E501
qurl = _url( qurl = _url(
host, host,
api.find_latest_path, api_ldr.find_latest_path,
site, site,
frametype, frametype,
ext=ext, ext=ext,
...@@ -567,7 +567,7 @@ def find_urls( ...@@ -567,7 +567,7 @@ def find_urls(
urltype="file", urltype="file",
on_gaps="warn", on_gaps="warn",
host=None, host=None,
ext=api.DEFAULT_EXT, ext=api_ldr.DEFAULT_EXT,
session=None, session=None,
**request_kw, **request_kw,
): ):
...@@ -635,7 +635,7 @@ def find_urls( ...@@ -635,7 +635,7 @@ def find_urls(
""" """
qurl = _url( qurl = _url(
host, host,
api.find_urls_path, api_ldr.find_urls_path,
site, site,
frametype, frametype,
gpsstart, gpsstart,
......
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