DocAI.cs 4.4 KB

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