Skip to content

Commit b229b4b

Browse files
committed
updating docstrings in response to PR comments
1 parent d7ba5e6 commit b229b4b

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

src/posit/connect/external/databricks.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,15 @@ def _get_auth_type(local_auth_type: str) -> str:
7575

7676

7777
class PositContentCredentialsProvider:
78-
"""CredentialsProvider implementation which initiates a credential exchange using a content-session-token."""
78+
"""CredentialsProvider implementation which initiates a credential exchange using a content-session-token.
79+
80+
The content-session-token is provided by Connect through the environment variable `CONNECT_CONTENT_SESSION_TOKEN`.
81+
82+
See Also
83+
--------
84+
* https://github.com/posit-dev/posit-sdk-py/blob/main/src/posit/connect/oauth/oauth.py
85+
86+
"""
7987

8088
def __init__(self, client: Client):
8189
self._client = client
@@ -86,7 +94,16 @@ def __call__(self) -> Dict[str, str]:
8694

8795

8896
class PositCredentialsProvider:
89-
"""CredentialsProvider implementation which initiates a credential exchange using a user-session-token."""
97+
"""CredentialsProvider implementation which initiates a credential exchange using a user-session-token.
98+
99+
The user-session-token is provided by Connect through the HTTP session header
100+
`Posit-Connect-User-Session-Token`.
101+
102+
See Also
103+
--------
104+
* https://github.com/posit-dev/posit-sdk-py/blob/main/src/posit/connect/oauth/oauth.py
105+
106+
"""
90107

91108
def __init__(self, client: Client, user_session_token: str):
92109
self._client = client
@@ -103,22 +120,26 @@ class PositContentCredentialsStrategy(CredentialsStrategy):
103120
This strategy callable class returns a `PositContentCredentialsProvider` when hosted on Connect, and
104121
its `local_strategy` strategy otherwise.
105122
106-
Example
107-
-------
123+
Examples
124+
--------
125+
126+
NOTE: in the example below, the PositContentCredentialsStrategy can be initialized anywhere that
127+
the Python process can read environment variables.
128+
129+
```python
108130
from posit.connect.external.databricks import PositContentCredentialsStrategy
109131
110132
import pandas as pd
111-
import requests
112133
113134
from databricks import sql
114135
from databricks.sdk.core import ApiClient, Config, databricks_cli
115136
from databricks.sdk.service.iam import CurrentUserAPI
116137
117-
# env vars
118138
DATABRICKS_HOST = "<REDACTED>"
119139
DATABRICKS_HOST_URL = f"https://{DATABRICKS_HOST}"
120140
SQL_HTTP_PATH = "<REDACTED>"
121141
142+
# reads `CONNECT_CONTENT_SESSION_TOKEN` environment variable if hosted on Connect
122143
posit_strategy = PositContentCredentialsStrategy(local_strategy=databricks_cli)
123144
124145
cfg = Config(host=DATABRICKS_HOST_URL, credentials_strategy=posit_strategy)
@@ -136,6 +157,7 @@ class PositContentCredentialsStrategy(CredentialsStrategy):
136157
cursor.execute(query)
137158
rows = cursor.fetchall()
138159
print(pd.DataFrame([row.asDict() for row in rows]))
160+
```
139161
"""
140162

141163
def __init__(
@@ -178,8 +200,13 @@ class PositCredentialsStrategy(CredentialsStrategy):
178200
This strategy callable class returns a `PositCredentialsProvider` when hosted on Connect, and
179201
its `local_strategy` strategy otherwise.
180202
181-
Example
182-
-------
203+
Examples
204+
--------
205+
206+
NOTE: In the example below, the PositCredentialsProvider *must* be initialized within the context of the
207+
shiny `server` function, which provides access to the HTTP session headers.
208+
209+
```python
183210
import os
184211
185212
import pandas as pd
@@ -188,15 +215,16 @@ class PositCredentialsStrategy(CredentialsStrategy):
188215
from databricks.sdk.service.iam import CurrentUserAPI
189216
from posit.connect.external.databricks import PositCredentialsStrategy
190217
from shiny import App, Inputs, Outputs, Session, render, ui
191-
192-
# env vars
218+
193219
DATABRICKS_HOST = "<REDACTED>"
194220
DATABRICKS_HOST_URL = f"https://{DATABRICKS_HOST}"
195221
SQL_HTTP_PATH = "<REDACTED>"
196222
197223
app_ui = ui.page_fluid(ui.output_text("text"), ui.output_data_frame("result"))
198224
199225
def server(i: Inputs, o: Outputs, session: Session):
226+
227+
# HTTP session headers are available in this context.
200228
session_token = session.http_conn.headers.get("Posit-Connect-User-Session-Token")
201229
posit_strategy = PositCredentialsStrategy(
202230
local_strategy=databricks_cli, user_session_token=session_token
@@ -224,6 +252,7 @@ def text():
224252
return f"Hello, {databricks_user_info.display_name}!"
225253
226254
app = App(app_ui, server)
255+
```
227256
"""
228257

229258
def __init__(

0 commit comments

Comments
 (0)