-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathDocparser.php
More file actions
executable file
·290 lines (250 loc) · 7.22 KB
/
Copy pathDocparser.php
File metadata and controls
executable file
·290 lines (250 loc) · 7.22 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
// disclaimer stuff
namespace Docparser;
use DateTime;
/**
* Class Docparser
* @package Docparser
*
* this class provides the developer-facing interface to the Docparser API
*
*/
class Docparser
{
/**
* base URL
*/
const API_URL = 'https://api.docparser.com/v1/';
/**
* allowed types for getResultsByParser
*/
const ALLOWED_LIST_TYPES = [
'last_uploaded',
'uploaded_after',
'processed_after',
];
private $apiToken;
/**
* Docparser constructor.
*
* @api
* @param String $apiToken
*/
public function __construct($apiToken)
{
$this->apiToken = $apiToken;
}
/**
* this method is used for testing authentication.
*
* @api
* @return boolean indicates validity of auth credentials
* @throws DocparserApiException
*/
public function ping()
{
$request = $this->createRequest();
if ((bool) $request->makeGetRequest('ping')) {
return true;
}
return false;
}
/**
* retrieves the created document parsers
*
* @api
* @return array
* @throws DocparserApiException
*/
public function getParsers()
{
$request = $this->createRequest();
$response = $request->makeGetRequest('parsers');
return $response;
}
/**
* fetches all of the model layouts associated with a given parser
*
* @api
* @param $parserId
* @return bool|mixed|\Psr\Http\Message\ResponseInterface|string
*/
public function getParserModelLayouts($parserId)
{
$request = $this->createRequest();
$endpoint = 'parser/models/' . $parserId;
$response = $request->makeGetRequest($endpoint);
return $response;
}
/**
* uploads documents from a given file path
*
* @api
* @param $parserId
* @param $filePath
* @param null $remoteId
* @return bool|mixed|\Psr\Http\Message\ResponseInterface|string
* @throws DocparserApiException
*/
public function uploadDocumentByPath($parserId, $filePath, $remoteId = null)
{
if (!file_exists($filePath)) {
throw new DocparserApiException("No such file.");
}
if (is_dir($filePath)) {
throw new DocparserApiException("Passed a directory, expected file.");
}
return $this->uploadDocumentByContents($parserId, fopen($filePath, 'r'), $remoteId);
}
/**
* uploads document by content or file handle
*
* @api
* @param $parserId
* @param $file
* @param null $remoteId
* @param null $filename optional filename
* @return bool|mixed|\Psr\Http\Message\ResponseInterface|string
* @throws DocparserApiException
*/
public function uploadDocumentByContents($parserId, $file, $remoteId = null, $filename = null)
{
if (empty($file)) {
throw new DocparserApiException("Given file (handle) is empty");
}
$request = $this->createRequest();
$endpoint = 'document/upload/' . $parserId;
$response = $request->uploadDocument($endpoint, $file, $remoteId, $filename);
return $response;
}
/**
* fetches a document from a publicly accessible URL
*
* @api
* @param $parserId
* @param $url
* @param null $remoteId
* @return bool|mixed|\Psr\Http\Message\ResponseInterface|string
*/
public function fetchDocumentFromURL($parserId, $url, $remoteId = null)
{
$request = $this->createRequest();
$endpoint = 'document/fetch/' . $parserId;
$response = $request->makePostRequest($endpoint, [
'url' => $url,
'remote_id' => $remoteId
]);
return $response;
}
/**
* retrieve results by parser + document ID
*
* @api
* @param $parserId
* @param $documentId
* @param string $format
* @return bool|mixed|\Psr\Http\Message\ResponseInterface|string
*/
public function getResultsByDocument($parserId, $documentId, $format = 'object')
{
$request = $this->createRequest();
$endpoint = 'results/' . $parserId . '/' . $documentId;
$response = $request->makeGetRequest($endpoint, [
'format' => $format
]);
return $response;
}
/**
* retrieves all results of a particular parser
*
* the parameters limit and list are switched around compared to
* the order in the devdocs, because you're more likely to need the limit parameter
*
* *NOTE:* usage of this method is not recommended - it's better to
* use Docparser Webhooks
* @see https://dev.docparser.com/?shell#get-multiple-data-sets
*
* @api
*
* @param $parserId
*
* @param array $options
* optionally containing the keys:
* format, limit, list, date, includeProcessingQueue
*
* @return bool|mixed|\Psr\Http\Message\ResponseInterface|string
*/
public function getResultsByParser(
$parserId,
$options = []
) {
$format = (isset($options['format'])) ? $options['format'] : 'object';
$limit = (isset($options['limit'])) ? $options['limit'] : 100;
$list = 'last_uploaded';
$remoteId = null;
if (isset($options['remoteId'])) {
$remoteId = $options['remoteId'];
}
if (isset($options['list'])) {
$list = self::translateListValue($options['list']);
}
$date = self::translateDateValue(new DateTime());
if (isset($options['date'])) {
$date = self::translateDateValue($options['date']);
}
$includeProcessingQueue = false;
if (isset($options['includeProcessingQueue'])) {
$includeProcessingQueue = (bool) $options['includeProcessingQueue'];
}
$request = $this->createRequest();
$endpoint = 'results/' . $parserId;
$response = $request->makeGetRequest($endpoint, [
'format' => $format,
'list' => $list,
'limit' => $limit,
'date' => $date,
'remote_id' => $remoteId,
'include_processing_queue' => $includeProcessingQueue
]);
return $response;
}
/**
* creates a request object
*
* @interal
* @return DocparserApiRequest
*/
protected function createRequest()
{
return new DocparserApiRequest(self::API_URL, $this->apiToken);
}
/**
* @param $listParam
* @return string
*/
private static function translateListValue($listParam = null)
{
if (!in_array($listParam, self::ALLOWED_LIST_TYPES)) {
return 'last_uploaded';
}
return $listParam;
}
/**
* @param $date
* (DateTime instance or acceptable DateTime constructor parameter
*
* @return string (date as ISO 8601)
*/
private static function translateDateValue($date = null)
{
if (!$date) {
$date = '1970-01-01T00:00:00+0000';
}
if ($date instanceof DateTime) {
return $date->format('c');
} else {
$date = new DateTime($date);
return $date->format('c');
}
}
}