-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathselective_authenticator.py
45 lines (37 loc) · 1.5 KB
/
selective_authenticator.py
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
#
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
#
from dataclasses import dataclass
from typing import Any, List, Mapping
import dpath
from airbyte_cdk.sources.declarative.auth.declarative_authenticator import DeclarativeAuthenticator
@dataclass
class SelectiveAuthenticator(DeclarativeAuthenticator):
"""Authenticator that selects concrete implementation based on specific config value."""
config: Mapping[str, Any]
authenticators: Mapping[str, DeclarativeAuthenticator]
authenticator_selection_path: List[str]
# returns "DeclarativeAuthenticator", but must return a subtype of "SelectiveAuthenticator"
def __new__( # type: ignore[misc]
cls,
config: Mapping[str, Any],
authenticators: Mapping[str, DeclarativeAuthenticator],
authenticator_selection_path: List[str],
*arg: Any,
**kwargs: Any,
) -> DeclarativeAuthenticator:
try:
selected_key = str(
dpath.get(
config, # type: ignore [arg-type] # Dpath wants mutable mapping but doesn't need it.
authenticator_selection_path,
)
)
except KeyError as err:
raise ValueError(
"The path from `authenticator_selection_path` is not found in the config."
) from err
try:
return authenticators[selected_key]
except KeyError as err:
raise ValueError(f"The authenticator `{selected_key}` is not found.") from err