DocAI.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 HttpClient _httpClient = null;
  24. private string _appName = null;
  25. private string _appVersion = null;
  26. private string _platform = null;
  27. private Uri _verifyUserUri = null;
  28. private Uri _redactInfoExtractUri = null;
  29. public DocAI(string appName, string appVersion)
  30. {
  31. _httpClient = new HttpClient();
  32. _appName = appName;
  33. _appVersion = appVersion;
  34. _platform = "uwp";
  35. _verifyUserUri = new Uri($"{DocAIDomain}/verify_user");
  36. _redactInfoExtractUri = new Uri($"{DocAIDomain}/redact_info_extract");
  37. }
  38. ~DocAI()
  39. {
  40. _httpClient.Dispose();
  41. }
  42. #region Public Method
  43. public void SetAccessToken(string accessToken)
  44. {
  45. _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
  46. }
  47. public async Task<VerifyUserResponse> VerifyUser()
  48. {
  49. VerifyUserResponse response = null;
  50. HttpResponseMessage result = null;
  51. try
  52. {
  53. result = await _httpClient.PostAsync(_verifyUserUri, new MultipartFormDataContent());
  54. }
  55. catch { }
  56. if (result != null && result.StatusCode == System.Net.HttpStatusCode.OK)
  57. {
  58. var jsonString = await result.Content.ReadAsStringAsync();
  59. response = JsonTool.DeserializeJSON<VerifyUserResponse>(jsonString);
  60. }
  61. return response;
  62. }
  63. public async Task<RedactInfoExtractResponse> RedactInfoExtract(StorageFile file, string fileName, string md5, string redactInfo, string docType, string pageRange = null, string redactCustomInfo = null)
  64. {
  65. RedactInfoExtractResponse response = null;
  66. using (var fileStream = await file.OpenStreamForReadAsync())
  67. {
  68. var content = new MultipartFormDataContent();
  69. var streamContent = new StreamContent(fileStream);
  70. streamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
  71. streamContent.Headers.ContentDisposition.Name = "redact_file";
  72. streamContent.Headers.ContentDisposition.FileName = fileName;
  73. content.Add(streamContent);
  74. content.Add(CreateStringContent(md5, "md5"));
  75. content.Add(CreateStringContent(redactInfo, "redact_info"));
  76. content.Add(CreateStringContent(_appName, "app_name"));
  77. content.Add(CreateStringContent(_appVersion, "app_version"));
  78. content.Add(CreateStringContent(_platform, "platform"));
  79. content.Add(CreateStringContent(docType, "doc_type"));
  80. if (!string.IsNullOrEmpty(redactCustomInfo))
  81. content.Add(CreateStringContent(redactCustomInfo, "redact_custom_info"));
  82. if (!string.IsNullOrEmpty(pageRange))
  83. content.Add(CreateStringContent(pageRange, "page_range"));
  84. HttpResponseMessage result = null;
  85. try
  86. {
  87. result = await _httpClient.PostAsync(_redactInfoExtractUri, content);
  88. }
  89. catch { }
  90. if (result != null && result.StatusCode == System.Net.HttpStatusCode.OK)
  91. {
  92. var jsonString = await result.Content.ReadAsStringAsync();
  93. response = JsonTool.DeserializeJSON<RedactInfoExtractResponse>(jsonString);
  94. }
  95. }
  96. return response;
  97. }
  98. #endregion
  99. #region Private Method
  100. private StringContent CreateStringContent(string content, string name)
  101. {
  102. var stringContent = new StringContent(content);
  103. stringContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
  104. stringContent.Headers.ContentDisposition.Name = name;
  105. return stringContent;
  106. }
  107. #endregion
  108. }
  109. }