From 6e6738c80940753b3b86e97dd3559ab7b4588f25 Mon Sep 17 00:00:00 2001 From: ChuanmingMao Date: Tue, 6 Aug 2024 00:06:35 +0200 Subject: [PATCH 1/4] include the port number if present --- pyvo/dal/vosi.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pyvo/dal/vosi.py b/pyvo/dal/vosi.py index 0cdbd99bc..48938464b 100644 --- a/pyvo/dal/vosi.py +++ b/pyvo/dal/vosi.py @@ -24,8 +24,12 @@ def _get_endpoint(self, endpoint): # do not trust baseurl as it might contain query or fragments urlcomp = urlparse(self.baseurl) + # Include the port number if present + netloc = urlcomp.hostname + if urlcomp.port: + netloc += f':{urlcomp.port}' curated_baseurl = '{}://{}{}'.format(urlcomp.scheme, - urlcomp.hostname, + netloc, urlcomp.path) if not endpoint: raise AttributeError('endpoint required') From 18dd6e915233c5ae15e07edfc94924fae594467e Mon Sep 17 00:00:00 2001 From: ChuanmingMao Date: Tue, 1 Oct 2024 19:48:39 +0200 Subject: [PATCH 2/4] split get_endpoint function; added test code --- pyvo/dal/tests/test_tap.py | 11 +++++++++++ pyvo/dal/vosi.py | 23 +++++++++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/pyvo/dal/tests/test_tap.py b/pyvo/dal/tests/test_tap.py index 4f93eaeec..106fb7b31 100644 --- a/pyvo/dal/tests/test_tap.py +++ b/pyvo/dal/tests/test_tap.py @@ -790,3 +790,14 @@ def test_missing_udf(self, tapservice): def test_get_udf(self, tapservice): func = tapservice.get_tap_capability().get_adql().get_udf("IVO_hasword") # case insensitive! assert func.form == "ivo_hasword(haystack TEXT, needle TEXT) -> INTEGER" + +def test_get_endpoint_candidates(): + # Directly instantiate the TAPService with a known base URL + svc = pyvo.dal.TAPService("http://astroweb.projects.phys.ucl.ac.uk:8000/tap") + + # Check if the correct endpoint candidates are generated + expected_urls = [ + "http://astroweb.projects.phys.ucl.ac.uk:8000/tap/capabilities", + "http://astroweb.projects.phys.ucl.ac.uk:8000/capabilities" + ] + assert svc._get_endpoint_candidates("capabilities") == expected_urls diff --git a/pyvo/dal/vosi.py b/pyvo/dal/vosi.py index 48938464b..891343594 100644 --- a/pyvo/dal/vosi.py +++ b/pyvo/dal/vosi.py @@ -18,28 +18,24 @@ class EndpointMixin(): - def _get_endpoint(self, endpoint): - # finds the endpoint relative to the base url or its parent - # and returns its content in raw format - - # do not trust baseurl as it might contain query or fragments + def _get_endpoint_candidates(self, endpoint): urlcomp = urlparse(self.baseurl) # Include the port number if present netloc = urlcomp.hostname if urlcomp.port: netloc += f':{urlcomp.port}' - curated_baseurl = '{}://{}{}'.format(urlcomp.scheme, - netloc, - urlcomp.path) + curated_baseurl = '{}://{}{}'.format(urlcomp.scheme, netloc, urlcomp.path) + if not endpoint: raise AttributeError('endpoint required') - ep_urls = [ - '{baseurl}/{endpoint}'.format(baseurl=curated_baseurl, - endpoint=endpoint), + + return [ + '{baseurl}/{endpoint}'.format(baseurl=curated_baseurl, endpoint=endpoint), url_sibling(curated_baseurl, endpoint) ] - for ep_url in ep_urls: + def _get_endpoint(self, endpoint): + for ep_url in self._get_endpoint_candidates(endpoint): try: response = self._session.get(ep_url, stream=True) response.raise_for_status() @@ -48,8 +44,7 @@ def _get_endpoint(self, endpoint): continue else: raise DALServiceError( - "No working {endpoint} endpoint provided".format( - endpoint=endpoint)) + f"No working {endpoint} endpoint provided") return response.raw From 7fe6884bdc94c3e86d599959542f8b407506ec22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brigitta=20Sip=C5=91cz?= Date: Tue, 1 Oct 2024 11:06:14 -0700 Subject: [PATCH 3/4] Fix namespace --- pyvo/dal/tests/test_tap.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyvo/dal/tests/test_tap.py b/pyvo/dal/tests/test_tap.py index 106fb7b31..65741a76c 100644 --- a/pyvo/dal/tests/test_tap.py +++ b/pyvo/dal/tests/test_tap.py @@ -793,7 +793,7 @@ def test_get_udf(self, tapservice): def test_get_endpoint_candidates(): # Directly instantiate the TAPService with a known base URL - svc = pyvo.dal.TAPService("http://astroweb.projects.phys.ucl.ac.uk:8000/tap") + svc = TAPService("http://astroweb.projects.phys.ucl.ac.uk:8000/tap") # Check if the correct endpoint candidates are generated expected_urls = [ From 2026a5f5035ae694a546ca02efb2c269d57e75ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brigitta=20Sip=C5=91cz?= Date: Tue, 1 Oct 2024 12:02:15 -0700 Subject: [PATCH 4/4] MAINT: fix codestyle and add changelog --- CHANGES.rst | 2 ++ pyvo/dal/tests/test_tap.py | 3 ++- pyvo/dal/vosi.py | 13 +++++-------- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index e2665e2b8..b9cad21e7 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -70,6 +70,8 @@ Deprecations and Removals - Adding ``session`` kwarg to allow to pass a session along when turning an Interface into a service via ``Interface.to_service``. [#590] +- Include port number if it is present in endpoint access URL. [#582] + 1.5.2 (2024-05-22) ================== diff --git a/pyvo/dal/tests/test_tap.py b/pyvo/dal/tests/test_tap.py index 65741a76c..0c22dc840 100644 --- a/pyvo/dal/tests/test_tap.py +++ b/pyvo/dal/tests/test_tap.py @@ -791,10 +791,11 @@ def test_get_udf(self, tapservice): func = tapservice.get_tap_capability().get_adql().get_udf("IVO_hasword") # case insensitive! assert func.form == "ivo_hasword(haystack TEXT, needle TEXT) -> INTEGER" + def test_get_endpoint_candidates(): # Directly instantiate the TAPService with a known base URL svc = TAPService("http://astroweb.projects.phys.ucl.ac.uk:8000/tap") - + # Check if the correct endpoint candidates are generated expected_urls = [ "http://astroweb.projects.phys.ucl.ac.uk:8000/tap/capabilities", diff --git a/pyvo/dal/vosi.py b/pyvo/dal/vosi.py index 891343594..6b7805f47 100644 --- a/pyvo/dal/vosi.py +++ b/pyvo/dal/vosi.py @@ -24,15 +24,12 @@ def _get_endpoint_candidates(self, endpoint): netloc = urlcomp.hostname if urlcomp.port: netloc += f':{urlcomp.port}' - curated_baseurl = '{}://{}{}'.format(urlcomp.scheme, netloc, urlcomp.path) + curated_baseurl = f'{urlcomp.scheme}://{netloc}{urlcomp.path}' if not endpoint: raise AttributeError('endpoint required') - - return [ - '{baseurl}/{endpoint}'.format(baseurl=curated_baseurl, endpoint=endpoint), - url_sibling(curated_baseurl, endpoint) - ] + + return [f'{curated_baseurl}/{endpoint}', url_sibling(curated_baseurl, endpoint)] def _get_endpoint(self, endpoint): for ep_url in self._get_endpoint_candidates(endpoint): @@ -43,8 +40,7 @@ def _get_endpoint(self, endpoint): except requests.RequestException: continue else: - raise DALServiceError( - f"No working {endpoint} endpoint provided") + raise DALServiceError(f"No working {endpoint} endpoint provided") return response.raw @@ -144,6 +140,7 @@ class VOSITables: Access to table names is like accessing dictionary keys. using iterator syntax or `keys()` """ + def __init__(self, vosi_tables, endpoint_url, *, session=None): self._vosi_tables = vosi_tables self._endpoint_url = endpoint_url