-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_utils.py
More file actions
67 lines (48 loc) · 2.14 KB
/
client_utils.py
File metadata and controls
67 lines (48 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import re
from typing import Any
from urllib.parse import SplitResult, urlencode, urlsplit, urlunparse
from mpt_api_client.http.query_options import QueryOptions
INVALID_ENV_URL_MESSAGE = (
"Base URL is required. "
"Set it up as env variable MPT_URL or pass it as `base_url` "
"argument to MPTClient. Expected format scheme://host[:port]"
)
PATHS_TO_REMOVE_RE = re.compile(r"^/$|^/public/?$|^/public/v1/?$")
def _format_host(hostname: str | None) -> str:
if not hostname or not isinstance(hostname, str):
raise ValueError(INVALID_ENV_URL_MESSAGE)
return f"[{hostname}]" if ":" in hostname else hostname
def _format_port(split_result: SplitResult) -> str:
try:
parsed_port = split_result.port
except ValueError as exc:
raise ValueError(INVALID_ENV_URL_MESSAGE) from exc
return f":{parsed_port}" if parsed_port else ""
def _sanitize_path(path: str) -> str:
return PATHS_TO_REMOVE_RE.sub("", path)
def _build_sanitized_base_url(split_result: SplitResult) -> str:
host = _format_host(split_result.hostname)
port = _format_port(split_result)
path = _sanitize_path(split_result.path)
return str(urlunparse((split_result.scheme, f"{host}{port}", path, "", "", "")))
def validate_base_url(base_url: str | None) -> str:
"""Validate base url."""
if not base_url or not isinstance(base_url, str):
raise ValueError(INVALID_ENV_URL_MESSAGE)
split_result = urlsplit(base_url, scheme="https")
if not split_result.scheme or not split_result.hostname:
raise ValueError(INVALID_ENV_URL_MESSAGE)
return _build_sanitized_base_url(split_result)
def get_query_params(
query_params: dict[str, Any] | None, options: QueryOptions | None = None
) -> str:
"""Get query params string from dict."""
filtered_params = {
query_param: query_value
for query_param, query_value in (query_params or {}).items()
if query_value is not None
}
query_params_str = urlencode(filtered_params) if filtered_params else ""
if options and options.render:
query_params_str += "&render()" if query_params_str else "render()"
return query_params_str