|
7 | 7 | These APIs are provided as a convenience and are subject to breaking changes: |
8 | 8 | https://github.com/databricks/databricks-sdk-py#interface-stability |
9 | 9 | """ |
| 10 | + |
10 | 11 | from __future__ import annotations |
11 | 12 |
|
12 | | -from typing_extensions import Dict, Optional |
| 13 | +import logging |
| 14 | + |
| 15 | +from typing_extensions import TYPE_CHECKING, Dict, Optional |
13 | 16 |
|
14 | 17 | from .._utils import is_connect, is_workbench |
15 | 18 | from ..client import Client |
16 | | -from ..oauth import Credentials |
17 | 19 |
|
18 | 20 | try: |
19 | 21 | from databricks.sdk.core import Config, DefaultCredentials |
20 | 22 | from databricks.sdk.credentials_provider import CredentialsProvider, CredentialsStrategy |
21 | | -except ImportError: |
22 | | - raise ImportError("The 'databricks-sdk' package is required to use this module.") |
| 23 | +except ImportError as e: |
| 24 | + raise ImportError("The 'databricks-sdk' package is required to use this module.") from e |
| 25 | + |
| 26 | +if TYPE_CHECKING: |
| 27 | + from ..oauth import Credentials |
23 | 28 |
|
24 | 29 |
|
25 | 30 | POSIT_OAUTH_INTEGRATION_AUTH_TYPE = "posit-oauth-integration" |
@@ -85,41 +90,47 @@ def __call__(self) -> Dict[str, str]: |
85 | 90 |
|
86 | 91 |
|
87 | 92 | class WorkbenchStrategy(CredentialsStrategy): |
88 | | - """ |
89 | | - """ |
90 | | - def __init__(self, config: Config): |
91 | | - self._config = config or Config("workbench") |
| 93 | + """`CredentialsStrategy` implementation which provides a bearer token for Workbench environments.""" |
| 94 | + |
| 95 | + def __init__(self, config: Optional[Config] = None): |
| 96 | + self._config = config or Config(profile="workbench") |
92 | 97 |
|
93 | 98 | def auth_type(self) -> str: |
94 | 99 | return POSIT_WORKBENCH_AUTH_TYPE |
95 | 100 |
|
96 | | - def __call__(self, *args, **kwargs) -> CredentialsProvider: # noqa: ARG002 |
| 101 | + def __call__(self, *args, **kwargs) -> CredentialsProvider: # noqa: ARG002 |
97 | 102 | if self._config.token is None: |
98 | 103 | raise ValueError("Missing value for field 'token' in Config.") |
99 | 104 |
|
100 | 105 | def cp(): |
101 | 106 | return {"Authorization": f"Bearer {self._config.token}"} |
| 107 | + |
102 | 108 | return cp |
103 | 109 |
|
104 | 110 |
|
105 | 111 | class ConnectStrategy(CredentialsStrategy): |
106 | | - """ |
107 | | - """ |
108 | | - def __init__(self, |
109 | | - client: Optional[Client] = None, |
110 | | - user_session_token: Optional[str] = None, |
| 112 | + """`CredentialsStrategy` implementation which provides a bearer token for Posit Connect environments.""" |
| 113 | + |
| 114 | + def __init__( |
| 115 | + self, |
| 116 | + client: Optional[Client] = None, |
| 117 | + user_session_token: Optional[str] = None, |
111 | 118 | ): |
112 | 119 | self._client = client |
113 | 120 | self._user_session_token = user_session_token |
114 | 121 | if self._user_session_token is None: |
115 | | - print() # log that we are falling back to client credentials |
| 122 | + logging.info( |
| 123 | + "ConnectStrategy will attempt to use OAuth Service Account credentials because user_session_token is not set." |
| 124 | + ) |
116 | 125 |
|
117 | 126 | def auth_type(self) -> str: |
118 | 127 | return POSIT_OAUTH_INTEGRATION_AUTH_TYPE |
119 | 128 |
|
120 | | - def __call__(self, *args, **kwargs) -> CredentialsProvider: # noqa: ARG002 |
| 129 | + def __call__(self, *args, **kwargs) -> CredentialsProvider: # noqa: ARG002 |
121 | 130 | if not is_connect(): |
122 | | - raise ValueError("The PositConnectCredentials is not supported for content running outside of Posit Connect.") |
| 131 | + raise ValueError( |
| 132 | + "The ConnectStrategy is not supported for content running outside of Posit Connect." |
| 133 | + ) |
123 | 134 |
|
124 | 135 | if self._client is None: |
125 | 136 | self._client = Client() |
@@ -157,30 +168,32 @@ def new_config( |
157 | 168 | Config |
158 | 169 | """ |
159 | 170 | credentials_strategy = None |
160 | | - if 'credentials_strategy' in kwargs: |
161 | | - del kwargs['credentials_strategy'] |
| 171 | + if "credentials_strategy" in kwargs: |
| 172 | + del kwargs["credentials_strategy"] |
162 | 173 |
|
163 | 174 | if is_connect(): |
| 175 | + logging.info( |
| 176 | + "Initializing ConnectStrategy because the content appears to be running on Posit Connect." |
| 177 | + ) |
164 | 178 | if posit_connect_strategy is not None: |
165 | 179 | credentials_strategy = posit_connect_strategy |
166 | 180 | else: |
167 | 181 | credentials_strategy = ConnectStrategy() |
168 | | - else: |
169 | | - print() |
170 | | - # log and continue |
171 | | - |
172 | | - if is_workbench(): |
| 182 | + elif is_workbench(): |
| 183 | + logging.info( |
| 184 | + "Initializing WorkbenchStrategy because the content appears to be running on Posit Workbench." |
| 185 | + ) |
173 | 186 | if posit_workbench_strategy is not None: |
174 | 187 | credentials_strategy = posit_workbench_strategy |
175 | 188 | else: |
176 | 189 | credentials_strategy = WorkbenchStrategy() |
177 | 190 | else: |
178 | | - print() |
179 | | - # log and continue |
180 | | - |
181 | | - if posit_default_strategy is not None: |
182 | | - credentials_strategy = posit_default_strategy |
183 | | - else: |
184 | | - credentials_strategy = DefaultCredentials() |
| 191 | + logging.info( |
| 192 | + "Initializing default strategy because neither Posit Connect nor Posit Workbench were detected." |
| 193 | + ) |
| 194 | + if posit_default_strategy is not None: |
| 195 | + credentials_strategy = posit_default_strategy |
| 196 | + else: |
| 197 | + credentials_strategy = DefaultCredentials() |
185 | 198 |
|
186 | 199 | return Config(credentials_strategy=credentials_strategy, **kwargs) |
0 commit comments