Skip to content

Commit d3f618a

Browse files
committed
refactor
1 parent 681cb76 commit d3f618a

File tree

2 files changed

+14
-28
lines changed

2 files changed

+14
-28
lines changed

_test_unstructured_client/test_check_url_protocol.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,9 @@ def test_clean_server_url_on_paid_api_url(server_url: str):
3434
("server_url"),
3535
[
3636
("http://localhost:8000"), # correct url
37-
("https://localhost:8000"),
3837
("localhost:8000"),
3938
("localhost:8000/general/v0/general"),
40-
("https://localhost:8000/general/v0/general"),
39+
("http://localhost:8000/general/v0/general"),
4140
]
4241
)
4342
def test_clean_server_url_on_localhost(server_url: str):
Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from __future__ import annotations
22

33
import functools
4-
import inspect
5-
from typing import Any, Callable, Dict, Optional
6-
from typing_extensions import ParamSpec, Concatenate
4+
from typing import cast, Callable, Optional
5+
from typing_extensions import ParamSpec
76
from urllib.parse import urlparse, urlunparse, ParseResult
87

98

@@ -14,21 +13,14 @@ def clean_server_url(func: Callable[_P, None]) -> Callable[_P, None]:
1413

1514
@functools.wraps(func)
1615
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> None:
16+
SERVER_URL_ARG_IDX = 3
17+
url_is_in_kwargs = True
1718

18-
def get_call_args_applying_defaults() -> Dict[str, Any]:
19-
"""Map both explicit and default arguments of decorated func call by param name."""
20-
sig = inspect.signature(func)
21-
call_args: Dict[str, Any] = dict(
22-
**dict(zip(sig.parameters, args)), **kwargs
23-
)
24-
for param in sig.parameters.values():
25-
if param.name not in call_args and param.default is not param.empty:
26-
call_args[param.name] = param.default
27-
return call_args
19+
server_url: Optional[str] = cast(Optional[str], kwargs.get("server_url"))
2820

29-
call_args = get_call_args_applying_defaults()
30-
31-
server_url: Optional[str] = call_args.get("server_url")
21+
if server_url is None and len(args) > SERVER_URL_ARG_IDX:
22+
server_url = cast(str, args[SERVER_URL_ARG_IDX])
23+
url_is_in_kwargs = False
3224

3325
if server_url:
3426
# -- add a url scheme if not present (urllib.parse does not work reliably without it)
@@ -40,20 +32,15 @@ def get_call_args_applying_defaults() -> Dict[str, Any]:
4032
if "api.unstructuredapp.io" in server_url:
4133
if parsed_url.scheme != "https":
4234
parsed_url = parsed_url._replace(scheme="https")
43-
else:
44-
# -- if not a paid api url, assume the api is hosted locally and the scheme is "http"
45-
if parsed_url.scheme != "http":
46-
parsed_url = parsed_url._replace(scheme="http")
4735

4836
# -- path should always be empty
4937
cleaned_url = parsed_url._replace(path="")
50-
call_args["server_url"] = urlunparse(cleaned_url)
5138

52-
# -- call_args contains all args and kwargs. If users define some parameters using
53-
# -- kwargs, param definitions would be duplicated. Pass only the `self`
54-
# -- param as an arg and keep the rest in kwargs to prevent duplicates.
55-
self_arg = (call_args.pop("self"),)
39+
if url_is_in_kwargs:
40+
kwargs["server_url"] = urlunparse(cleaned_url)
41+
else:
42+
args = args[:SERVER_URL_ARG_IDX] + (urlunparse(cleaned_url),) + args[SERVER_URL_ARG_IDX+1:] # type: ignore
5643

57-
return func(*self_arg, **call_args) # type: ignore
44+
return func(*args, **kwargs)
5845

5946
return wrapper

0 commit comments

Comments
 (0)