Skip to content

Commit 756a7e7

Browse files
committed
Rename RefreshableCredentials to SessionCredentials (#116)
Align more with other SDKs
1 parent 405b147 commit 756a7e7

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@ Works for both AWS and Azure. Not supported for GCP at the moment.
368368

369369
```python
370370
from databricks.sdk.oauth import OAuthClient
371+
371372
oauth_client = OAuthClient(host='<workspace-url>',
372373
client_id='<oauth client ID>',
373374
redirect_url=f'http://host.domain/callback',
@@ -380,29 +381,31 @@ APP_NAME = 'flask-demo'
380381
app = Flask(APP_NAME)
381382
app.secret_key = secrets.token_urlsafe(32)
382383

384+
383385
@app.route('/callback')
384386
def callback():
385-
from databricks.sdk.oauth import Consent
386-
consent = Consent.from_dict(oauth_client, session['consent'])
387-
session['creds'] = consent.exchange_callback_parameters(request.args).as_dict()
388-
return redirect(url_for('index'))
387+
from databricks.sdk.oauth import Consent
388+
consent = Consent.from_dict(oauth_client, session['consent'])
389+
session['creds'] = consent.exchange_callback_parameters(request.args).as_dict()
390+
return redirect(url_for('index'))
391+
389392

390393
@app.route('/')
391394
def index():
392-
if 'creds' not in session:
393-
consent = oauth_client.initiate_consent()
394-
session['consent'] = consent.as_dict()
395-
return redirect(consent.auth_url)
395+
if 'creds' not in session:
396+
consent = oauth_client.initiate_consent()
397+
session['consent'] = consent.as_dict()
398+
return redirect(consent.auth_url)
396399

397-
from databricks.sdk import WorkspaceClient
398-
from databricks.sdk.oauth import RefreshableCredentials
400+
from databricks.sdk import WorkspaceClient
401+
from databricks.sdk.oauth import SessionCredentials
399402

400-
credentials_provider = RefreshableCredentials.from_dict(oauth_client, session['creds'])
401-
workspace_client = WorkspaceClient(host=oauth_client.host,
402-
product=APP_NAME,
403-
credentials_provider=credentials_provider)
403+
credentials_provider = SessionCredentials.from_dict(oauth_client, session['creds'])
404+
workspace_client = WorkspaceClient(host=oauth_client.host,
405+
product=APP_NAME,
406+
credentials_provider=credentials_provider)
404407

405-
return render_template_string('...', w=workspace_client)
408+
return render_template_string('...', w=workspace_client)
406409
```
407410

408411
### SSO for local scripts on development machines

databricks/sdk/oauth.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def do_GET(self):
177177
self.wfile.write(b'You can close this tab.')
178178

179179

180-
class RefreshableCredentials(Refreshable):
180+
class SessionCredentials(Refreshable):
181181

182182
def __init__(self, client: 'OAuthClient', token: Token):
183183
self._client = client
@@ -187,8 +187,8 @@ def as_dict(self) -> dict:
187187
return {'token': self._token.as_dict()}
188188

189189
@staticmethod
190-
def from_dict(client: 'OAuthClient', raw: dict) -> 'RefreshableCredentials':
191-
return RefreshableCredentials(client=client, token=Token.from_dict(raw['token']))
190+
def from_dict(client: 'OAuthClient', raw: dict) -> 'SessionCredentials':
191+
return SessionCredentials(client=client, token=Token.from_dict(raw['token']))
192192

193193
def auth_type(self):
194194
"""Implementing CredentialsProvider protocol"""
@@ -237,7 +237,7 @@ def as_dict(self) -> dict:
237237
def from_dict(client: 'OAuthClient', raw: dict) -> 'Consent':
238238
return Consent(client, raw['state'], raw['verifier'])
239239

240-
def launch_external_browser(self) -> RefreshableCredentials:
240+
def launch_external_browser(self) -> SessionCredentials:
241241
redirect_url = urllib.parse.urlparse(self._client.redirect_url)
242242
if redirect_url.hostname not in ('localhost', '127.0.0.1'):
243243
raise ValueError(f'cannot listen on {redirect_url.hostname}')
@@ -254,14 +254,14 @@ def launch_external_browser(self) -> RefreshableCredentials:
254254
query = feedback.pop()
255255
return self.exchange_callback_parameters(query)
256256

257-
def exchange_callback_parameters(self, query: Dict[str, str]) -> RefreshableCredentials:
257+
def exchange_callback_parameters(self, query: Dict[str, str]) -> SessionCredentials:
258258
if 'error' in query:
259259
raise ValueError('{error}: {error_description}'.format(**query))
260260
if 'code' not in query or 'state' not in query:
261261
raise ValueError('No code returned in callback')
262262
return self.exchange(query['code'], query['state'])
263263

264-
def exchange(self, code: str, state: str) -> RefreshableCredentials:
264+
def exchange(self, code: str, state: str) -> SessionCredentials:
265265
if self._state != state:
266266
raise ValueError('state mismatch')
267267
params = {
@@ -279,7 +279,7 @@ def exchange(self, code: str, state: str) -> RefreshableCredentials:
279279
params=params,
280280
headers=headers,
281281
use_params=True)
282-
return RefreshableCredentials(self._client, token)
282+
return SessionCredentials(self._client, token)
283283
except ValueError as e:
284284
if NO_ORIGIN_FOR_SPA_CLIENT_ERROR in str(e):
285285
# Retry in cases of 'Single-Page Application' client-type with
@@ -420,7 +420,7 @@ def filename(self) -> str:
420420
hash.update(chunk.encode('utf-8'))
421421
return os.path.expanduser(os.path.join(self.__class__.BASE_PATH, hash.hexdigest() + ".json"))
422422

423-
def load(self) -> Optional[RefreshableCredentials]:
423+
def load(self) -> Optional[SessionCredentials]:
424424
"""
425425
Load credentials from cache file. Return None if the cache file does not exist or is invalid.
426426
"""
@@ -430,11 +430,11 @@ def load(self) -> Optional[RefreshableCredentials]:
430430
try:
431431
with open(self.filename, 'r') as f:
432432
raw = json.load(f)
433-
return RefreshableCredentials.from_dict(self.client, raw)
433+
return SessionCredentials.from_dict(self.client, raw)
434434
except Exception:
435435
return None
436436

437-
def save(self, credentials: RefreshableCredentials) -> None:
437+
def save(self, credentials: SessionCredentials) -> None:
438438
"""
439439
Save credentials to cache file.
440440
"""

examples/flask_app_with_oauth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ def index():
7878
return redirect(consent.auth_url)
7979

8080
from databricks.sdk import WorkspaceClient
81-
from databricks.sdk.oauth import RefreshableCredentials
81+
from databricks.sdk.oauth import SessionCredentials
8282

83-
credentials_provider = RefreshableCredentials.from_dict(oauth_client, session["creds"])
83+
credentials_provider = SessionCredentials.from_dict(oauth_client, session["creds"])
8484
workspace_client = WorkspaceClient(host=oauth_client.host,
8585
product=APP_NAME,
8686
credentials_provider=credentials_provider,

0 commit comments

Comments
 (0)