123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- 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<VerifyUserResponse> VerifyUser()
- {
- VerifyUserResponse response = null;
- var result = await _httpClient.PostAsync(_verifyUserUri, new MultipartFormDataContent());
- if (result.StatusCode == System.Net.HttpStatusCode.OK)
- {
- var jsonString = await result.Content.ReadAsStringAsync();
- response = JsonTool.DeserializeJSON<VerifyUserResponse>(jsonString);
- }
- return response;
- }
- public async Task<RedactInfoExtractResponse> RedactInfoExtract(StorageFile file, string redactInfo, string docType, string pageRange = null, string redactCustomInfo = null)
- {
- RedactInfoExtractResponse response = 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();
- response = JsonTool.DeserializeJSON<RedactInfoExtractResponse>(jsonString);
- }
- }
- return response;
- }
- #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
- }
- }
|