-
Notifications
You must be signed in to change notification settings - Fork 856
Expand file tree
/
Copy pathinternal_utils.py
More file actions
145 lines (122 loc) · 4.66 KB
/
internal_utils.py
File metadata and controls
145 lines (122 loc) · 4.66 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import copy
import logging
import re
from typing import Dict, Callable
from typing import Union, Optional, Any
from urllib.parse import quote
from .default_arg import DefaultArg, NotGiven
from slack_sdk.web.internal_utils import get_user_agent
def _build_query(params: Optional[Dict[str, Any]]) -> str:
if params is not None and len(params) > 0:
return "&".join({f"{quote(str(k))}={quote(str(v))}" for k, v in params.items() if v is not None})
return ""
def _is_iterable(obj: Union[Optional[Any], DefaultArg]) -> bool:
return obj is not None and obj is not NotGiven
def _to_dict_without_not_given(obj: Any) -> dict:
dict_value = {}
given_dict = obj if isinstance(obj, dict) else vars(obj)
for key, value in given_dict.items():
if key == "unknown_fields":
if value is not None:
converted = _to_dict_without_not_given(value)
dict_value.update(converted)
continue
dict_key = _to_camel_case_key(key)
if value is NotGiven:
continue
if isinstance(value, list):
dict_value[dict_key] = [elem.to_dict() if hasattr(elem, "to_dict") else elem for elem in value]
elif isinstance(value, dict):
dict_value[dict_key] = _to_dict_without_not_given(value)
else:
dict_value[dict_key] = value.to_dict() if hasattr(value, "to_dict") else value
return dict_value
def _create_copy(original: Any) -> Any:
return copy.deepcopy(original)
def _to_camel_case_key(key: str) -> str:
next_to_capital = False
result = ""
for c in key:
if c == "_":
next_to_capital = True
elif next_to_capital:
result += c.upper()
next_to_capital = False
else:
result += c
return result
def _to_snake_cased(original: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
return _convert_dict_keys(
original,
{},
lambda s: re.sub(
"^_",
"",
"".join(["_" + c.lower() if c.isupper() else c for c in s]),
),
)
def _to_camel_cased(original: Optional[Dict[str, Any]]) -> Optional[Dict[str, Any]]:
return _convert_dict_keys(
original,
{},
_to_camel_case_key,
)
def _convert_dict_keys(
original_dict: Optional[Dict[str, Any]],
result_dict: Dict[str, Any],
convert: Callable[[str], str],
) -> Optional[Dict[str, Any]]:
if original_dict is None:
return result_dict
for original_key, original_value in original_dict.items():
new_key = convert(original_key)
if isinstance(original_value, dict):
result_dict[new_key] = {}
new_value = _convert_dict_keys(original_value, result_dict[new_key], convert)
result_dict[new_key] = new_value
elif isinstance(original_value, list):
result_dict[new_key] = []
is_dict = len(original_value) > 0 and isinstance(original_value[0], dict)
for element in original_value:
if is_dict:
if isinstance(element, dict):
new_element = {}
for elem_key, elem_value in element.items():
new_element[convert(elem_key)] = (
_convert_dict_keys(elem_value, {}, convert)
if isinstance(elem_value, dict)
else _create_copy(elem_value)
)
result_dict[new_key].append(new_element)
else:
result_dict[new_key].append(_create_copy(original_value))
else:
result_dict[new_key] = _create_copy(original_value)
return result_dict
def _build_request_headers(
token: str,
default_headers: Dict[str, str],
additional_headers: Optional[Dict[str, str]],
) -> Dict[str, str]:
request_headers = {
"Content-Type": "application/json;charset=utf-8",
"Authorization": f"Bearer {token}",
}
if default_headers is None or "User-Agent" not in default_headers:
request_headers["User-Agent"] = get_user_agent()
if default_headers is not None:
request_headers.update(default_headers)
if additional_headers is not None:
request_headers.update(additional_headers)
return request_headers
def _debug_log_response(
logger,
resp: "SCIMResponse", # noqa: F821
) -> None:
if logger.level <= logging.DEBUG:
logger.debug(
"Received the following response - "
f"status: {resp.status_code}, "
f"headers: {(dict(resp.headers))}, "
f"body: {resp.raw_body}"
)