Skip to content

Commit 8625982

Browse files
committed
Refactor server url helper functions
1 parent f21ac18 commit 8625982

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

src/unstructured_client/_hooks/custom/clean_server_url_hook.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,44 @@ def clean_server_url(base_url: str) -> str:
1212

1313
if not base_url:
1414
return ""
15-
# -- add a url scheme if not present (urllib.parse does not work reliably without it)
15+
16+
# add a url scheme if not present (urllib.parse does not work reliably without it)
1617
if "http" not in base_url:
1718
base_url = "http://" + base_url
1819

1920
parsed_url: ParseResult = urlparse(base_url)
2021

21-
if "api.unstructuredapp.io" in base_url:
22+
unstructured_services = [
23+
"api.unstructuredapp.io",
24+
"api.unstructured.io",
25+
"platform.unstructuredapp.io",
26+
]
27+
if parsed_url.netloc in unstructured_services:
2228
if parsed_url.scheme != "https":
2329
parsed_url = parsed_url._replace(scheme="https")
2430

25-
# -- path should always be empty
26-
return urlunparse(parsed_url._replace(path=""))
31+
# We only want the base url
32+
return urlunparse(parsed_url._replace(path="", params="", query="", fragment=""))
2733

2834

29-
def choose_server_url(endpoint_url, client_url, default_endpoint_url) -> str:
35+
def choose_server_url(endpoint_url: str, client_url: str, default_endpoint_url: str) -> str:
3036
"""
3137
Helper function to fix a breaking change in the SDK past 0.30.0.
3238
When we merged the partition and platform specs, the client server_url stopped working,
3339
and users need to pass it in the endpoint function.
3440
For now, call this helper in the generated code to set the correct url.
35-
"""
3641
37-
# First, see if the endpoint has a url:
38-
# s.general.partition(server_url=...)
39-
if endpoint_url is not None:
40-
url = endpoint_url
42+
Order of choices:
43+
Endpoint server_url -> s.general.partition(server_url=...)
44+
(Passed in as None if not set)
4145
42-
# Next, try the base client url:
43-
# s = UnstructuredClient(server_url=...)
44-
# (If not set it's an empty string)
45-
elif client_url != "":
46-
url = client_url
46+
Base client server_url -> s = UnstructuredClient(server_url=...)
47+
(Passed as empty string if not set)
4748
48-
# Finally, take the url defined in the spec:
49-
# operations.PARTITION_SERVERS[...]
50-
else:
51-
url = default_endpoint_url
49+
Default endpoint URL as defined in the spec
50+
"""
5251

53-
# Make sure we drop the path if it's provided anywhere
54-
# (The endpoint url will be set after we've done the init hooks)
52+
url = endpoint_url if endpoint_url is not None else (client_url or default_endpoint_url)
5553
return clean_server_url(url)
5654

5755

0 commit comments

Comments
 (0)