Example #1
0
        private static HttpResponseMessage CreateInstallTicketResponse(Guid appId)
        {
            var ticket = new InstallTicket($"ticketContents{appId}", Guid.NewGuid(), DateTime.UtcNow.AddMinutes(10));

            return(new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(JsonConvert.SerializeObject(ticket))
            });
        }
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var appId         = Configuration[Constants.AppIdConfigurationKey];
            var packageKey    = Configuration[Constants.PackageKeyConfigurationKey];
            var ownerTenantId = Configuration[Constants.OwnerIdConfigurationKey];

            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            var    config      = JsonConvert.DeserializeObject <IDictionary <string, string> >(requestBody);

            var appInstallRequest = new CreateInstallTicketRequest
            {
                InstallDetails = new List <TemplateAppInstallDetails>
                {
                    new TemplateAppInstallDetails
                    {
                        AppId         = new Guid(appId),
                        PackageKey    = packageKey,
                        OwnerTenantId = new Guid(ownerTenantId),
                        Config        = new TemplateAppConfigurationRequest
                        {
                            Configuration = config
                        }
                    }
                }
            };

            InstallTicket ticket      = null;
            var           credentials = await SignIn(
                authorityUrl : string.Format(Constants.PowerBIApiAuthorityUrlFormat, ownerTenantId),
                resourceUrl : Constants.PowerBIApiResourceUrl,
                clientId : Configuration[Constants.ClientIdConfigurationKey],
                clientSecret : Configuration[Constants.ClientSecretConfigurationKey]);

            var endpointUrl = Constants.PowerBIApiUrl;

            using (var client = new PowerBIClient(new Uri(endpointUrl), credentials))
            {
                ticket = await client.TemplateApps.CreateInstallTicketAsync(appInstallRequest);
            }

            var baseUrl     = Constants.PowerBIApiBaseUrl;
            var redirectUrl = string.Format(Constants.RedirectUrlFormat, baseUrl, appId, packageKey.Replace("=", string.Empty), ownerTenantId);

            return(new ContentResult()
            {
                Content = RedirectWithData(redirectUrl, ticket.Ticket), ContentType = "text/html"
            });
        }