Skip to content

Commit 18b0f1a

Browse files
authored
Merge pull request #582 from chmmao/fix-endpoint
BUG: Include port number if present
2 parents 759742e + 2026a5f commit 18b0f1a

File tree

3 files changed

+27
-17
lines changed

3 files changed

+27
-17
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ Deprecations and Removals
7070
- Adding ``session`` kwarg to allow to pass a session along when turning
7171
an Interface into a service via ``Interface.to_service``. [#590]
7272

73+
- Include port number if it is present in endpoint access URL. [#582]
74+
7375

7476
1.5.2 (2024-05-22)
7577
==================

pyvo/dal/tests/test_tap.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,3 +790,15 @@ def test_missing_udf(self, tapservice):
790790
def test_get_udf(self, tapservice):
791791
func = tapservice.get_tap_capability().get_adql().get_udf("IVO_hasword") # case insensitive!
792792
assert func.form == "ivo_hasword(haystack TEXT, needle TEXT) -> INTEGER"
793+
794+
795+
def test_get_endpoint_candidates():
796+
# Directly instantiate the TAPService with a known base URL
797+
svc = TAPService("http://astroweb.projects.phys.ucl.ac.uk:8000/tap")
798+
799+
# Check if the correct endpoint candidates are generated
800+
expected_urls = [
801+
"http://astroweb.projects.phys.ucl.ac.uk:8000/tap/capabilities",
802+
"http://astroweb.projects.phys.ucl.ac.uk:8000/capabilities"
803+
]
804+
assert svc._get_endpoint_candidates("capabilities") == expected_urls

pyvo/dal/vosi.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,29 @@
1818

1919

2020
class EndpointMixin():
21-
def _get_endpoint(self, endpoint):
22-
# finds the endpoint relative to the base url or its parent
23-
# and returns its content in raw format
24-
25-
# do not trust baseurl as it might contain query or fragments
21+
def _get_endpoint_candidates(self, endpoint):
2622
urlcomp = urlparse(self.baseurl)
27-
curated_baseurl = '{}://{}{}'.format(urlcomp.scheme,
28-
urlcomp.hostname,
29-
urlcomp.path)
23+
# Include the port number if present
24+
netloc = urlcomp.hostname
25+
if urlcomp.port:
26+
netloc += f':{urlcomp.port}'
27+
curated_baseurl = f'{urlcomp.scheme}://{netloc}{urlcomp.path}'
28+
3029
if not endpoint:
3130
raise AttributeError('endpoint required')
32-
ep_urls = [
33-
'{baseurl}/{endpoint}'.format(baseurl=curated_baseurl,
34-
endpoint=endpoint),
35-
url_sibling(curated_baseurl, endpoint)
36-
]
3731

38-
for ep_url in ep_urls:
32+
return [f'{curated_baseurl}/{endpoint}', url_sibling(curated_baseurl, endpoint)]
33+
34+
def _get_endpoint(self, endpoint):
35+
for ep_url in self._get_endpoint_candidates(endpoint):
3936
try:
4037
response = self._session.get(ep_url, stream=True)
4138
response.raise_for_status()
4239
break
4340
except requests.RequestException:
4441
continue
4542
else:
46-
raise DALServiceError(
47-
"No working {endpoint} endpoint provided".format(
48-
endpoint=endpoint))
43+
raise DALServiceError(f"No working {endpoint} endpoint provided")
4944

5045
return response.raw
5146

@@ -145,6 +140,7 @@ class VOSITables:
145140
Access to table names is like accessing dictionary keys. using iterator
146141
syntax or `keys()`
147142
"""
143+
148144
def __init__(self, vosi_tables, endpoint_url, *, session=None):
149145
self._vosi_tables = vosi_tables
150146
self._endpoint_url = endpoint_url

0 commit comments

Comments
 (0)