Skip to content

Commit

Permalink
Auto-renew Power BI API access tokens (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
mars-lan authored Apr 9, 2024
1 parent a3cbf32 commit f4053e7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
18 changes: 8 additions & 10 deletions metaphor/power_bi/power_bi_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,19 @@ 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(
config.client_id,
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"))
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -445,7 +443,7 @@ def _call_get(
try:
return get_request(
url,
self._headers,
self.get_headers(),
type_,
transform_response,
)
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit f4053e7

Please sign in to comment.