private static async Task Run(AppConfiguration configuration) { var validator = new ConfigurationValidator(configuration); if (!validator.Validate()) { return; } using (var httpClient = new HttpClient()) using (var tokenProviderFactory = new LogicTokenProviderFactory(configuration.TokenProvider)) { var documentGenerationClient = new DocumentGenerationClient(httpClient, tokenProviderFactory, configuration.DocumentGeneration); var configurationId = configuration.GenerationSample.ConfigurationId ?? throw new Exception("Validation should have reported no ConfigurationId set"); var hierarchyPath = configuration.GenerationSample.HierarchyPath; var subject = configuration.GenerationSample.Subject; var mergeData = JObject.Parse(File.ReadAllText($"values/{CustomerDataFileName}")); try { var templates = (await documentGenerationClient.GetTemplates(configurationId, hierarchyPath, subject) .ConfigureAwait(false)) .ToDictionary(t => t.TemplateId, t => t); var allFormats = Enum.GetValues(typeof(DocumentFormat)).Cast <DocumentFormat>().ToArray(); var documentDetailsList = new List <DocumentDetails>(); foreach (var templateId in new[] { BasicWordTemplateId, WordWithPartialTemplateId, WordToSaveAsTxtTemplateId }) { if (!templates.TryGetValue(templateId, out var template)) { DiagnosticLog($"Unable to find {templateId} in the template storage area"); continue; } await GetMetaDataForTemplate(documentGenerationClient, configurationId, template, hierarchyPath).ConfigureAwait(false); foreach (var format in allFormats) { var documentDetails = new GeneratedDocumentDetails { ConfigurationId = configurationId, HierarchyPath = hierarchyPath, Template = template, MergeData = mergeData, DocumentFormat = format, Description = $"Generated from {template.TemplateId} to {format}", }; await documentGenerationClient.GenerateDocumentFromTemplate(documentDetails) .ConfigureAwait(false); if (documentDetails.AnchorDetails != null) { documentDetailsList.Add(documentDetails); if (documentDetails.AnchorDetails.Href != null) { var convertedDocumentDetails = new ConvertedDocumentDetails { ConfigurationId = configurationId, SourceDocumentUrl = documentDetails.AnchorDetails.Href, SourceDocumentFormat = format, DocumentFormat = DocumentFormat.Pdf, Description = $"Rendered from ({documentDetails.Description})", }; await documentGenerationClient.RenderPdfAFromDocumentLink(convertedDocumentDetails) .ConfigureAwait(false); if (convertedDocumentDetails.AnchorDetails != null) { documentDetailsList.Add(convertedDocumentDetails); } } } } } GeneratePageWithDocumentLinks(documentDetailsList); } catch (Exception e) { DiagnosticLog($"{e.Message}/{e.StackTrace}"); } } }
private static async Task GenerateDocumentFromTemplate(this DocumentGenerationClient documentGenerationClient, GeneratedDocumentDetails documentDetails) { try { DiagnosticLog($"Requesting document generation from template {documentDetails.Template.TemplateId}"); var documentGenerationProgress = await documentGenerationClient.RequestDocumentGeneration( documentDetails.ConfigurationId, new DocumentGenerationRequestDetails( documentDetails.HierarchyPath, documentDetails.Template.TemplateId, documentDetails.Template.Languages.FirstOrDefault() ?? string.Empty, documentDetails.DocumentFormat, documentDetails.MergeData, null, false, new Guid("11111111-1111-1111-1111-111111111111"))) .ConfigureAwait(false); const int secondsToTimesUp = 30; var stopWatch = Stopwatch.StartNew(); while (stopWatch.Elapsed.TotalSeconds < secondsToTimesUp) { var generationProgress = await documentGenerationClient.GetDocumentGenerationProgress(documentGenerationProgress.Id) .ConfigureAwait(false); switch (generationProgress.State) { case DocumentGenerationState.Failed: DiagnosticLog( $"Document generation failed for template: {documentDetails.Template.TemplateId}: {generationProgress.FailReason}"); throw new Exception("Document generation failed for template: "); case DocumentGenerationState.Requested: DiagnosticLog($"{documentDetails.Template.TemplateId}: document not yet ready."); Thread.Sleep(300); continue; case DocumentGenerationState.Completed: break; } var documentUri = await documentGenerationClient.GetDocumentGenerationUri(documentGenerationProgress.Id) .ConfigureAwait(false); if (documentUri?.Uri != null) { var filename = GetTempFilename(documentDetails.Template.TemplateId, documentDetails.DocumentFormat.Ext()); await using (var fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None)) { var response = await new HttpClient().GetAsync(documentUri.Uri).ConfigureAwait(false); await response.Content.CopyToAsync(fs).ConfigureAwait(false); } DiagnosticLog( $"{documentDetails.Template.TemplateId}: Generated document written to {filename}"); documentDetails.LocalDownloadPath = filename; documentDetails.FileSize = new FileInfo(filename).Length; documentDetails.AnchorDetails = new AnchorDetails { Href = documentUri.Uri, Expiry = documentUri.UriExpiryTime, LinkText = $"download.{documentDetails.DocumentFormat.Ext()}", }; } return; } DiagnosticLog($"{documentDetails.Template.TemplateId}: Gave up after {secondsToTimesUp} seconds."); } catch (Exception e) { DiagnosticLog($"{e.Message}/{e.StackTrace}"); } }