-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathclient.py
More file actions
464 lines (402 loc) · 14.6 KB
/
Copy pathclient.py
File metadata and controls
464 lines (402 loc) · 14.6 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
from abc import ABCMeta, abstractmethod
from json import JSONDecodeError
from typing import Any, Dict, Optional
from httpx import URL
from httpx import AsyncClient as HttpxAsyncClient
from httpx import Client as HttpxClient
from httpx import HTTPStatusError, Request, RequestError, Response
from httpx import codes as HttpxCodes
from httpx._types import AuthTypes
from firebolt.client.auth import Auth
from firebolt.client.auth.base import AuthRequest
from firebolt.client.constants import (
DEFAULT_API_URL,
PROTOCOL_VERSION,
PROTOCOL_VERSION_HEADER_NAME,
)
from firebolt.client.http_backend import (
AsyncKeepaliveTransport,
KeepaliveTransport,
)
from firebolt.utils.exception import (
AccountNotFoundError,
FireboltEngineError,
InterfaceError,
)
from firebolt.utils.urls import (
ACCOUNT_BY_NAME_URL_V1,
ACCOUNT_ENGINE_ID_BY_NAME_URL,
ACCOUNT_ENGINE_URL,
ACCOUNT_ENGINE_URL_BY_DATABASE_NAME_V1,
ACCOUNT_URL,
)
from firebolt.utils.util import (
cached_property,
fix_url_schema,
get_auth_endpoint,
merge_urls,
mixin_for,
)
FireboltClientMixinBase = mixin_for(HttpxClient) # type: Any
class FireboltClientMixin(FireboltClientMixinBase):
"""HttpxAsyncClient mixin with Firebolt authentication functionality."""
def __init__(
self,
*args: Any,
auth: Auth,
account_name: Optional[str] = None,
api_endpoint: str = DEFAULT_API_URL,
**kwargs: Any,
):
self.account_name = account_name
self._api_endpoint = URL(fix_url_schema(api_endpoint))
self._auth_endpoint = get_auth_endpoint(self._api_endpoint)
super().__init__(*args, auth=auth, **kwargs)
self._set_default_header(PROTOCOL_VERSION_HEADER_NAME, PROTOCOL_VERSION)
def _set_default_header(self, key: str, value: str) -> None:
if key not in self.headers:
self.headers[key] = value
def _build_auth(self, auth: Optional[AuthTypes]) -> Auth:
"""Create Auth object based on auth provided.
Overrides ``httpx.Client._build_auth``
Args:
auth (AuthTypes): Provided auth
Returns:
Optional[Auth]: Auth object
Raises:
TypeError: Auth argument has unsupported type
"""
if not (auth is None or isinstance(auth, Auth)):
raise TypeError(f'Invalid "auth" argument: {auth!r}')
assert auth is not None # type check
return auth
def _merge_auth_request(self, request: Request) -> Request:
if isinstance(request, AuthRequest):
request.url = merge_urls(self._auth_endpoint, request.url)
request._prepare(dict(request.headers))
return request
def _enforce_trailing_slash(self, url: URL) -> URL:
"""Don't automatically append trailing slash to a base url"""
return url
def clone(self) -> "Client":
return self.__class__(
auth=self.auth,
account_name=self.account_name,
base_url=self.base_url,
api_endpoint=str(self._api_endpoint),
timeout=self.timeout,
headers=self.headers,
)
class Client(FireboltClientMixin, HttpxClient, metaclass=ABCMeta):
def __init__(self, *args: Any, **kwargs: Any):
# We pop it from kwargs because it's unknown to HttpxClient which won't accept it
client_side_lb = kwargs.pop("client_side_lb", False)
super().__init__(
*args,
**kwargs,
transport=KeepaliveTransport(
verify=kwargs.get("verify", True),
client_side_lb=client_side_lb,
),
)
@property
@abstractmethod
def account_id(self) -> str:
...
def _send_handling_redirects(
self, request: Request, *args: Any, **kwargs: Any
) -> Response:
return super()._send_handling_redirects(
self._merge_auth_request(request), *args, **kwargs
)
class ClientV2(Client):
"""An HTTP client, based on httpx.Client.
Handles the authentication for Firebolt database.
Authentication can be passed through auth keyword as a tuple or as a
FireboltAuth instance
"""
def __init__(
self,
*args: Any,
auth: Auth,
account_name: str,
api_endpoint: str = DEFAULT_API_URL,
client_side_lb: bool = False,
**kwargs: Any,
):
super().__init__(
*args,
auth=auth,
account_name=account_name,
api_endpoint=api_endpoint,
client_side_lb=client_side_lb,
**kwargs,
)
@property
def account_id(self) -> str:
"""User account ID.
If account_name was provided during Client construction, returns its ID;
gets default account otherwise.
Returns:
str: Account ID
Raises:
AccountNotFoundError: No account found with provided name
"""
return ""
class ClientV1(Client):
"""An HTTP client, based on httpx.Client.
Handles the authentication for Firebolt database.
Authentication can be passed through auth keyword as a tuple or as a
FireboltAuth instance
"""
def __init__(
self,
*args: Any,
auth: Auth,
account_name: Optional[str] = None,
api_endpoint: str = DEFAULT_API_URL,
**kwargs: Any,
):
super().__init__(
*args,
auth=auth,
account_name=account_name,
api_endpoint=api_endpoint,
**kwargs,
)
self._auth_endpoint = URL(fix_url_schema(api_endpoint))
@cached_property
def account_id(self) -> str:
"""User account ID.
If account_name was provided during Client construction, returns its ID;
gets default account otherwise.
Returns:
str: Account ID
Raises:
AccountNotFoundError: No account found with provided name
"""
if self.account_name:
response = self.get(
url=ACCOUNT_BY_NAME_URL_V1, params={"account_name": self.account_name}
)
if response.status_code == HttpxCodes.NOT_FOUND:
raise AccountNotFoundError(self.account_name)
# process all other status codes
response.raise_for_status()
return response.json()["account_id"]
# account_name isn't set, use the default account.
return self.get(url=ACCOUNT_URL).json()["account"]["id"]
def _get_database_default_engine_url(
self,
database: str,
) -> str:
try:
account_id = self.account_id
response = self.get(
url=ACCOUNT_ENGINE_URL_BY_DATABASE_NAME_V1.format(
account_id=account_id
),
params={"database_name": database},
)
response.raise_for_status()
return response.json()["engine_url"]
except (
JSONDecodeError,
RequestError,
RuntimeError,
HTTPStatusError,
KeyError,
) as e:
raise InterfaceError(f"Unable to retrieve default engine endpoint: {e}.")
def _resolve_engine_url(self, engine_name: str) -> str:
account_id = self.account_id
url = ACCOUNT_ENGINE_ID_BY_NAME_URL.format(account_id=account_id)
try:
response = self.get(
url=url,
params={"engine_name": engine_name},
)
response.raise_for_status()
engine_id = response.json()["engine_id"]["engine_id"]
url = ACCOUNT_ENGINE_URL.format(account_id=account_id, engine_id=engine_id)
response = self.get(url=url)
response.raise_for_status()
return response.json()["engine"]["endpoint"]
except HTTPStatusError as e:
# Engine error would be 404.
if e.response.status_code != 404:
raise InterfaceError(
f"Error {e.__class__.__name__}: Unable to retrieve engine "
f"endpoint {url}."
)
# Once this is point is reached we've already authenticated with
# the backend so it's safe to assume the cause of the error is
# missing engine.
raise FireboltEngineError(f"Firebolt engine {engine_name} does not exist.")
except (JSONDecodeError, RequestError, RuntimeError) as e:
raise InterfaceError(
f"Error {e.__class__.__name__}: "
f"Unable to retrieve engine endpoint {url}."
)
class AsyncClient(FireboltClientMixin, HttpxAsyncClient, metaclass=ABCMeta):
def __init__(self, *args: Any, **kwargs: Any):
# We pop it from kwargs because it's unknown to HttpxClient which won't accept it
client_side_lb = kwargs.pop("client_side_lb", False)
super().__init__(
*args,
**kwargs,
transport=AsyncKeepaliveTransport(
verify=kwargs.get("verify", True),
client_side_lb=client_side_lb,
),
)
@property
@abstractmethod
async def account_id(self) -> str:
...
async def _send_handling_redirects(
self, request: Request, *args: Any, **kwargs: Any
) -> Response:
return await super()._send_handling_redirects(
self._merge_auth_request(request), *args, **kwargs
)
class AsyncClientV2(AsyncClient):
"""An HTTP client, based on httpx.Client.
Handles the authentication for Firebolt database.
Authentication can be passed through auth keyword as a tuple or as a
FireboltAuth instance
"""
def __init__(
self,
*args: Any,
auth: Auth,
account_name: str,
api_endpoint: str = DEFAULT_API_URL,
client_side_lb: bool = False,
**kwargs: Any,
):
super().__init__(
*args,
auth=auth,
account_name=account_name,
api_endpoint=api_endpoint,
client_side_lb=client_side_lb,
**kwargs,
)
@property
async def account_id(self) -> str:
"""User account ID.
If account_name was provided during Client construction, returns its ID;
gets default account otherwise.
Returns:
str: Account ID
Raises:
AccountNotFoundError: No account found with provided name
"""
return ""
class AsyncClientV1(AsyncClient):
"""An HTTP client, based on httpx.Client.
Handles the authentication for Firebolt database.
Authentication can be passed through auth keyword as a tuple or as a
FireboltAuth instance
"""
def __init__(
self,
*args: Any,
auth: Auth,
account_name: Optional[str] = None,
api_endpoint: str = DEFAULT_API_URL,
**kwargs: Any,
):
super().__init__(
*args,
auth=auth,
account_name=account_name,
api_endpoint=api_endpoint,
**kwargs,
)
self.account_id_cache: Dict[str, str] = {}
self._auth_endpoint = URL(fix_url_schema(api_endpoint))
@property
async def account_id(self) -> str:
"""User account ID.
If account_name was provided during Client construction, returns its ID;
gets default account otherwise.
Returns:
str: Account ID
Raises:
AccountNotFoundError: No account found with provided name
"""
# manual caching to avoid async_cached_property issues
if self.account_name in self.account_id_cache:
return self.account_id_cache[self.account_name]
if self.account_name:
response = await self.get(
url=ACCOUNT_BY_NAME_URL_V1, params={"account_name": self.account_name}
)
if response.status_code == HttpxCodes.NOT_FOUND:
raise AccountNotFoundError(self.account_name)
# process all other status codes
response.raise_for_status()
account_id = response.json()["account_id"]
else:
# account_name isn't set; use the default account.
account_response = (await self.get(url=ACCOUNT_URL)).json()["account"]
account_id = account_response["id"]
self.account_name = account_response["name"]
assert self.account_name is not None # type check
# cache for future use
self.account_id_cache[self.account_name] = account_id
return account_id
async def _get_database_default_engine_url(
self,
database: str,
) -> str:
try:
account_id = await self.account_id
response = await self.get(
url=ACCOUNT_ENGINE_URL_BY_DATABASE_NAME_V1.format(
account_id=account_id
),
params={"database_name": database},
)
response.raise_for_status()
return response.json()["engine_url"]
except (
JSONDecodeError,
RequestError,
RuntimeError,
HTTPStatusError,
KeyError,
) as e:
raise InterfaceError(f"Unable to retrieve default engine endpoint: {e}.")
async def _resolve_engine_url(self, engine_name: str) -> str:
account_id = await self.account_id
url = ACCOUNT_ENGINE_ID_BY_NAME_URL.format(account_id=account_id)
try:
response = await self.get(
url=url,
params={"engine_name": engine_name},
)
response.raise_for_status()
engine_id = response.json()["engine_id"]["engine_id"]
url = ACCOUNT_ENGINE_URL.format(account_id=account_id, engine_id=engine_id)
response = await self.get(url=url)
response.raise_for_status()
return response.json()["engine"]["endpoint"]
except HTTPStatusError as e:
# Engine error would be 404.
if e.response.status_code != 404:
raise InterfaceError(
f"Error {e.__class__.__name__}: Unable to retrieve engine "
f"endpoint {url}."
)
# Once this is point is reached we've already authenticated with
# the backend so it's safe to assume the cause of the error is
# missing engine.
raise FireboltEngineError(f"Firebolt engine {engine_name} does not exist.")
except (JSONDecodeError, RequestError, RuntimeError) as e:
raise InterfaceError(
f"Error {e.__class__.__name__}: "
f"Unable to retrieve engine endpoint {url}."
)