-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·132 lines (106 loc) · 2.79 KB
/
index.js
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
var rp = require('request-promise')
var fs = require('fs')
function Client (authToken) {
// internal
var generateUrl = function (endpoint) {
return 'https://api.docparser.com/v1/' + endpoint
}
var parseJson = function (body, response, resolveWithFullResponse) {
return JSON.parse(body)
}
this.httpClient = rp.defaults({
headers: {
'api_key': authToken
},
transform: parseJson,
transform2xxOnly: true
})
// api methods
this.ping = function () {
return this.httpClient.get(generateUrl('ping'))
}
this.getParsers = function () {
return this.httpClient.get(generateUrl('parsers'))
}
this.getParserModelLayouts = function (parserId) {
return this.httpClient.get(generateUrl('parser/models/' + parserId));
}
this.fetchDocumentFromURL = function (parserId, remoteURL, options) {
var endpoint = generateUrl('document/fetch/' + parserId)
var request = {
url: remoteURL
}
if (options === undefined) {
options = {}
}
if (options.remote_id) {
request.remote_id = options.remote_id
}
return this.httpClient.post({
url: endpoint,
formData: request
})
}
this.uploadFileByPath = function (parserId, filePath, options) {
// check file existence
if (fs.existsSync(filePath)) {
// create read stream
var stream = fs.createReadStream(filePath)
// upload using uploadFileByStream method
return this.uploadFileByStream(parserId, stream, options)
}
}
this.uploadFileByStream = function (parserId, stream, options) {
var endpoint = generateUrl('document/upload/' + parserId)
var request = {
file: {
value: stream,
options: {
filename: null
}
}
}
if (options === undefined) {
options = {}
}
if (options.remote_id) {
request.remote_id = options.remote_id
}
if (options.filename) {
request.file.options.filename = options.filename
}
return this.httpClient.post({
url: endpoint,
formData: request
})
}
this.getResultsByDocument = function (parserId, documentId, options) {
var endpoint = generateUrl('results/' + parserId + '/' + documentId)
if (options === undefined) {
options = {}
}
if (!options.format) {
options.format = 'object'
}
return this.httpClient.get({
url: endpoint,
qs: options
})
}
this.getResultsByParser = function (parserId, options) {
var endpoint = generateUrl('results/' + parserId)
if (options === undefined) {
options = {}
}
if (!options.format) {
options.format = 'object'
}
return this.httpClient.get({
url: endpoint,
qs: options
})
}
// constructor
this.authToken = authToken
}
module.exports.Client = Client