|
| 1 | +"""Snowflake SDK credentials implementations which support interacting with Posit OAuth integrations on Connect. |
| 2 | +
|
| 3 | +NOTE: The APIs in this module are provided as a convenience and are subject to breaking changes. |
| 4 | +""" |
| 5 | + |
1 | 6 | from typing import Optional |
2 | 7 |
|
3 | 8 | from ..client import Client |
4 | 9 | from .external import is_local |
5 | 10 |
|
6 | | -""" |
7 | | -NOTE: The APIs in this module are provided as a convenience and are subject to breaking changes. |
8 | | -""" |
9 | | - |
10 | 11 |
|
11 | 12 | class PositAuthenticator: |
| 13 | + """ |
| 14 | + Authenticator for Snowflake SDK which supports Posit OAuth integrations on Connect. |
| 15 | +
|
| 16 | + Examples |
| 17 | + -------- |
| 18 | + ```python |
| 19 | + import os |
| 20 | +
|
| 21 | + import pandas as pd |
| 22 | + import snowflake.connector |
| 23 | + import streamlit as st |
| 24 | +
|
| 25 | + from posit.connect.external.snowflake import PositAuthenticator |
| 26 | +
|
| 27 | + ACCOUNT = os.getenv("SNOWFLAKE_ACCOUNT") |
| 28 | + WAREHOUSE = os.getenv("SNOWFLAKE_WAREHOUSE") |
| 29 | +
|
| 30 | + # USER is only required when running the example locally with external browser auth |
| 31 | + USER = os.getenv("SNOWFLAKE_USER") |
| 32 | +
|
| 33 | + # https://docs.snowflake.com/en/user-guide/sample-data-using |
| 34 | + DATABASE = os.getenv("SNOWFLAKE_DATABASE", "snowflake_sample_data") |
| 35 | + SCHEMA = os.getenv("SNOWFLAKE_SCHEMA", "tpch_sf1") |
| 36 | + TABLE = os.getenv("SNOWFLAKE_TABLE", "lineitem") |
| 37 | +
|
| 38 | + session_token = st.context.headers.get("Posit-Connect-User-Session-Token") |
| 39 | + auth = PositAuthenticator( |
| 40 | + local_authenticator="EXTERNALBROWSER", user_session_token=session_token |
| 41 | + ) |
| 42 | +
|
| 43 | + con = snowflake.connector.connect( |
| 44 | + user=USER, |
| 45 | + account=ACCOUNT, |
| 46 | + warehouse=WAREHOUSE, |
| 47 | + database=DATABASE, |
| 48 | + schema=SCHEMA, |
| 49 | + authenticator=auth.authenticator, |
| 50 | + token=auth.token, |
| 51 | + ) |
| 52 | +
|
| 53 | + snowflake_user = con.cursor().execute("SELECT CURRENT_USER()").fetchone() |
| 54 | + st.write(f"Hello, {snowflake_user[0]}!") |
| 55 | +
|
| 56 | + with st.spinner("Loading data from Snowflake..."): |
| 57 | + df = pd.read_sql_query(f"SELECT * FROM {TABLE} LIMIT 10", con) |
| 58 | +
|
| 59 | + st.dataframe(df) |
| 60 | + ``` |
| 61 | + """ |
| 62 | + |
12 | 63 | def __init__( |
13 | 64 | self, |
14 | 65 | local_authenticator: Optional[str] = None, |
|
0 commit comments