From f4053e75d8cbe3dee397b49689f9ea27efd64b41 Mon Sep 17 00:00:00 2001 From: Mars Lan Date: Tue, 9 Apr 2024 14:50:57 -0700 Subject: [PATCH] Auto-renew Power BI API access tokens (#823) --- metaphor/power_bi/power_bi_client.py | 18 ++++++++---------- poetry.lock | 2 +- pyproject.toml | 4 ++-- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/metaphor/power_bi/power_bi_client.py b/metaphor/power_bi/power_bi_client.py index 6178da80..7dea8f7c 100644 --- a/metaphor/power_bi/power_bi_client.py +++ b/metaphor/power_bi/power_bi_client.py @@ -71,7 +71,10 @@ class PowerBIClient: MAX_WORKSPACES_PER_SCAN = 100 def __init__(self, config: PowerBIRunConfig): - self._headers = {"Authorization": self.retrieve_access_token(config)} + self.config = config + + def get_headers(self): + return {"Authorization": self.retrieve_access_token(self.config)} def retrieve_access_token(self, config: PowerBIRunConfig) -> str: app = msal.ConfidentialClientApplication( @@ -79,13 +82,8 @@ def retrieve_access_token(self, config: PowerBIRunConfig) -> str: authority=self.AUTHORITY.format(tenant_id=config.tenant_id), client_credential=config.secret, ) - token = app.acquire_token_silent(self.SCOPES, account=None) - if not token: - logger.info( - "No suitable token exists in cache. Let's get a new one from AAD." - ) - token = app.acquire_token_for_client(scopes=self.SCOPES) + token = app.acquire_token_for_client(scopes=self.SCOPES) access_token = token.get("access_token") if access_token is None: raise AccessTokenError(token.get("error_description", "unknown error")) @@ -317,7 +315,7 @@ def create_scan() -> str: request_body = {"workspaces": workspace_ids} result = requests.post( url, - headers=self._headers, + headers=self.get_headers(), params={ "datasetExpressions": True, "datasetSchema": True, @@ -347,7 +345,7 @@ def wait_for_scan_result(scan_id: str, max_timeout_in_secs: int = 30) -> bool: waiting_time = 0 sleep_time = 1 while True: - result = requests.get(url, headers=self._headers, timeout=600) + result = requests.get(url, headers=self.get_headers(), timeout=600) if result.status_code != 200: return False if result.json()["status"] == "Succeeded": @@ -445,7 +443,7 @@ def _call_get( try: return get_request( url, - self._headers, + self.get_headers(), type_, transform_response, ) diff --git a/poetry.lock b/poetry.lock index 47b2894b..f4cc85bf 100644 --- a/poetry.lock +++ b/poetry.lock @@ -6124,4 +6124,4 @@ unity-catalog = ["databricks-sdk", "databricks-sql-connector"] [metadata] lock-version = "2.0" python-versions = ">=3.8.1,<3.12" -content-hash = "483624f37f20fc32c59bbf5603b5be27b6ae48846ce40d9360f65d13e082250f" +content-hash = "fed4c7503c30dcc465a54eda410bdf98e8150398567eafe4b2cb046265dd64ca" diff --git a/pyproject.toml b/pyproject.toml index 39852ce8..70bf9714 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "metaphor-connectors" -version = "0.13.161" +version = "0.13.162" license = "Apache-2.0" description = "A collection of Python-based 'connectors' that extract metadata from various sources to ingest into the Metaphor app." authors = ["Metaphor "] @@ -42,7 +42,7 @@ looker-sdk = { version = "^23.6.0", optional = true } lxml = { version = "~=5.0.0", optional = true } metaphor-models = "0.32.1" more-itertools = { version = "^10.1.0", optional = true } -msal = { version = "^1.20.0", optional = true } +msal = { version = "^1.28.0", optional = true } msgraph-beta-sdk = { version = "1.1.0", optional = true } parse = { version = "^1.20.0", optional = true } pathvalidate = "^3.2.0"