DocAI.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. var result = await _httpClient.PostAsync(_verifyUserUri, new MultipartFormDataContent());
  51. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  52. {
  53. var jsonString = await result.Content.ReadAsStringAsync();
  54. response = JsonTool.DeserializeJSON<VerifyUserResponse>(jsonString);
  55. }
  56. return response;
  57. }
  58. public async Task<RedactInfoExtractResponse> RedactInfoExtract(StorageFile file, string redactInfo, string docType, string pageRange = null, string redactCustomInfo = null)
  59. {
  60. RedactInfoExtractResponse response = null;
  61. using (var fileStream = await file.OpenStreamForReadAsync())
  62. {
  63. var content = new MultipartFormDataContent();
  64. var streamContent = new StreamContent(fileStream);
  65. streamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
  66. streamContent.Headers.ContentDisposition.Name = "redact_file";
  67. streamContent.Headers.ContentDisposition.FileName = file.Name;
  68. content.Add(streamContent);
  69. content.Add(CreateStringContent(redactInfo, "redact_info"));
  70. content.Add(CreateStringContent(_appName, "app_name"));
  71. content.Add(CreateStringContent(_appVersion, "app_version"));
  72. content.Add(CreateStringContent(_platform, "platform"));
  73. content.Add(CreateStringContent(docType, "doc_type"));
  74. if (!string.IsNullOrEmpty(redactCustomInfo))
  75. content.Add(CreateStringContent(redactCustomInfo, "redact_custom_info"));
  76. if (!string.IsNullOrEmpty(pageRange))
  77. content.Add(CreateStringContent(pageRange, "page_range"));
  78. var result = await _httpClient.PostAsync(_redactInfoExtractUri, content);
  79. if (result.StatusCode == System.Net.HttpStatusCode.OK)
  80. {
  81. var jsonString = await result.Content.ReadAsStringAsync();
  82. response = JsonTool.DeserializeJSON<RedactInfoExtractResponse>(jsonString);
  83. }
  84. }
  85. return response;
  86. }
  87. #endregion
  88. #region Private Method
  89. private StringContent CreateStringContent(string content, string name)
  90. {
  91. var stringContent = new StringContent(content);
  92. stringContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
  93. stringContent.Headers.ContentDisposition.Name = name;
  94. return stringContent;
  95. }
  96. #endregion
  97. }
  98. }