From 9d0b97ead1c468be427052641b27309b8e4f4101 Mon Sep 17 00:00:00 2001 From: tdstein Date: Thu, 8 Aug 2024 14:42:35 -0400 Subject: [PATCH 1/4] chore: apply formatting --- examples/connect/dash/app.py | 11 +++++---- examples/connect/fastapi/app.py | 8 ++++--- examples/connect/flask/app.py | 9 +++---- examples/connect/shiny-python/app.py | 7 +++--- examples/connect/streamlit/app.py | 9 +++---- src/posit/connect/external/databricks.py | 7 +++--- .../posit/connect/external/test_databricks.py | 24 ++++++++++++------- 7 files changed, 46 insertions(+), 29 deletions(-) diff --git a/examples/connect/dash/app.py b/examples/connect/dash/app.py index 72ff2a65..f445571e 100644 --- a/examples/connect/dash/app.py +++ b/examples/connect/dash/app.py @@ -39,13 +39,14 @@ def update_page(_): "Posit-Connect-User-Session-Token" ) posit_strategy = PositCredentialsStrategy( - local_strategy=databricks_cli, - user_session_token=session_token) + local_strategy=databricks_cli, user_session_token=session_token + ) cfg = Config( host=DATABRICKS_HOST_URL, # uses Posit's custom credential_strategy if running on Connect, # otherwise falls back to the strategy defined by local_strategy - credentials_strategy=posit_strategy) + credentials_strategy=posit_strategy, + ) def get_greeting(): databricks_user_info = CurrentUserAPI(ApiClient(cfg)).me() @@ -61,7 +62,9 @@ def get_table(): server_hostname=DATABRICKS_HOST, http_path=SQL_HTTP_PATH, # https://github.com/databricks/databricks-sql-python/issues/148#issuecomment-2271561365 - credentials_provider=posit_strategy.sql_credentials_provider(cfg) + credentials_provider=posit_strategy.sql_credentials_provider( + cfg + ), ) as connection: with connection.cursor() as cursor: cursor.execute(query) diff --git a/examples/connect/fastapi/app.py b/examples/connect/fastapi/app.py index c9eeabb7..9247d5ba 100644 --- a/examples/connect/fastapi/app.py +++ b/examples/connect/fastapi/app.py @@ -29,12 +29,14 @@ async def get_fares( posit_strategy = PositCredentialsStrategy( local_strategy=databricks_cli, - user_session_token=posit_connect_user_session_token) + user_session_token=posit_connect_user_session_token, + ) cfg = Config( host=DATABRICKS_HOST_URL, # uses Posit's custom credential_strategy if running on Connect, # otherwise falls back to the strategy defined by local_strategy - credentials_strategy=posit_strategy) + credentials_strategy=posit_strategy, + ) if rows is None: query = "SELECT * FROM samples.nyctaxi.trips LIMIT 10;" @@ -43,7 +45,7 @@ async def get_fares( server_hostname=DATABRICKS_HOST, http_path=SQL_HTTP_PATH, # https://github.com/databricks/databricks-sql-python/issues/148#issuecomment-2271561365 - credentials_provider=posit_strategy.sql_credentials_provider(cfg) + credentials_provider=posit_strategy.sql_credentials_provider(cfg), ) as connection: with connection.cursor() as cursor: cursor.execute(query) diff --git a/examples/connect/flask/app.py b/examples/connect/flask/app.py index f41fd8d1..9290bf4b 100644 --- a/examples/connect/flask/app.py +++ b/examples/connect/flask/app.py @@ -30,13 +30,14 @@ def get_fares(): session_token = request.headers.get("Posit-Connect-User-Session-Token") posit_strategy = PositCredentialsStrategy( - local_strategy=databricks_cli, - user_session_token=session_token) + local_strategy=databricks_cli, user_session_token=session_token + ) cfg = Config( host=DATABRICKS_HOST_URL, # uses Posit's custom credential_strategy if running on Connect, # otherwise falls back to the strategy defined by local_strategy - credentials_strategy=posit_strategy) + credentials_strategy=posit_strategy, + ) if rows is None: query = "SELECT * FROM samples.nyctaxi.trips LIMIT 10;" @@ -45,7 +46,7 @@ def get_fares(): server_hostname=DATABRICKS_HOST, http_path=SQL_HTTP_PATH, # https://github.com/databricks/databricks-sql-python/issues/148#issuecomment-2271561365 - credentials_provider=posit_strategy.sql_credentials_provider(cfg) + credentials_provider=posit_strategy.sql_credentials_provider(cfg), ) as connection: with connection.cursor() as cursor: cursor.execute(query) diff --git a/examples/connect/shiny-python/app.py b/examples/connect/shiny-python/app.py index fb903383..d0453787 100644 --- a/examples/connect/shiny-python/app.py +++ b/examples/connect/shiny-python/app.py @@ -25,13 +25,14 @@ def server(i: Inputs, o: Outputs, session: Session): "Posit-Connect-User-Session-Token" ) posit_strategy = PositCredentialsStrategy( - local_strategy=databricks_cli, - user_session_token=session_token) + local_strategy=databricks_cli, user_session_token=session_token + ) cfg = Config( host=DATABRICKS_HOST_URL, # uses Posit's custom credential_strategy if running on Connect, # otherwise falls back to the strategy defined by local_strategy - credentials_strategy=posit_strategy) + credentials_strategy=posit_strategy, + ) @render.data_frame def result(): diff --git a/examples/connect/streamlit/app.py b/examples/connect/streamlit/app.py index 0be0b773..4af08f84 100644 --- a/examples/connect/streamlit/app.py +++ b/examples/connect/streamlit/app.py @@ -15,13 +15,14 @@ session_token = st.context.headers.get("Posit-Connect-User-Session-Token") posit_strategy = PositCredentialsStrategy( - local_strategy=databricks_cli, - user_session_token=session_token) + local_strategy=databricks_cli, user_session_token=session_token +) cfg = Config( host=DATABRICKS_HOST_URL, # uses Posit's custom credential_strategy if running on Connect, # otherwise falls back to the strategy defined by local_strategy - credentials_strategy=posit_strategy) + credentials_strategy=posit_strategy, +) databricks_user = CurrentUserAPI(ApiClient(cfg)).me() st.write(f"Hello, {databricks_user.display_name}!") @@ -30,7 +31,7 @@ server_hostname=DATABRICKS_HOST, http_path=SQL_HTTP_PATH, # https://github.com/databricks/databricks-sql-python/issues/148#issuecomment-2271561365 - credentials_provider=posit_strategy.sql_credentials_provider(cfg) + credentials_provider=posit_strategy.sql_credentials_provider(cfg), ) as connection: with connection.cursor() as cursor: cursor.execute("SELECT * FROM samples.nyctaxi.trips LIMIT 10;") diff --git a/src/posit/connect/external/databricks.py b/src/posit/connect/external/databricks.py index 2324258e..8a97ad7a 100644 --- a/src/posit/connect/external/databricks.py +++ b/src/posit/connect/external/databricks.py @@ -13,6 +13,7 @@ # The Databricks SDK CredentialsProvider == Databricks SQL HeaderFactory CredentialsProvider = Callable[[], Dict[str, str]] + class CredentialsStrategy(abc.ABC): """Maintain compatibility with the Databricks SQL/SDK client libraries. @@ -52,11 +53,11 @@ def __call__(self) -> Dict[str, str]: class PositCredentialsStrategy(CredentialsStrategy): - - def __init__(self, + def __init__( + self, local_strategy: CredentialsStrategy, user_session_token: Optional[str] = None, - client: Optional[Client] = None + client: Optional[Client] = None, ): self.user_session_token = user_session_token self.local_strategy = local_strategy diff --git a/tests/posit/connect/external/test_databricks.py b/tests/posit/connect/external/test_databricks.py index ad58ff0d..19d08dfe 100644 --- a/tests/posit/connect/external/test_databricks.py +++ b/tests/posit/connect/external/test_databricks.py @@ -13,9 +13,11 @@ class mock_strategy: def auth_type(self) -> str: return "local" + def __call__(self) -> CredentialsProvider: - def inner() -> Dict[str,str]: + def inner() -> Dict[str, str]: return {"Authorization": "Bearer static-pat-token"} + return inner @@ -45,7 +47,9 @@ def test_posit_credentials_provider(self): register_mocks() client = Client(api_key="12345", url="https://connect.example/") - cp = PositCredentialsProvider(posit_oauth=client.oauth, user_session_token="cit") + cp = PositCredentialsProvider( + posit_oauth=client.oauth, user_session_token="cit" + ) assert cp() == {"Authorization": f"Bearer dynamic-viewer-access-token"} @responses.activate @@ -54,9 +58,11 @@ def test_posit_credentials_strategy(self): register_mocks() client = Client(api_key="12345", url="https://connect.example/") - cs = PositCredentialsStrategy(local_strategy=mock_strategy(), - user_session_token="cit", - client=client) + cs = PositCredentialsStrategy( + local_strategy=mock_strategy(), + user_session_token="cit", + client=client, + ) cp = cs() assert cs.auth_type() == "posit-oauth-integration" assert cp() == {"Authorization": "Bearer dynamic-viewer-access-token"} @@ -64,9 +70,11 @@ def test_posit_credentials_strategy(self): def test_posit_credentials_strategy_fallback(self): # local_strategy is used when the content is running locally client = Client(api_key="12345", url="https://connect.example/") - cs = PositCredentialsStrategy(local_strategy=mock_strategy(), - user_session_token="cit", - client=client) + cs = PositCredentialsStrategy( + local_strategy=mock_strategy(), + user_session_token="cit", + client=client, + ) cp = cs() assert cs.auth_type() == "local" assert cp() == {"Authorization": "Bearer static-pat-token"} From 54de713372b91ff8309e0abf7ae930493ebc16f1 Mon Sep 17 00:00:00 2001 From: tdstein Date: Thu, 8 Aug 2024 14:44:54 -0400 Subject: [PATCH 2/4] ci: add format check --- .github/workflows/ci.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 435be9b4..a7b8393f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -16,6 +16,7 @@ jobs: cache: 'pip' - run: make deps - run: make lint + - run: make fmt test: runs-on: ubuntu-latest From 2319ef131a28730cc6461c270b71c2ffb953468b Mon Sep 17 00:00:00 2001 From: tdstein Date: Thu, 8 Aug 2024 14:46:09 -0400 Subject: [PATCH 3/4] sneak through formatting issue --- src/posit/connect/auth.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/posit/connect/auth.py b/src/posit/connect/auth.py index 1bb61f4e..b9748f8a 100644 --- a/src/posit/connect/auth.py +++ b/src/posit/connect/auth.py @@ -14,5 +14,6 @@ def __init__(self, config: Config) -> None: def __call__(self, r: PreparedRequest) -> PreparedRequest: """Add authorization header to the request.""" + r.headers["Authorization"] = f"Key {self._config.api_key}" return r From fb90f7dff63093564750ab448610211cb9489aaf Mon Sep 17 00:00:00 2001 From: tdstein Date: Thu, 8 Aug 2024 14:49:15 -0400 Subject: [PATCH 4/4] fix formatting issue --- src/posit/connect/auth.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/posit/connect/auth.py b/src/posit/connect/auth.py index b9748f8a..1bb61f4e 100644 --- a/src/posit/connect/auth.py +++ b/src/posit/connect/auth.py @@ -14,6 +14,5 @@ def __init__(self, config: Config) -> None: def __call__(self, r: PreparedRequest) -> PreparedRequest: """Add authorization header to the request.""" - r.headers["Authorization"] = f"Key {self._config.api_key}" return r