Debian Codegraph @

src/auth0/management/core/client_wrapper.py

raw symbols SCIP
1 # This file was auto-generated by Fern from our API Definition.
2 # Modified by Auth0 to use Auth0 telemetry format with dynamic versioning
3
4 import base64
5 import platform
6 import sys
7 import typing
8 from json import dumps
9
10 import httpx
11 from .http_client import AsyncHttpClient, HttpClient
12 from .logging import LogConfig, Logger
13
14
15 class BaseClientWrapper:
16 def __init__(
17 self,
18 *,
19 token: typing.Union[str, typing.Callable[[], str]],
20 headers: typing.Optional[typing.Dict[str, str]] = None,
21 base_url: str,
22 timeout: typing.Optional[float] = None,
23 max_retries: int = 2,
24 stream_reconnection_enabled: typing.Optional[bool] = None,
25 max_stream_reconnection_attempts: typing.Optional[int] = None,
26 logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
27 ):
28 self._token = token
29 self._headers = headers
30 self._base_url = base_url
31 self._timeout = timeout
32 self._max_retries = max_retries
33 self._stream_reconnection_enabled = stream_reconnection_enabled
34 self._max_stream_reconnection_attempts = max_stream_reconnection_attempts
35 self._logging = logging
36
37 def get_headers(self) -> typing.Dict[str, str]:
38 py_version = platform.python_version()
39 version = sys.modules["auth0"].__version__
40
41 auth0_client = dumps({
42 "name": "auth0-python",
43 "version": version,
44 "env": {"python": py_version}
45 }).encode("utf-8")
46
47 headers: typing.Dict[str, str] = {
48 "User-Agent": f"Python/{py_version}",
49 "Auth0-Client": base64.b64encode(auth0_client).decode(),
50 **(self.get_custom_headers() or {}),
51 }
52 headers["Authorization"] = f"Bearer {self._get_token()}"
53 return headers
54
55 def _get_token(self) -> str:
56 if isinstance(self._token, str):
57 return self._token
58 else:
59 return self._token()
60
61 def get_custom_headers(self) -> typing.Optional[typing.Dict[str, str]]:
62 return self._headers
63
64 def get_base_url(self) -> str:
65 return self._base_url
66
67 def get_timeout(self) -> typing.Optional[float]:
68 return self._timeout
69
70 def get_max_retries(self) -> int:
71 return self._max_retries
72
73 def get_stream_reconnection_enabled(self) -> bool:
74 return self._stream_reconnection_enabled if self._stream_reconnection_enabled is not None else True
75
76 def get_max_stream_reconnection_attempts(self) -> typing.Optional[int]:
77 return self._max_stream_reconnection_attempts
78
79
80 class SyncClientWrapper(BaseClientWrapper):
81 def __init__(
82 self,
83 *,
84 token: typing.Union[str, typing.Callable[[], str]],
85 headers: typing.Optional[typing.Dict[str, str]] = None,
86 base_url: str,
87 timeout: typing.Optional[float] = None,
88 max_retries: int = 2,
89 stream_reconnection_enabled: typing.Optional[bool] = None,
90 max_stream_reconnection_attempts: typing.Optional[int] = None,
91 logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
92 httpx_client: httpx.Client,
93 ):
94 super().__init__(
95 token=token,
96 headers=headers,
97 base_url=base_url,
98 timeout=timeout,
99 max_retries=max_retries,
100 stream_reconnection_enabled=stream_reconnection_enabled,
101 max_stream_reconnection_attempts=max_stream_reconnection_attempts,
102 logging=logging,
103 )
104 self.httpx_client = HttpClient(
105 httpx_client=httpx_client,
106 base_headers=self.get_headers,
107 base_timeout=self.get_timeout,
108 base_url=self.get_base_url,
109 base_max_retries=self.get_max_retries(),
110 logging_config=self._logging,
111 )
112
113
114 class AsyncClientWrapper(BaseClientWrapper):
115 def __init__(
116 self,
117 *,
118 token: typing.Union[str, typing.Callable[[], str]],
119 headers: typing.Optional[typing.Dict[str, str]] = None,
120 base_url: str,
121 timeout: typing.Optional[float] = None,
122 max_retries: int = 2,
123 stream_reconnection_enabled: typing.Optional[bool] = None,
124 max_stream_reconnection_attempts: typing.Optional[int] = None,
125 logging: typing.Optional[typing.Union[LogConfig, Logger]] = None,
126 async_token: typing.Optional[typing.Callable[[], typing.Awaitable[str]]] = None,
127 httpx_client: httpx.AsyncClient,
128 ):
129 super().__init__(
130 token=token,
131 headers=headers,
132 base_url=base_url,
133 timeout=timeout,
134 max_retries=max_retries,
135 stream_reconnection_enabled=stream_reconnection_enabled,
136 max_stream_reconnection_attempts=max_stream_reconnection_attempts,
137 logging=logging,
138 )
139 self._async_token = async_token
140 self.httpx_client = AsyncHttpClient(
141 httpx_client=httpx_client,
142 base_headers=self.get_headers,
143 base_timeout=self.get_timeout,
144 base_url=self.get_base_url,
145 base_max_retries=self.get_max_retries(),
146 async_base_headers=self.async_get_headers,
147 logging_config=self._logging,
148 )
149
150 async def async_get_headers(self) -> typing.Dict[str, str]:
151 headers = self.get_headers()
152 if self._async_token is not None:
153 token = await self._async_token()
154 headers["Authorization"] = f"Bearer {token}"
155 return headers