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 RenderPdfAFromDocumentLink(this DocumentGenerationClient documentGenerationClient, ConvertedDocumentDetails convertedDocumentDetails) { var ext = convertedDocumentDetails.DocumentFormat; try { DiagnosticLog($"Requesting document conversion from url {convertedDocumentDetails.Description}"); var documentGenerationProgress = await documentGenerationClient.RequestDocumentConversionToPdfA( convertedDocumentDetails.ConfigurationId, new DocumentConversionToPdfARequestDetails( convertedDocumentDetails.SourceDocumentUrl, convertedDocumentDetails.SourceDocumentFormat)) .ConfigureAwait(false); const int secondsToTimesUp = 30; var stopWatch = Stopwatch.StartNew(); while (stopWatch.Elapsed.TotalSeconds < secondsToTimesUp) { var documentGenerationRequestStatusCheck = await documentGenerationClient.GetDocumentGenerationProgress(documentGenerationProgress.Id) .ConfigureAwait(false); switch (documentGenerationRequestStatusCheck.State) { case DocumentGenerationState.Failed: DiagnosticLog( $"Document conversion failed for {convertedDocumentDetails.Description}: {documentGenerationRequestStatusCheck.FailReason}"); throw new Exception("Document generation failed for template: "); case DocumentGenerationState.Requested: DiagnosticLog($"{convertedDocumentDetails.Description}: 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(convertedDocumentDetails.Description, ext.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( $"{convertedDocumentDetails.Description}: Rendered document written to {filename}"); convertedDocumentDetails.LocalDownloadPath = filename; convertedDocumentDetails.FileSize = new FileInfo(filename).Length; convertedDocumentDetails.AnchorDetails = new AnchorDetails { Href = documentUri.Uri, Expiry = documentUri.UriExpiryTime, LinkText = $"download.{ext.Ext()}", }; } return; } DiagnosticLog($"{convertedDocumentDetails.Description}: Gave up after {secondsToTimesUp} seconds."); } catch (Exception e) { DiagnosticLog($"{e.Message}/{e.StackTrace}"); } }