-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathpersistent_oauth.py
63 lines (48 loc) · 2.09 KB
/
persistent_oauth.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""databricks-sql-connector supports user to machine OAuth login which means the
end user has to be present to login in a browser which will be popped up by the Python process.
Pre-requisites:
- You have installed a browser (Chrome, Firefox, Safari, Internet Explorer, etc) that will be
accessible on the machine for performing OAuth login.
For security, databricks-sql-connector does not persist OAuth tokens automatically. Hence, after
the Python process terminates the end user will have to log-in again. We provide APIs to be
implemented by the end user for persisting the OAuth token. The SampleOAuthPersistence reference
shows which methods you may implement.
For this example, the DevOnlyFilePersistence class is provided. Do not use this in production.
"""
import os
from typing import Optional
from databricks import sql
from databricks.sql.experimental.oauth_persistence import (
OAuthPersistence,
OAuthToken,
DevOnlyFilePersistence,
)
class SampleOAuthPersistence(OAuthPersistence):
def persist(self, hostname: str, oauth_token: OAuthToken):
"""To be implemented by the end user to persist in the preferred storage medium.
OAuthToken has two properties:
1. OAuthToken.access_token
2. OAuthToken.refresh_token
Both should be persisted.
"""
pass
def read(self, hostname: str) -> Optional[OAuthToken]:
"""To be implemented by the end user to fetch token from the preferred storage
Fetch the access_token and refresh_token for the given hostname.
Return OAuthToken(access_token, refresh_token)
"""
pass
with sql.connect(
server_hostname=os.getenv("DATABRICKS_SERVER_HOSTNAME"),
http_path=os.getenv("DATABRICKS_HTTP_PATH"),
auth_type="databricks-oauth",
experimental_oauth_persistence=DevOnlyFilePersistence("./sample.json"),
) as connection:
for x in range(1, 100):
cursor = connection.cursor()
cursor.execute("SELECT 1+1")
result = cursor.fetchall()
for row in result:
print(row)
cursor.close()
connection.close()