|
18 | 18 |
|
19 | 19 |
|
20 | 20 | 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): |
26 | 22 | 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 | + |
30 | 29 | if not endpoint:
|
31 | 30 | raise AttributeError('endpoint required')
|
32 |
| - ep_urls = [ |
33 |
| - '{baseurl}/{endpoint}'.format(baseurl=curated_baseurl, |
34 |
| - endpoint=endpoint), |
35 |
| - url_sibling(curated_baseurl, endpoint) |
36 |
| - ] |
37 | 31 |
|
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): |
39 | 36 | try:
|
40 | 37 | response = self._session.get(ep_url, stream=True)
|
41 | 38 | response.raise_for_status()
|
42 | 39 | break
|
43 | 40 | except requests.RequestException:
|
44 | 41 | continue
|
45 | 42 | else:
|
46 |
| - raise DALServiceError( |
47 |
| - "No working {endpoint} endpoint provided".format( |
48 |
| - endpoint=endpoint)) |
| 43 | + raise DALServiceError(f"No working {endpoint} endpoint provided") |
49 | 44 |
|
50 | 45 | return response.raw
|
51 | 46 |
|
@@ -145,6 +140,7 @@ class VOSITables:
|
145 | 140 | Access to table names is like accessing dictionary keys. using iterator
|
146 | 141 | syntax or `keys()`
|
147 | 142 | """
|
| 143 | + |
148 | 144 | def __init__(self, vosi_tables, endpoint_url, *, session=None):
|
149 | 145 | self._vosi_tables = vosi_tables
|
150 | 146 | self._endpoint_url = endpoint_url
|
|
0 commit comments