Untitled

 avatar
unknown
plain_text
18 days ago
5.0 kB
2
Indexable
import azure.functions as func
import logging
import json
import os
from utils.powerbi_utils import get_access_token, get_pbi_datasets, get_all_groups_and_datasets, upload_datasets_to_blob_by_group

app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)

@app.function_name(name="GetDatasets")
@app.route(route="get-datasets", methods=["GET"])
def get_datasets_func(req: func.HttpRequest) -> func.HttpResponse:
    logging.info("Calling Power BI REST API: Get Datasets")

        tenant_id = os.environ.get("TENANT_ID")
            client_id = os.environ.get("CLIENT_ID")
                client_secret = os.environ.get("CLIENT_SECRET")

                    try:
                            token = get_access_token(tenant_id, client_id, client_secret)
                                    datasets = get_pbi_datasets(token)
                                            return func.HttpResponse(
                                                            json.dumps(datasets),
                                                                        mimetype="application/json",
                                                                                    status_code=200
                                            )
                                                except Exception as e:
                                                        logging.error(f"Error: {e}")
                                                                return func.HttpResponse(str(e), status_code=500)

                                                                @app.function_name(name="GetAllDatasets")
                                                                @app.route(route="getalldatasets", methods=["GET"])
                                                                def get_all_datasets(req: func.HttpRequest) -> func.HttpResponse:
                                                                    logging.info("Fetching all datasets from all Power BI workspaces...")

                                                                        try:
                                                                                all_data = get_all_groups_and_datasets()
                                                                                        return func.HttpResponse(json.dumps(all_data, indent=2), mimetype="application/json", status_code=200)
                                                                                            except Exception as e:
                                                                                                    logging.error(f"Error fetching datasets: {str(e)}")
                                                                                                            return func.HttpResponse(f"Error: {str(e)}", status_code=500)

                                                                                                            @app.function_name(name="SaveStructuredDatasets")
                                                                                                            @app.route(route="savedatasetsstructured", methods=["GET"])
                                                                                                            def save_structured_datasets(req: func.HttpRequest) -> func.HttpResponse:
                                                                                                                try:
                                                                                                                        logging.info("Fetching and uploading structured datasets to Blob Storage...")

                                                                                                                                all_group_data = get_all_groups_and_datasets()
                                                                                                                                        upload_datasets_to_blob_by_group(all_group_data)

                                                                                                                                                return func.HttpResponse("Datasets structured and uploaded to blob storage.", status_code=200)
                                                                                                                                                    except Exception as e:
                                                                                                                                                            logging.error(f"Error: {str(e)}")
                                                                                                                                                                    return func.HttpResponse(f"Error: {str(e)}", status_code=500)
                                                                                                                                                                    
                                            )
Editor is loading...
Leave a Comment