Skip to content

Commit 7ba4994

Browse files
authored
Merge pull request astropy#3526 from cds-astro/add-user-agent-in-basevoquery
feat: add extra_user_agents to BaseVOQuery
2 parents 87fd3af + ca66a07 commit 7ba4994

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ Infrastructure, Utility and Other Changes and Additions
119119

120120
- Versions of numpy <1.22 are no longer supported. [#3504]
121121

122+
- ``BaseVOQuery`` now accepts a ``extra_user_agents`` parameter to allow the addition
123+
of user agents on top of astroquery's ones [#3526]
124+
122125

123126
utils.tap
124127
^^^^^^^^^

astroquery/query.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,14 @@ class BaseVOQuery:
183183
Use in modules that rely on PyVO, either on its own or in combination with ``BaseQuery`` (be mindful
184184
about resolution order of base classes!).
185185
"""
186-
def __init__(self):
186+
def __init__(self, *, extra_user_agents=None):
187+
"""Create an instance of BaseVOQuery.
188+
189+
Parameters
190+
----------
191+
extra_user_agents : str | list(str)
192+
Extra user agents to be added to the ones already provided by astroquery.
193+
"""
187194
super().__init__()
188195
if not hasattr(self, '_session'):
189196
# We don't want to override another, e.g. already authenticated session from another baseclass
@@ -199,6 +206,12 @@ def __init__(self):
199206
user_agents = [f"astroquery/{version.version} pyVO/{pyvo.__version__} "
200207
f"Python/{platform.python_version()} ({platform.system()})"] + user_agents
201208

209+
if extra_user_agents:
210+
if isinstance(extra_user_agents, str):
211+
user_agents += [extra_user_agents]
212+
else:
213+
user_agents += extra_user_agents
214+
202215
self._session.headers['User-Agent'] = " ".join(user_agents)
203216

204217
self.name = self.__class__.__name__.split("Class")[0]

astroquery/tests/test_query.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,21 @@ def test_download_file_remote_large(self, base_query, tmp_path, head_safe):
332332

333333
def test_session_VO_header():
334334
"""Test that the session header includes both astroquery and pyVO."""
335-
test_instance = with_VO()
335+
test_instance = with_VO(extra_user_agents="test")
336336
user_agent = test_instance._session.headers['User-Agent']
337337
assert 'astroquery' in user_agent
338338
assert 'pyVO' in user_agent
339+
assert user_agent.endswith("test")
339340
assert user_agent.count('astroquery') == 1
340341

341342

343+
def test_session_only_VO_header():
344+
"""Test that we can add extra user agents."""
345+
test_instance = only_VO(extra_user_agents=["agent1", "agent2"])
346+
user_agent = test_instance._session.headers['User-Agent']
347+
assert all([string in user_agent for string in ["astroquery", "pyVO", "agent1", "agent2"]])
348+
349+
342350
def test_session_nonVO_header():
343351
"""Test that the session header includes astroquery but not pyVO."""
344352
test_instance = without_VO()

0 commit comments

Comments
 (0)