public static async Task ProcessRecord(WebApiRequestRecord record, IOcrClient ocrClient, int idx) { WebApiResponseRecord waRecord = new WebApiResponseRecord(); record.Data.TryGetValue("formUrl", out object imgFile); record.Data.TryGetValue("formSasToken", out object sasToken); string imgFileWithSaS = imgFile.ToString() + sasToken.ToString(); string fileType = imgFile.ToString().Substring(imgFile.ToString().LastIndexOf(".")); string localFile = Path.GetTempPath() + "\\" + "temp_" + idx.ToString() + fileType; using (var client = new WebClient()) { client.DownloadFile(imgFileWithSaS, localFile); } // Process image // You could also call ProcessDocumentAsync or any other processing method declared below var resultUrls = await ProcessImageAsync(ocrClient, localFile); //Get results - the first doc is a docx, second is a text file using (var client = new WebClient()) { waRecord.Data.Add("content", client.DownloadString(resultUrls[1].ToString())); } File.Delete(Path.GetTempPath() + "\\" + "temp_" + idx.ToString() + fileType); waRecord.RecordId = record.RecordId; bag.Add(waRecord); }
private async Task <WebApiResponseRecord> ProcessInvoiceRecord(WebApiRequestRecord webApiRequestRecord, WebApiResponseRecord webApiResponseRecord) { var formUrl = webApiRequestRecord.Data["formUrl"] as string; _log.LogInformation($"{ServiceConstants.FormAnalyzerServiceName} - Got form URL: {formUrl}"); var analysisResult = await ProcessInvoiceDocumentContent(formUrl); webApiResponseRecord.Data = new Dictionary <string, object>(); var invoiceData = new InvoiceData(); if (analysisResult.documentResults != null) { var documents = analysisResult.documentResults; foreach (var documentResult in documents) { var documentFields = documentResult.fields; if (documentFields != null) { if (documentFields.Charges != null) { webApiResponseRecord.Data.Add(documentFields.Charges.fieldName, documentFields.Charges.text); invoiceData.Charges = documentFields.Charges.text; } else { _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'Charges' for the form with URL: {formUrl}"); } if (documentFields.ForCompany != null) { webApiResponseRecord.Data.Add(documentFields.ForCompany.fieldName, documentFields.ForCompany.text); invoiceData.ForCompany = documentFields.ForCompany.text; } else { _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'ForCompany' for the form with URL: {formUrl}"); } if (documentFields.FromCompany != null) { webApiResponseRecord.Data.Add(documentFields.FromCompany.fieldName, documentFields.FromCompany.text); invoiceData.FromCompany = documentFields.FromCompany.text; } else { _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'FromCompany' for the form with URL: {formUrl}"); } if (documentFields.InvoiceDate != null) { webApiResponseRecord.Data.Add(documentFields.InvoiceDate.fieldName, documentFields.InvoiceDate.text); invoiceData.InvoiceDate = documentFields.InvoiceDate.text; } else { _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'InvoiceDate' for the form with URL: {formUrl}"); } if (documentFields.InvoiceDueDate != null) { webApiResponseRecord.Data.Add(documentFields.InvoiceDueDate.fieldName, documentFields.InvoiceDueDate.text); invoiceData.InvoiceDueDate = documentFields.InvoiceDueDate.text; } else { _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'InvoiceDueDate' for the form with URL: {formUrl}"); } if (documentFields.InvoiceNumber != null) { webApiResponseRecord.Data.Add(documentFields.InvoiceNumber.fieldName, documentFields.InvoiceNumber.text); invoiceData.InvoiceNumber = documentFields.InvoiceNumber.text; } else { _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'InvoiceNumber' for the form with URL: {formUrl}"); } if (documentFields.VatID != null) { webApiResponseRecord.Data.Add(documentFields.VatID.fieldName, documentFields.VatID.text); invoiceData.VatID = documentFields.VatID.text; } else { _log.LogWarning($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get field: 'VatID' for the form with URL: {formUrl}"); } } else { _log.LogError($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get any fields from the form with URL: {formUrl}"); } } } else { _log.LogError($"{ServiceConstants.FormAnalyzerServiceName} - Cannot get any document results from the form with URL: {formUrl}"); } await _dataService.AddAsync(invoiceData); return(webApiResponseRecord); }