DocAI.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using KdanCommon.DocAI.Data;
  2. using KdanCommon.Helpers;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Net.Http;
  9. using System.Net.Mime;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Windows.Media.Protection.PlayReady;
  13. using Windows.Storage;
  14. using Windows.Web.Http.Headers;
  15. using System.Xml.Linq;
  16. using System.IO.Pipes;
  17. namespace KdanCommon.DocAI
  18. {
  19. public class DocAI
  20. {
  21. // DocAI website : https://kdanai.azurewebsites.net/swagger/index.html
  22. private static string DocAIDomain = "https://kdanai.azurewebsites.net";
  23. private static string DocAIPreparingDomain = "https://kdanai-test.azurewebsites.net";
  24. private HttpClient _httpClient = null;
  25. private string _appName = null;
  26. private string _appVersion = null;
  27. private string _platform = null;
  28. private Uri _verifyUserUri = null;
  29. private Uri _redactInfoExtractUri = null;
  30. public DocAI(string appName, string appVersion, bool isPreparing)
  31. {
  32. _httpClient = new HttpClient();
  33. _appName = appName;
  34. _appVersion = appVersion;
  35. _platform = "uwp";
  36. if (isPreparing)
  37. {
  38. _verifyUserUri = new Uri($"{DocAIPreparingDomain}/verify_user");
  39. _redactInfoExtractUri = new Uri($"{DocAIPreparingDomain}/redact_info_extract");
  40. }
  41. else
  42. {
  43. _verifyUserUri = new Uri($"{DocAIDomain}/verify_user");
  44. _redactInfoExtractUri = new Uri($"{DocAIDomain}/redact_info_extract");
  45. }
  46. }
  47. ~DocAI()
  48. {
  49. _httpClient.Dispose();
  50. }
  51. #region Public Method
  52. public void SetAccessToken(string accessToken)
  53. {
  54. _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
  55. }
  56. public async Task<VerifyUserResponse> VerifyUser()
  57. {
  58. VerifyUserResponse response = null;
  59. HttpResponseMessage result = null;
  60. try
  61. {
  62. result = await _httpClient.PostAsync(_verifyUserUri, new MultipartFormDataContent());
  63. }
  64. catch { }
  65. if (result != null && result.StatusCode == System.Net.HttpStatusCode.OK)
  66. {
  67. var jsonString = await result.Content.ReadAsStringAsync();
  68. response = JsonTool.DeserializeJSON<VerifyUserResponse>(jsonString);
  69. }
  70. return response;
  71. }
  72. public async Task<RedactInfoExtractResponse> RedactInfoExtract(StorageFile file, string fileName, string md5, string redactInfo, string docType, string pageRange = null, string redactCustomInfo = null)
  73. {
  74. RedactInfoExtractResponse response = null;
  75. using (var fileStream = await file.OpenStreamForReadAsync())
  76. {
  77. var content = new MultipartFormDataContent();
  78. var streamContent = new StreamContent(fileStream);
  79. streamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
  80. streamContent.Headers.ContentDisposition.Name = "redact_file";
  81. streamContent.Headers.ContentDisposition.FileName = fileName;
  82. content.Add(streamContent);
  83. content.Add(CreateStringContent(md5, "md5"));
  84. content.Add(CreateStringContent(redactInfo, "redact_info"));
  85. content.Add(CreateStringContent(_appName, "app_name"));
  86. content.Add(CreateStringContent(_appVersion, "app_version"));
  87. content.Add(CreateStringContent(_platform, "platform"));
  88. content.Add(CreateStringContent(docType, "doc_type"));
  89. if (!string.IsNullOrEmpty(redactCustomInfo))
  90. content.Add(CreateStringContent(redactCustomInfo, "redact_custom_info"));
  91. if (!string.IsNullOrEmpty(pageRange))
  92. content.Add(CreateStringContent(pageRange, "page_range"));
  93. HttpResponseMessage result = null;
  94. try
  95. {
  96. result = await _httpClient.PostAsync(_redactInfoExtractUri, content);
  97. }
  98. catch { }
  99. if (result != null && result.StatusCode == System.Net.HttpStatusCode.OK)
  100. {
  101. var jsonString = await result.Content.ReadAsStringAsync();
  102. response = JsonTool.DeserializeJSON<RedactInfoExtractResponse>(jsonString);
  103. }
  104. }
  105. return response;
  106. }
  107. #endregion
  108. #region Private Method
  109. private StringContent CreateStringContent(string content, string name)
  110. {
  111. var stringContent = new StringContent(content);
  112. stringContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
  113. stringContent.Headers.ContentDisposition.Name = name;
  114. return stringContent;
  115. }
  116. #endregion
  117. }
  118. }