Skip to content

Commit 4c9294a

Browse files
committed
make fmt fixes
1 parent f02bf2d commit 4c9294a

File tree

8 files changed

+63
-52
lines changed

8 files changed

+63
-52
lines changed

examples/connect/databricks/dash/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
from databricks.sdk.service.iam import CurrentUserAPI
1111

1212
from posit.connect.external.databricks import (
13+
ConnectStrategy,
1314
new_config,
1415
sql_credentials,
15-
ConnectStrategy,
1616
)
1717

1818
DATABRICKS_HOST = os.getenv("DATABRICKS_HOST")

examples/connect/databricks/fastapi/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from typing_extensions import TYPE_CHECKING, Annotated
1010

1111
from posit.connect.external.databricks import (
12+
ConnectStrategy,
1213
new_config,
1314
sql_credentials,
14-
ConnectStrategy,
1515
)
1616

1717
if TYPE_CHECKING:

examples/connect/databricks/flask/app.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
from flask import Flask, request
77

88
from posit.connect.external.databricks import (
9+
ConnectStrategy,
910
new_config,
1011
sql_credentials,
11-
ConnectStrategy,
1212
)
1313

1414
DATABRICKS_HOST = os.getenv("DATABRICKS_HOST")
@@ -34,9 +34,7 @@ def get_fares():
3434

3535
session_token = request.headers.get("Posit-Connect-User-Session-Token")
3636
cfg = new_config(
37-
posit_connect_strategy=ConnectStrategy(
38-
user_session_token=session_token
39-
),
37+
posit_connect_strategy=ConnectStrategy(user_session_token=session_token),
4038
)
4139

4240
if rows is None:

examples/connect/databricks/shiny/app.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from shiny import App, Inputs, Outputs, Session, render, ui
1010

1111
from posit.connect.external.databricks import (
12+
ConnectStrategy,
1213
new_config,
1314
sql_credentials,
14-
ConnectStrategy,
1515
)
1616

1717
DATABRICKS_HOST = os.getenv("DATABRICKS_HOST")
@@ -28,9 +28,7 @@ def server(i: Inputs, o: Outputs, session: Session):
2828
"""
2929
session_token = session.http_conn.headers.get("Posit-Connect-User-Session-Token")
3030
cfg = new_config(
31-
posit_connect_strategy=ConnectStrategy(
32-
user_session_token=session_token
33-
),
31+
posit_connect_strategy=ConnectStrategy(user_session_token=session_token),
3432
)
3533

3634
@render.data_frame

examples/connect/databricks/streamlit/app.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
from databricks.sdk.service.iam import CurrentUserAPI
1010

1111
from posit.connect.external.databricks import (
12+
ConnectStrategy,
1213
new_config,
1314
sql_credentials,
14-
ConnectStrategy,
1515
)
1616

1717
DATABRICKS_HOST = os.getenv("DATABRICKS_HOST")
@@ -20,9 +20,7 @@
2020

2121
session_token = st.context.headers.get("Posit-Connect-User-Session-Token")
2222
cfg = new_config(
23-
posit_connect_strategy=ConnectStrategy(
24-
user_session_token=session_token
25-
),
23+
posit_connect_strategy=ConnectStrategy(user_session_token=session_token),
2624
)
2725

2826
databricks_user = CurrentUserAPI(ApiClient(cfg)).me()

src/posit/connect/_utils.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@ def is_workbench() -> bool:
3737
3838
There is not yet a definitive way to determine if the content is running on Workbench. This method is best-effort.
3939
"""
40-
return "RSW_LAUNCHER" in os.environ or \
41-
"RSTUDIO_MULTI_SESSION" in os.environ or \
42-
"RS_SERVER_ADDRESS" in os.environ or \
43-
"RS_SERVER_URL" in os.environ
40+
return (
41+
"RSW_LAUNCHER" in os.environ
42+
or "RSTUDIO_MULTI_SESSION" in os.environ
43+
or "RS_SERVER_ADDRESS" in os.environ
44+
or "RS_SERVER_URL" in os.environ
45+
)
4446

4547

4648
def is_connect() -> bool:

src/posit/connect/client.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def with_user_session_token(self, token: str) -> Client:
208208
--------
209209
```python
210210
from posit.connect import Client
211+
211212
client = Client().with_user_session_token("my-user-session-token")
212213
```
213214
@@ -218,13 +219,14 @@ def with_user_session_token(self, token: str) -> Client:
218219
219220
client = Client()
220221
222+
221223
@reactive.calc
222224
def visitor_client():
223225
## read the user session token and generate a new client
224-
user_session_token = session.http_conn.headers.get(
225-
"Posit-Connect-User-Session-Token"
226-
)
226+
user_session_token = session.http_conn.headers.get("Posit-Connect-User-Session-Token")
227227
return client.with_user_session_token(user_session_token)
228+
229+
228230
@render.text
229231
def user_profile():
230232
# fetch the viewer's profile information

src/posit/connect/external/databricks.py

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@
77
These APIs are provided as a convenience and are subject to breaking changes:
88
https://github.com/databricks/databricks-sdk-py#interface-stability
99
"""
10+
1011
from __future__ import annotations
1112

12-
from typing_extensions import Dict, Optional
13+
import logging
14+
15+
from typing_extensions import TYPE_CHECKING, Dict, Optional
1316

1417
from .._utils import is_connect, is_workbench
1518
from ..client import Client
16-
from ..oauth import Credentials
1719

1820
try:
1921
from databricks.sdk.core import Config, DefaultCredentials
2022
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
2328

2429

2530
POSIT_OAUTH_INTEGRATION_AUTH_TYPE = "posit-oauth-integration"
@@ -85,41 +90,47 @@ def __call__(self) -> Dict[str, str]:
8590

8691

8792
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")
9297

9398
def auth_type(self) -> str:
9499
return POSIT_WORKBENCH_AUTH_TYPE
95100

96-
def __call__(self, *args, **kwargs) -> CredentialsProvider: # noqa: ARG002
101+
def __call__(self, *args, **kwargs) -> CredentialsProvider: # noqa: ARG002
97102
if self._config.token is None:
98103
raise ValueError("Missing value for field 'token' in Config.")
99104

100105
def cp():
101106
return {"Authorization": f"Bearer {self._config.token}"}
107+
102108
return cp
103109

104110

105111
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,
111118
):
112119
self._client = client
113120
self._user_session_token = user_session_token
114121
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+
)
116125

117126
def auth_type(self) -> str:
118127
return POSIT_OAUTH_INTEGRATION_AUTH_TYPE
119128

120-
def __call__(self, *args, **kwargs) -> CredentialsProvider: # noqa: ARG002
129+
def __call__(self, *args, **kwargs) -> CredentialsProvider: # noqa: ARG002
121130
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+
)
123134

124135
if self._client is None:
125136
self._client = Client()
@@ -157,30 +168,32 @@ def new_config(
157168
Config
158169
"""
159170
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"]
162173

163174
if is_connect():
175+
logging.info(
176+
"Initializing ConnectStrategy because the content appears to be running on Posit Connect."
177+
)
164178
if posit_connect_strategy is not None:
165179
credentials_strategy = posit_connect_strategy
166180
else:
167181
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+
)
173186
if posit_workbench_strategy is not None:
174187
credentials_strategy = posit_workbench_strategy
175188
else:
176189
credentials_strategy = WorkbenchStrategy()
177190
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()
185198

186199
return Config(credentials_strategy=credentials_strategy, **kwargs)

0 commit comments

Comments
 (0)