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: ...@@ -12,7 +12,7 @@ Build-Depends:
pybuild-plugin-pyproject, pybuild-plugin-pyproject,
python3-all, python3-all,
python3-argparse-manpage, python3-argparse-manpage,
python3-igwn-auth-utils (>= 0.3.1), python3-igwn-auth-utils (>= 1.0.0),
python3-igwn-segments, python3-igwn-segments,
python3-pytest (>= 2.8.0), python3-pytest (>= 2.8.0),
python3-requests-mock, python3-requests-mock,
...@@ -25,7 +25,7 @@ Architecture: all ...@@ -25,7 +25,7 @@ Architecture: all
Depends: Depends:
${misc:Depends}, ${misc:Depends},
${python3:Depends}, ${python3:Depends},
python3-igwn-auth-utils (>= 0.3.1), python3-igwn-auth-utils (>= 1.0.0),
python3-igwn-segments, python3-igwn-segments,
Description: GW data discovery Python 3 library Description: GW data discovery Python 3 library
The DataFind service allows users to query for the location of The DataFind service allows users to query for the location of
......
...@@ -62,12 +62,12 @@ def gwdatafind_server_env(): ...@@ -62,12 +62,12 @@ def gwdatafind_server_env():
@yield_fixture(autouse=True, scope="module") @yield_fixture(autouse=True, scope="module")
def noauth(): 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. So that the tests don't fall over if the test runner has bad creds.
""" """
_get_noauth = partial(ui._get, cert=False, token=False) _get_noauth = partial(ui.get_json, cert=False, token=False)
with mock.patch("gwdatafind.ui._get", _get_noauth): with mock.patch("gwdatafind.ui.get_json", _get_noauth):
yield yield
...@@ -90,7 +90,7 @@ def test_url_scheme_handling(in_, url): ...@@ -90,7 +90,7 @@ def test_url_scheme_handling(in_, url):
def test_ping(requests_mock): 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() ui.ping()
......
...@@ -22,16 +22,14 @@ referred to in usage as ``gwdatafind.<function>`` and not ...@@ -22,16 +22,14 @@ referred to in usage as ``gwdatafind.<function>`` and not
``gwdatafind.ui.<function>`` ``gwdatafind.ui.<function>``
""" """
from functools import wraps
from re import compile as compile_regex from re import compile as compile_regex
from urllib.parse import urlparse from urllib.parse import urlparse
from warnings import warn from warnings import warn
import igwn_segments as segments import igwn_segments as segments
import requests
from igwn_auth_utils.requests import get as _get
from . import api from . import api
from .requests import Session
from .utils import ( from .utils import (
file_segment, file_segment,
get_default_host, get_default_host,
...@@ -110,23 +108,7 @@ def connect(host=None, port=None): # pragma: no cover ...@@ -110,23 +108,7 @@ def connect(host=None, port=None): # pragma: no cover
# -- new methods -------------------------------- # -- new methods --------------------------------
@wraps(_get) def get_json(*args, session=None, **kwargs):
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):
"""Perform a GET request and return JSON. """Perform a GET request and return JSON.
Parameters Parameters
...@@ -145,7 +127,11 @@ def get_json(*args, **kwargs): ...@@ -145,7 +127,11 @@ def get_json(*args, **kwargs):
igwn_auth_utils.requests.get igwn_auth_utils.requests.get
for information on how the request is performed 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() response.raise_for_status()
return response.json() return response.json()
...@@ -222,8 +208,7 @@ def ping(host=None, ext=api.DEFAULT_EXT, session=None, **request_kw): ...@@ -222,8 +208,7 @@ def ping(host=None, ext=api.DEFAULT_EXT, session=None, **request_kw):
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.ping_path, ext=ext)
response = get(qurl, session=session, **request_kw) return get_json(qurl, session=session, **request_kw)
response.raise_for_status()
def find_observatories( def find_observatories(
......
...@@ -35,7 +35,7 @@ classifiers = [ ...@@ -35,7 +35,7 @@ classifiers = [
# requirements # requirements
requires-python = ">=3.6" requires-python = ">=3.6"
dependencies = [ dependencies = [
"igwn-auth-utils >=0.3.1", "igwn-auth-utils >=1.0.0",
"igwn-segments", "igwn-segments",
] ]
dynamic = [ dynamic = [
......
...@@ -23,7 +23,7 @@ BuildRequires: python3dist(wheel) ...@@ -23,7 +23,7 @@ BuildRequires: python3dist(wheel)
# man pages # man pages
BuildRequires: python3dist(argparse-manpage) BuildRequires: python3dist(argparse-manpage)
BuildRequires: python3dist(igwn-auth-utils) >= 0.3.1 BuildRequires: python3dist(igwn-auth-utils) >= 1.0.0
BuildRequires: python3dist(igwn-segments) BuildRequires: python3dist(igwn-segments)
# testing dependencies # testing dependencies
...@@ -61,7 +61,7 @@ libraries. ...@@ -61,7 +61,7 @@ libraries.
%package -n python3-%{srcname} %package -n python3-%{srcname}
Summary: Python %{python3_version} library for the GWDataFind service 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) Requires: python3dist(igwn-segments)
%description -n python3-%{srcname} %description -n python3-%{srcname}
The DataFind service allows users to query for the location of The DataFind service allows users to query for the location of
...@@ -92,7 +92,7 @@ url = %{url} ...@@ -92,7 +92,7 @@ url = %{url}
packages = find: packages = find:
python_requires = >=3.6 python_requires = >=3.6
install_requires = install_requires =
igwn-auth-utils >= 0.3.1 igwn-auth-utils >= 1.0.0
igwn-segments >= 2.0.0 igwn-segments >= 2.0.0
[options.entry_points] [options.entry_points]
console_scripts = 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