-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdocument-reader.ts
More file actions
executable file
·247 lines (224 loc) · 8.57 KB
/
document-reader.ts
File metadata and controls
executable file
·247 lines (224 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import { HealthcheckApi } from '../api/healthcheck-api';
import { ProcessApi } from '../api/process-api';
import { TransactionApi } from '../api/transaction-api';
import { ResourcesApi } from '../api/resources-api';
import { ProcessResult } from './process-result';
import { Configuration, ConfigurationParameters } from '../configuration';
import globalAxios, { AxiosInstance, AxiosResponse, RawAxiosRequestConfig } from 'axios';
import { BASE_PATH } from '../base';
import {
ProcessRequestImage,
Light,
ProcessRequest as ProcessRequestBase,
Scenario,
Result,
DeviceInfo,
TransactionProcessRequest,
TransactionProcessResult,
ListTransactionsByTagResponse,
TransactionProcessGetResponse,
Healthcheck,
DatabaseDocumentList,
} from '../models';
import { Base64String, instanceOfProcessRequest, ProcessRequestExt } from './process-request-ext';
import { ProcessRequestImageWrapper } from './process-request-image-wrapper';
import * as converter from 'base64-arraybuffer';
export class DocumentReaderApi {
private readonly healthcheckApi: HealthcheckApi;
private readonly processApi: ProcessApi;
private readonly transactionApi: TransactionApi;
private readonly resourcesApi: ResourcesApi;
private license: string | undefined;
constructor(
configuration?: ConfigurationParameters,
basePath: string = BASE_PATH,
axios: AxiosInstance = globalAxios,
) {
this.healthcheckApi = new HealthcheckApi(new Configuration(configuration), basePath, axios);
this.processApi = new ProcessApi(new Configuration(configuration), basePath, axios);
this.transactionApi = new TransactionApi(new Configuration(configuration), basePath, axios);
this.resourcesApi = new ResourcesApi(new Configuration(configuration), basePath, axios);
}
async doclist(options?: RawAxiosRequestConfig): Promise<DatabaseDocumentList> {
const axiosResult = await this.resourcesApi.doclist(options);
return axiosResult.data;
}
async ping(xRequestID?: string, options?: RawAxiosRequestConfig): Promise<DeviceInfo> {
const axiosResult = await this.healthcheckApi.ping(xRequestID, options);
return axiosResult.data;
}
async health(xRequestID?: string, options?: RawAxiosRequestConfig): Promise<Healthcheck> {
const axiosResult = await this.healthcheckApi.healthz(xRequestID, options);
return axiosResult.data;
}
/**
*
* @summary Process list of documents images and return extracted data
* @param {ProcessRequestExt} [request] Request options such as image, results types and etc.
* @param {string} xRequestID It allows the client and server to correlate each HTTP request.
* @param {*} [options] Override http request option.
* @throws {RequiredError} If some request params are missed
* */
async process(
request: ProcessRequestExt | ProcessRequestBase,
xRequestID?: string,
options?: RawAxiosRequestConfig,
): Promise<ProcessResult> {
let baseRequest;
if (instanceOfProcessRequest(request)) {
if (!request.processParam) {
request.processParam = {
scenario: Scenario.FULL_PROCESS,
resultTypeOutput: [Result.TEXT],
};
}
if (!request.systemInfo) {
request.systemInfo = {};
}
if (!request.systemInfo.license && this.license) {
request.systemInfo.license = this.license;
}
baseRequest = requestToBaseRequest(request);
} else {
baseRequest = request;
}
const axiosResult = await this.processApi.apiProcess(baseRequest, xRequestID, options);
return new ProcessResult(axiosResult.data);
}
public setLicense(license: ArrayBuffer | Base64String) {
if (typeof license === 'string') {
this.license = license;
} else {
this.license = bufferToBase64String(license);
}
}
/**
*
* @summary Reprocess
* @param {string} transactionId Transaction id
* @param {TransactionProcessRequest} transactionProcessRequest
* @param {boolean} useCache Get processed values from storage in case transaction has already processed.
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
public reprocessTransaction(
transactionId: string,
transactionProcessRequest: TransactionProcessRequest,
useCache?: boolean,
options?: RawAxiosRequestConfig,
): Promise<AxiosResponse<TransactionProcessResult, any>> {
return this.transactionApi.apiV2TransactionTransactionIdProcessPost(
transactionId,
transactionProcessRequest,
useCache,
options,
);
}
/**
*
* @summary Get Reprocess transaction result
* @param {string} transactionId Transaction id
* @param {boolean} [withImages] With base64 images or url
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async getReprocessTransactionResult(
transactionId: string,
withImages?: boolean,
options?: RawAxiosRequestConfig,
): Promise<ProcessResult> {
const axiosResult = await this.transactionApi.apiV2TransactionTransactionIdResultsGet(
transactionId,
withImages,
options,
);
return new ProcessResult(axiosResult.data);
}
/**
*
* @summary Get transactions by tag
* @param {string} tagId Tag id
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async getTransactionsByTag(
tagId: string,
options?: RawAxiosRequestConfig,
): Promise<AxiosResponse<ListTransactionsByTagResponse>> {
return this.transactionApi.apiV2TagTagIdTransactionsGet(tagId, options);
}
/**
*
* @summary Delete Reprocess transactions by tag
* @param {string} tagId Tag id
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async deleteReprocessTransactionsByTag(
tagId: string,
options?: RawAxiosRequestConfig,
): Promise<AxiosResponse<object, any>> {
return this.transactionApi.apiV2TagTagIdDelete(tagId, options);
}
/**
*
* @summary Get Reprocess transaction file
* @param {string} transactionId Transaction id
* @param {string} name File name
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async getReprocessTransactionFile(
transactionId: string,
name: string,
options?: RawAxiosRequestConfig,
): Promise<AxiosResponse<any, any>> {
return this.transactionApi.apiV2TransactionTransactionIdFileGet(transactionId, name, options);
}
/**
*
* @summary Get Reprocess transaction data
* @param {string} transactionId Transaction id
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async getReprocessTransactionData(
transactionId: string,
options?: RawAxiosRequestConfig,
): Promise<AxiosResponse<TransactionProcessGetResponse, any>> {
return this.transactionApi.apiV2TransactionTransactionIdGet(transactionId, options);
}
}
export function requestToBaseRequest(request: ProcessRequestExt): ProcessRequestBase {
const imageList: Array<ProcessRequestImage> = [];
request.images.forEach((image, index) => {
if (typeof image === 'string') {
imageList.push({ ImageData: { image: image }, light: Light.WHITE, page_idx: index });
} else if (image instanceof ArrayBuffer) {
const data = bufferToBase64String(image);
imageList.push({ ImageData: { image: data }, light: Light.WHITE, page_idx: index });
} else {
imageList.push(imageDataToBaseImageData(image, index));
}
});
return {
...request,
List: imageList,
};
}
function imageDataToBaseImageData(image: ProcessRequestImageWrapper, arrayIndex: number): ProcessRequestImage {
let data: string;
if (typeof image.ImageData === 'string') {
data = image.ImageData;
} else {
data = bufferToBase64String(image.ImageData);
}
return {
ImageData: { image: data },
light: image.light,
page_idx: typeof image.page_idx === 'undefined' ? arrayIndex : image.page_idx,
};
}
function bufferToBase64String(buffer: ArrayBuffer) {
return converter.encode(buffer);
}