-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path__init__.py
More file actions
163 lines (122 loc) · 5.08 KB
/
__init__.py
File metadata and controls
163 lines (122 loc) · 5.08 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import copy
import os
import sys
from contextvars import ContextVar, Token
from dataclasses import dataclass
from typing import Any, Dict, Literal, Optional, overload
import truststore
from typing_extensions import TypedDict
from .cli_constants import CLI_COMMANDS
truststore.inject_into_ssl()
__all__ = ["poly"]
if len(sys.argv) > 1 and sys.argv[1] not in CLI_COMMANDS:
currdir = os.path.dirname(os.path.abspath(__file__))
if not os.path.isdir(os.path.join(currdir, "poly")):
print("No 'poly' found. Please run 'python3 -m polyapi generate' to generate the 'poly' library for your tenant.")
sys.exit(1)
class PolyCustomDict(TypedDict, total=False):
"""Type definition for polyCustom dictionary."""
executionId: Optional[str] # Read-only unless explicitly unlocked
executionApiKey: Optional[str]
userSessionId: Optional[str]
responseStatusCode: Optional[int]
responseContentType: Optional[str]
responseHeaders: Dict[str, Any]
@dataclass
class _PolyCustomState:
internal_store: Dict[str, Any]
execution_id_locked: bool = False
class PolyCustom:
def __init__(self) -> None:
object.__setattr__(
self,
"_default_store",
{
"executionId": None,
"executionApiKey": None,
"userSessionId": None,
"responseStatusCode": 200,
"responseContentType": None,
"responseHeaders": {},
},
)
object.__setattr__(self, "_state_var", ContextVar("_poly_custom_state", default=None))
def _make_state(self) -> _PolyCustomState:
return _PolyCustomState(internal_store=copy.deepcopy(self._default_store))
def _get_state(self) -> _PolyCustomState:
state = self._state_var.get()
if state is None:
state = self._make_state()
self._state_var.set(state)
return state
def push_scope(self, initial_values: Optional[Dict[str, Any]] = None) -> Token:
state = self._make_state()
if initial_values:
state.internal_store.update(copy.deepcopy(initial_values))
if state.internal_store.get("executionId") is not None:
state.execution_id_locked = True
return self._state_var.set(state)
def pop_scope(self, token: Token) -> None:
self._state_var.reset(token)
def set_once(self, key: str, value: Any) -> None:
state = self._get_state()
if key == "executionId" and state.execution_id_locked:
return
state.internal_store[key] = value
if key == "executionId":
state.execution_id_locked = True
def get(self, key: str, default: Any = None) -> Any:
return self._get_state().internal_store.get(key, default)
def lock_execution_id(self) -> None:
self._get_state().execution_id_locked = True
def unlock_execution_id(self) -> None:
self._get_state().execution_id_locked = False
@overload
def __getitem__(self, key: Literal["executionId"]) -> Optional[str]: ...
@overload
def __getitem__(self, key: Literal["executionApiKey"]) -> Optional[str]: ...
@overload
def __getitem__(self, key: Literal["userSessionId"]) -> Optional[str]: ...
@overload
def __getitem__(self, key: Literal["responseStatusCode"]) -> Optional[int]: ...
@overload
def __getitem__(self, key: Literal["responseContentType"]) -> Optional[str]: ...
@overload
def __getitem__(self, key: Literal["responseHeaders"]) -> Dict[str, Any]: ...
def __getitem__(self, key: str) -> Any:
return self.get(key)
@overload
def __setitem__(self, key: Literal["executionApiKey"], value: Optional[str]) -> None: ...
@overload
def __setitem__(self, key: Literal["userSessionId"], value: Optional[str]) -> None: ...
@overload
def __setitem__(self, key: Literal["responseStatusCode"], value: Optional[int]) -> None: ...
@overload
def __setitem__(self, key: Literal["responseContentType"], value: Optional[str]) -> None: ...
@overload
def __setitem__(self, key: Literal["responseHeaders"], value: Dict[str, Any]) -> None: ...
def __setitem__(self, key: str, value: Any) -> None:
self.set_once(key, value)
def __getattr__(self, key: str) -> Any:
if key in self._default_store:
return self.get(key)
raise AttributeError(f"{type(self).__name__!r} object has no attribute {key!r}")
def __setattr__(self, key: str, value: Any) -> None:
if key.startswith("_"):
object.__setattr__(self, key, value)
return
self.set_once(key, value)
def __repr__(self) -> str:
return f"PolyCustom({self._get_state().internal_store})"
def copy(self) -> "PolyCustom":
new = PolyCustom()
state = self._get_state()
new._state_var.set(
_PolyCustomState(
internal_store=copy.deepcopy(state.internal_store),
execution_id_locked=state.execution_id_locked,
)
)
return new
_PolyCustom = PolyCustom
polyCustom: PolyCustom = PolyCustom()