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

ui: use new Session wrapper in UI calls

with igwn-auth-utils 1.0.0 auth handling
parent 45d4ffa9
No related branches found
No related tags found
1 merge request!86Draft: Provide custom Session wrapper with default token_scope
......@@ -12,7 +12,7 @@ Build-Depends:
pybuild-plugin-pyproject,
python3-all,
python3-argparse-manpage,
python3-igwn-auth-utils (>= 0.3.1),
python3-igwn-auth-utils (>= 1.0.0),
python3-igwn-segments,
python3-pytest (>= 2.8.0),
python3-requests-mock,
......@@ -25,7 +25,7 @@ Architecture: all
Depends:
${misc:Depends},
${python3:Depends},
python3-igwn-auth-utils (>= 0.3.1),
python3-igwn-auth-utils (>= 1.0.0),
python3-igwn-segments,
Description: GW data discovery Python 3 library
The DataFind service allows users to query for the location of
......
......@@ -62,12 +62,12 @@ def gwdatafind_server_env():
@yield_fixture(autouse=True, scope="module")
def noauth():
"""Force the underlying _get() function to use no authentication.
"""Force the underlying get_json() function to use no authentication.
So that the tests don't fall over if the test runner has bad creds.
"""
_get_noauth = partial(ui._get, cert=False, token=False)
with mock.patch("gwdatafind.ui._get", _get_noauth):
_get_noauth = partial(ui.get_json, cert=False, token=False)
with mock.patch("gwdatafind.ui.get_json", _get_noauth):
yield
......@@ -90,7 +90,7 @@ def test_url_scheme_handling(in_, url):
def test_ping(requests_mock):
requests_mock.get(_url(api.ping_path()), status_code=200)
requests_mock.get(_url(api.ping_path()), status_code=200, json=[])
ui.ping()
......
......@@ -22,16 +22,14 @@ referred to in usage as ``gwdatafind.<function>`` and not
``gwdatafind.ui.<function>``
"""
from functools import wraps
from re import compile as compile_regex
from urllib.parse import urlparse
from warnings import warn
import igwn_segments as segments
import requests
from igwn_auth_utils.requests import get as _get
from . import api
from .requests import Session
from .utils import (
file_segment,
get_default_host,
......@@ -110,23 +108,7 @@ def connect(host=None, port=None): # pragma: no cover
# -- new methods --------------------------------
@wraps(_get)
def get(url, *args, **kwargs):
if url.startswith("http://") and requests.__version__ < "2.15.0":
# workaround https://github.com/psf/requests/issues/4025
kwargs.setdefault("cert", False)
scheme, netloc = urlparse(url)[:2]
host = netloc.split(":", 1)[0] # remove the port
kwargs.setdefault("token_audience", list({
f"{scheme}://{netloc}",
f"{scheme}://{host}",
"ANY",
}))
kwargs.setdefault("token_scope", "gwdatafind.read")
return _get(url, *args, **kwargs)
def get_json(*args, **kwargs):
def get_json(*args, session=None, **kwargs):
"""Perform a GET request and return JSON.
Parameters
......@@ -145,7 +127,11 @@ def get_json(*args, **kwargs):
igwn_auth_utils.requests.get
for information on how the request is performed
"""
response = get(*args, **kwargs)
if session:
response = session.get(*args, **kwargs)
else:
with Session() as sess:
response = sess.get(*args, **kwargs)
response.raise_for_status()
return response.json()
......@@ -222,8 +208,7 @@ def ping(host=None, ext=api.DEFAULT_EXT, session=None, **request_kw):
if the request fails for any reason
"""
qurl = _url(host, api.ping_path, ext=ext)
response = get(qurl, session=session, **request_kw)
response.raise_for_status()
return get_json(qurl, session=session, **request_kw)
def find_observatories(
......
......@@ -35,7 +35,7 @@ classifiers = [
# requirements
requires-python = ">=3.6"
dependencies = [
"igwn-auth-utils >=0.3.1",
"igwn-auth-utils >=1.0.0",
"igwn-segments",
]
dynamic = [
......
......@@ -23,7 +23,7 @@ BuildRequires: python3dist(wheel)
# man pages
BuildRequires: python3dist(argparse-manpage)
BuildRequires: python3dist(igwn-auth-utils) >= 0.3.1
BuildRequires: python3dist(igwn-auth-utils) >= 1.0.0
BuildRequires: python3dist(igwn-segments)
# testing dependencies
......@@ -61,7 +61,7 @@ libraries.
%package -n python3-%{srcname}
Summary: Python %{python3_version} library for the GWDataFind service
Requires: python3dist(igwn-auth-utils) >= 0.3.1
Requires: python3dist(igwn-auth-utils) >= 1.0.0
Requires: python3dist(igwn-segments)
%description -n python3-%{srcname}
The DataFind service allows users to query for the location of
......@@ -92,7 +92,7 @@ url = %{url}
packages = find:
python_requires = >=3.6
install_requires =
igwn-auth-utils >= 0.3.1
igwn-auth-utils >= 1.0.0
igwn-segments >= 2.0.0
[options.entry_points]
console_scripts =
......
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