-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #262 from DocShow-AI/integrate-powerbi-reports
Integrate powerbi reports
- Loading branch information
Showing
11 changed files
with
327 additions
and
53 deletions.
There are no files selected for viewing
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,9 @@ | ||
from typing import List | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class GenerateEmbededTokenRequest(BaseModel): | ||
workspace_ids: List[str] | ||
dataset_ids: List[str] | ||
report_ids: List[str] |
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 |
---|---|---|
@@ -1,11 +1,35 @@ | ||
from fastapi import APIRouter | ||
from models.powerbi import GenerateEmbededTokenRequest | ||
from utils.azure.azure_manager import AzureManager | ||
|
||
powerbi_router = APIRouter() | ||
|
||
|
||
@powerbi_router.get("/powerbi/token/") | ||
async def get_powerbi_token(): | ||
@powerbi_router.post("/powerbi/embeded-token/") | ||
async def generate_powerbi_token(request: GenerateEmbededTokenRequest): | ||
azure_manager = AzureManager() | ||
token = azure_manager.get_powerbi_token() | ||
token = await azure_manager.get_powerbi_embeded_token( | ||
request.workspace_ids, request.dataset_ids, request.report_ids | ||
) | ||
return {"token": token} | ||
|
||
|
||
@powerbi_router.get("/powerbi/reports/") | ||
async def get_powerbi_reports(): | ||
azure_manager = AzureManager() | ||
reports = await azure_manager.get_powerbi_reports() | ||
return {"reports": reports} | ||
|
||
|
||
@powerbi_router.get("/powerbi/reports/{report_id}") | ||
async def get_powerbi_report(report_id: str): | ||
azure_manager = AzureManager() | ||
report = await azure_manager.get_powerbi_report(report_id) | ||
return {"report": report} | ||
|
||
|
||
@powerbi_router.get("/powerbi/workspaces/") | ||
async def get_powerbi_workspaces(): | ||
azure_manager = AzureManager() | ||
workspaces = await azure_manager.get_powerbi_workspaces() | ||
return {"workspaces": workspaces} |
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
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 |
---|---|---|
@@ -1,15 +1,95 @@ | ||
import json | ||
import traceback | ||
|
||
import httpx | ||
from azure.identity import ClientSecretCredential | ||
from settings import AZURE_APP_SECRET, AZURE_CLIENT_ID, AZURE_TENANT_ID | ||
from settings import AZURE_CLIENT_ID, AZURE_SECRET_VALUE, AZURE_TENANT_ID | ||
|
||
|
||
class AzureManager: | ||
def __init__(self): | ||
self.credential = ClientSecretCredential( | ||
AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_APP_SECRET | ||
AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_SECRET_VALUE | ||
) | ||
self.powerbi_token = self.credential.get_token( | ||
self.azure_token = self.credential.get_token( | ||
"https://analysis.windows.net/powerbi/api/.default" | ||
) | ||
|
||
def get_powerbi_token(self): | ||
return self.powerbi_token.token | ||
def get_azure_token(self): | ||
return self.azure_token.token | ||
|
||
async def get_powerbi_embeded_token(self, workspace_ids, dataset_ids, report_ids): | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {self.get_azure_token()}", | ||
} | ||
async with httpx.AsyncClient() as client: | ||
try: | ||
payload = { | ||
"targetWorkspaces": [ | ||
{"id": workspace_id} for workspace_id in workspace_ids | ||
], | ||
"datasets": [ | ||
{"id": dataset_id, "xmlaPermissions": "ReadOnly"} | ||
for dataset_id in dataset_ids | ||
], | ||
"reports": [ | ||
{"id": report_id, "allowEdit": True} for report_id in report_ids | ||
], | ||
} | ||
response = await client.post( | ||
"https://api.powerbi.com/v1.0/myorg/GenerateToken", | ||
headers=headers, | ||
content=json.dumps(payload), | ||
) | ||
print(f"Response status code: {response.status_code}") | ||
token = response.json().get("token") | ||
return token | ||
except Exception as e: | ||
print(f"An error occurred: {e}") | ||
traceback.print_exc() | ||
return None | ||
|
||
async def get_powerbi_reports(self): | ||
headers = { | ||
"Content-Type": "application/json", | ||
"Authorization": f"Bearer {self.get_azure_token()}", | ||
} | ||
print(headers) | ||
async with httpx.AsyncClient() as client: | ||
try: | ||
response = await client.get( | ||
# "https://analysis.windows.net/powerbi/api/v1.0/myorg/reports", | ||
# headers=headers | ||
"https://api.powerbi.com/v1.0/myorg/groups/361eea67-d03b-4799-8779-7c1d6c175182/reports", | ||
headers=headers, | ||
) | ||
print(f"Response status code: {response.status_code}") | ||
if response.status_code == 200: | ||
response_json = response.json() | ||
print(response_json) | ||
return response_json | ||
else: | ||
print(f"Response content: {response.content}") | ||
return None | ||
except Exception as e: | ||
print(f"An error occurred: {e}") | ||
traceback.print_exc() | ||
return None | ||
|
||
async def get_powerbi_report(self, report_id): | ||
headers = {"Authorization": f"Bearer {self.get_azure_token()}"} | ||
async with httpx.AsyncClient() as client: | ||
response = await client.get( | ||
f"https://api.powerbi.com/v1.0/myorg/groups/361eea67-d03b-4799-8779-7c1d6c175182/reports/{report_id}", | ||
headers=headers, | ||
) | ||
return response.json() | ||
|
||
async def get_powerbi_workspaces(self): | ||
headers = {"Authorization": f"Bearer {self.get_azure_token()}"} | ||
async with httpx.AsyncClient() as client: | ||
response = await client.get( | ||
"https://api.powerbi.com/v1.0/myorg/groups", headers=headers | ||
) | ||
return response.json() |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Oops, something went wrong.