-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3/n][dagster-powerbi] build initial PowerBIWorkspace resource (#23246)
## Summary Adds a `PowerBIWorkspace` resource, with utility methods to reach out to the PowerBI API. ## Test Plan Mocked out API unit test.
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
python_modules/libraries/dagster-powerbi/dagster_powerbi/__init__.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
python_modules/libraries/dagster-powerbi/dagster_powerbi/resource.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
from typing import Any, Dict | ||
|
||
import requests | ||
from dagster import ConfigurableResource | ||
from dagster._utils.cached_method import cached_method | ||
from pydantic import Field | ||
|
||
BASE_API_URL = "https://api.powerbi.com/v1.0/myorg/" | ||
|
||
|
||
class PowerBIWorkspace(ConfigurableResource): | ||
"""Represents a workspace in PowerBI and provides utilities | ||
to interact with the PowerBI API. | ||
""" | ||
|
||
api_token: str = Field(..., description="An API token used to connect to PowerBI.") | ||
workspace_id: str = Field(..., description="The ID of the PowerBI group to use.") | ||
|
||
def fetch_json(self, endpoint: str) -> Dict[str, Any]: | ||
"""Fetch JSON data from the PowerBI API. Raises an exception if the request fails. | ||
Args: | ||
endpoint (str): The API endpoint to fetch data from. | ||
Returns: | ||
Dict[str, Any]: The JSON data returned from the API. | ||
""" | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {self.api_token}", | ||
} | ||
response = requests.get( | ||
f"{BASE_API_URL}/groups/{self.workspace_id}/{endpoint}", headers=headers | ||
) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
@cached_method | ||
def get_reports(self) -> Dict[str, Any]: | ||
"""Fetches a list of all PowerBI reports in the workspace.""" | ||
return self.fetch_json("reports") | ||
|
||
@cached_method | ||
def get_semantic_models(self) -> Dict[str, Any]: | ||
"""Fetches a list of all PowerBI semantic models in the workspace.""" | ||
return self.fetch_json("datasets") | ||
|
||
@cached_method | ||
def get_semantic_model_sources( | ||
self, | ||
dataset_id: str, | ||
) -> Dict[str, Any]: | ||
"""Fetches a list of all data sources for a given semantic model.""" | ||
return self.fetch_json(f"datasets/{dataset_id}/datasources") | ||
|
||
@cached_method | ||
def get_dashboards(self) -> Dict[str, Any]: | ||
"""Fetches a list of all PowerBI dashboards in the workspace.""" | ||
return self.fetch_json("dashboards") | ||
|
||
@cached_method | ||
def get_dashboard_tiles( | ||
self, | ||
dashboard_id: str, | ||
) -> Dict[str, Any]: | ||
"""Fetches a list of all tiles for a given PowerBI dashboard, | ||
including which reports back each tile. | ||
""" | ||
return self.fetch_json(f"dashboards/{dashboard_id}/tiles") |
27 changes: 27 additions & 0 deletions
27
python_modules/libraries/dagster-powerbi/dagster_powerbi_tests/test_resource.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import uuid | ||
|
||
import responses | ||
from dagster_powerbi import PowerBIWorkspace | ||
from dagster_powerbi.resource import BASE_API_URL | ||
|
||
|
||
@responses.activate | ||
def test_basic_resource_request() -> None: | ||
fake_token = uuid.uuid4().hex | ||
fake_workspace_id = uuid.uuid4().hex | ||
resource = PowerBIWorkspace( | ||
api_token=fake_token, | ||
workspace_id=fake_workspace_id, | ||
) | ||
|
||
responses.add( | ||
method=responses.GET, | ||
url=f"{BASE_API_URL}/groups/{fake_workspace_id}/reports", | ||
json={}, | ||
status=200, | ||
) | ||
|
||
resource.get_reports() | ||
|
||
assert len(responses.calls) == 1 | ||
assert responses.calls[0].request.headers["Authorization"] == f"Bearer {fake_token}" |