|
@@ -0,0 +1,115 @@
|
|
|
+using KdanCommon.DocAI.Data;
|
|
|
+using KdanCommon.Helpers;
|
|
|
+using Newtonsoft.Json;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+using System.Net.Http;
|
|
|
+using System.Net.Mime;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using Windows.Media.Protection.PlayReady;
|
|
|
+using Windows.Storage;
|
|
|
+using Windows.Web.Http.Headers;
|
|
|
+using System.Xml.Linq;
|
|
|
+using System.IO.Pipes;
|
|
|
+
|
|
|
+namespace KdanCommon.DocAI
|
|
|
+{
|
|
|
+ public class DocAI
|
|
|
+ {
|
|
|
+ // DocAI website : https://kdanai.azurewebsites.net/swagger/index.html
|
|
|
+
|
|
|
+ private static string DocAIDomain = "https://kdanai.azurewebsites.net";
|
|
|
+
|
|
|
+ private HttpClient _httpClient = null;
|
|
|
+ private string _appName = null;
|
|
|
+ private string _appVersion = null;
|
|
|
+ private string _platform = null;
|
|
|
+
|
|
|
+ private Uri _verifyUserUri = null;
|
|
|
+ private Uri _redactInfoExtractUri = null;
|
|
|
+
|
|
|
+ public DocAI(string appName, string appVersion)
|
|
|
+ {
|
|
|
+ _httpClient = new HttpClient();
|
|
|
+ _appName = appName;
|
|
|
+ _appVersion = appVersion;
|
|
|
+ _platform = "uwp";
|
|
|
+
|
|
|
+ _verifyUserUri = new Uri($"{DocAIDomain}/verify_user");
|
|
|
+ _redactInfoExtractUri = new Uri($"{DocAIDomain}/redact_info_extract");
|
|
|
+ }
|
|
|
+
|
|
|
+ ~DocAI()
|
|
|
+ {
|
|
|
+ _httpClient.Dispose();
|
|
|
+ }
|
|
|
+
|
|
|
+ #region Public Method
|
|
|
+ public void SetAccessToken(string accessToken)
|
|
|
+ {
|
|
|
+ _httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task VerifyUser()
|
|
|
+ {
|
|
|
+ var content = new MultipartFormDataContent();
|
|
|
+ var result = await _httpClient.PostAsync(_verifyUserUri, content);
|
|
|
+ if (result.StatusCode == System.Net.HttpStatusCode.OK)
|
|
|
+ {
|
|
|
+ var jsonString = await result.Content.ReadAsStringAsync();
|
|
|
+ var response = JsonTool.DeserializeJSON<VerifyUserResponse>(jsonString);
|
|
|
+ if (response != null)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task RedactInfoExtract(StorageFile file, string redactInfo, string docType, string pageRange = null, string redactCustomInfo = null)
|
|
|
+ {
|
|
|
+ using (var fileStream = await file.OpenStreamForReadAsync())
|
|
|
+ {
|
|
|
+ var content = new MultipartFormDataContent();
|
|
|
+ var streamContent = new StreamContent(fileStream);
|
|
|
+ streamContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
|
|
|
+ streamContent.Headers.ContentDisposition.Name = "redact_file";
|
|
|
+ streamContent.Headers.ContentDisposition.FileName = file.Name;
|
|
|
+ content.Add(streamContent);
|
|
|
+ content.Add(CreateStringContent(redactInfo, "redact_info"));
|
|
|
+ content.Add(CreateStringContent(_appName, "app_name"));
|
|
|
+ content.Add(CreateStringContent(_appVersion, "app_version"));
|
|
|
+ content.Add(CreateStringContent(_platform, "platform"));
|
|
|
+ content.Add(CreateStringContent(docType, "doc_type"));
|
|
|
+ if (!string.IsNullOrEmpty(redactCustomInfo))
|
|
|
+ content.Add(CreateStringContent(redactCustomInfo, "redact_custom_info"));
|
|
|
+ if (!string.IsNullOrEmpty(pageRange))
|
|
|
+ content.Add(CreateStringContent(pageRange, "page_range"));
|
|
|
+ var result = await _httpClient.PostAsync(_redactInfoExtractUri, content);
|
|
|
+ if (result.StatusCode == System.Net.HttpStatusCode.OK)
|
|
|
+ {
|
|
|
+ var jsonString = await result.Content.ReadAsStringAsync();
|
|
|
+ var response = JsonTool.DeserializeJSON<RedactInfoExtractResponse>(jsonString);
|
|
|
+ if (response != null)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region Private Method
|
|
|
+ private StringContent CreateStringContent(string content, string name)
|
|
|
+ {
|
|
|
+ var stringContent = new StringContent(content);
|
|
|
+ stringContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
|
|
|
+ stringContent.Headers.ContentDisposition.Name = name;
|
|
|
+ return stringContent;
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+}
|