11from __future__ import annotations
22
33import 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
76from 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