Exemple #1
0
        /// <summary>
        /// Creates a new instance of the PowerBIClient with the specified token
        /// </summary>
        /// <returns></returns>
        static async Task <PowerBIClient> CreateClient()
        {
            // Create a token credentials with "AppKey" type
            string accessToken = await PBIEHelper.GetPowerBIAPIAuthTokenAsync();

            var tokenCredentials = new TokenCredentials(accessToken, "Bearer");

            // Instantiate your Power BI client passing in the required credentials
            var client = new PowerBIClient(new Uri(ConfigurationManager.AppSettings["apiUrl"]), tokenCredentials);

            return(client);
        }
Exemple #2
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "workspaces/{workspaceid}/Reports")] HttpRequestMessage req, string workspaceId, TraceWriter log)
        {
            log.Info("GetReportsByWorkspace processed a request.");

            // get a list of workspaces the application user has access too
            var reports = await PBIEHelper.GetReportsInWorkspace(workspaceId);

            // convert the list to JSON
            var result = JsonConvert.SerializeObject(reports);

            // return the result
            return(req.CreateResponse(HttpStatusCode.OK, result));
        }
Exemple #3
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "workspaces/{workspaceid}/reports")] HttpRequestMessage req, string workspaceId, TraceWriter log)
        {
            log.Info("AddReport function processed a request.");

            HttpResponseMessage rtnResponse = null;

            try
            {
                // Get request body
                AddReportRequest requestparameters = await req.Content.ReadAsAsync <AddReportRequest>();

                // import the report
                var reportresult = PBIEHelper.ImportPbix(workspaceId, requestparameters.name, requestparameters.report).Result;

                foreach (Dataset dataset in reportresult.Datasets)
                {
                    PBIEHelper.UpdateDataSetConnectionString(workspaceId, dataset.Id, requestparameters);

                    // we're not dealing with an Azure SQL DB, so we may need to create a datasource
                    if (!requestparameters.server.ToLower().Contains(".database.windows.net"))
                    {
                        IEnumerable <GatewayDatasource> datasources = await PBIEHelper.GetGatewayDataSourcesAsync(workspaceId, dataset.Id);

                        if (datasources.Count() <= 0) // no associated gateway, need to create a datasource
                        {
                            // find matching gateway
                            Gateway gateway = await PBIEHelper.GetWorkspaceGatewaysAsync(requestparameters.gatewayname);

                            if (gateway != null)
                            {
                                await PBIEHelper.CreateGatewayDatasourceAsync(gateway, requestparameters.server, requestparameters.database, requestparameters.credentials);
                            }
                        }
                    }
                }

                rtnResponse = req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(reportresult.Id));
            }
            catch (Exception ex)
            {
                rtnResponse = req.CreateResponse(HttpStatusCode.InternalServerError, $"An unexpected error occured. This could be caused by any number of issues including invalid parameters or incorrect database connection details. Please check the request parameters and try again. Details: {ex.Message}");
            }


            return(rtnResponse);
        }
Exemple #4
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "post", Route = "workspace")] HttpRequestMessage req, TraceWriter log)
        {
            log.Info("CreateAppWorkspace processed a request.");

            HttpResponseMessage rtnResponse = null;
            string workspacename            = string.Empty;

            try
            {
                // Get request body
                dynamic data = await req.Content.ReadAsAsync <object>();

                workspacename = data.name;

                var workspace = PBIEHelper.CreateGroupAsync(workspacename).Result;

                rtnResponse = req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(workspace));
            }
            catch (RuntimeBinderException)
            {
                //  "name property doesn't exist in request body
                rtnResponse = req.CreateResponse(HttpStatusCode.BadRequest, @"Please pass the app workspace name in the body of your request: { ""name"" : ""myname"" }");
            }
            catch (AggregateException ex)
            {
                if (ex.InnerException.GetType().Equals(typeof(HttpOperationException)))
                {
                    // check to see if the workspace already exists
                    var workspace = PBIEHelper.GetAppWorkspaceByNameAsync(workspacename).Result;
                    rtnResponse = (workspace != null)
                        ? req.CreateResponse(HttpStatusCode.OK, JsonConvert.SerializeObject(workspace))
                        : req.CreateResponse(HttpStatusCode.BadRequest, @"An unexpected http error occured. An App Workspace of the name you specified may already exist. Did it contain invalid characters?");
                }
            }


            return(rtnResponse);
        }
Exemple #5
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "workspaces/{workspaceid}/reports/{reportId}")] HttpRequestMessage req, string workspaceId, string reportId, TraceWriter log)
        {
            log.Info("GetEmbeddedReportSettings function processed a request.");

            HttpResponseMessage returnResponse = req.CreateResponse(HttpStatusCode.OK);

            try
            {
                var settings = await PBIEHelper.GetEmbeddedReportSettings(workspaceId, reportId);

                var result = JsonConvert.SerializeObject(settings);

                // return the result
                returnResponse = req.CreateResponse(HttpStatusCode.OK, result);
            }
            catch (HttpOperationException ex)
            {
                if ((ex.Response.StatusCode == HttpStatusCode.BadRequest) || (ex.Response.StatusCode == HttpStatusCode.NotFound))
                {
                    returnResponse = req.CreateResponse(ex.Response.StatusCode, $"Request failed, Likely cause is that specified workspace or report could not be found. Details: {ex.Message}");
                }
                else if (ex.Response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    returnResponse = req.CreateResponse(ex.Response.StatusCode, $"Request failed, there appears to be an issue with access. Details: {ex.Message}");
                }
                else
                {
                    throw ex;
                }
            }
            catch (Exception ex)
            {
                returnResponse = req.CreateResponse(HttpStatusCode.BadRequest, $"An unknown error occured. Details: {ex.Message}");
            }

            return(returnResponse);
        }
Exemple #6
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "delete", Route = "workspaces/{workspaceid}/Reports/{reportId}")] HttpRequestMessage req, string workspaceId, string reportId, TraceWriter log)
        {
            log.Info("DeleteReport function processed a request.");

            HttpResponseMessage returnResponse = req.CreateResponse(HttpStatusCode.OK);

            try
            {
                await PBIEHelper.DeleteReportAsync(workspaceId, reportId);
            }
            catch (HttpOperationException ex)
            {
                if ((ex.Response.StatusCode == HttpStatusCode.BadRequest) || (ex.Response.StatusCode == HttpStatusCode.NotFound))
                {
                    returnResponse = req.CreateResponse(HttpStatusCode.BadRequest, $"Request failed, Likely cause is that specified report or its dataset could not be found. Details: {ex.Message}");
                }
            }
            catch (Exception ex)
            {
                returnResponse = req.CreateResponse(HttpStatusCode.BadRequest, $"An unknown error occured. Details: {ex.Message}");
            }

            return(returnResponse);
        }