-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathClient.php
More file actions
177 lines (156 loc) · 6.13 KB
/
Copy pathClient.php
File metadata and controls
177 lines (156 loc) · 6.13 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
<?php
namespace Mindee\V2;
use Mindee\ClientOptions\PollingOptions;
use Mindee\CustomSleepMixin;
use Mindee\Error\MindeeException;
use Mindee\Input\InputSource;
use Mindee\V2\ClientOptions\BaseParameters;
use Mindee\V2\HTTP\MindeeAPIV2;
use Mindee\V2\Parsing\Inference\BaseResponse;
use Mindee\V2\Parsing\JobResponse;
use Mindee\V2\Product\Extraction\Params\ExtractionParameters;
/**
* Mindee Client V2.
*/
class Client
{
use CustomSleepMixin;
/**
* @var MindeeAPIV2 Mindee API V2.
*/
protected MindeeAPIV2 $mindeeApi;
/**
* Mindee Client V2.
*
* @param string|null $apiKey Optional API key. Will fall back to environment variable if not provided.
*/
public function __construct(?string $apiKey = null)
{
$this->mindeeApi = new MindeeAPIV2($apiKey ?: getenv('MINDEE_V2_API_KEY'));
}
/**
* Send the document to an asynchronous endpoint and return its ID in the queue.
*
* @param InputSource $inputSource File to parse.
* @param ExtractionParameters $params Parameters relating to prediction options.
* @return JobResponse A JobResponse containing the job (queue) corresponding to a document.
* @throws MindeeException Throws if the input document is not provided.
* @category Asynchronous
*/
public function enqueueInference(
InputSource $inputSource,
ExtractionParameters $params
): JobResponse {
return $this->enqueue($inputSource, $params);
}
/**
* Send the document to an asynchronous endpoint and return its ID in the queue.
* @param InputSource $inputSource File to parse.
* @param BaseParameters $params Parameters relating to prediction options.
* @return JobResponse A JobResponse containing the job (queue) corresponding to a document.
* @throws MindeeException Throws if the input document is not provided.
* @category Asynchronous
*/
public function enqueue(
InputSource $inputSource,
BaseParameters $params
): JobResponse {
return $this->mindeeApi->reqPostEnqueue($inputSource, $params);
}
/**
* @template T of BaseResponse
* @param string $responseClass The response class to construct.
* @phpstan-param class-string<T> $responseClass
* @param string $resultUrl URL of the result.
* @return BaseResponse A response containing parsing results.
*/
public function getResultFromUrl(
string $responseClass,
string $resultUrl
): BaseResponse {
return $this->mindeeApi->reqGetResultFromUrl($responseClass, $resultUrl);
}
/**
* @template T of BaseResponse
* @param string $responseClass The response class to construct.
* @phpstan-param class-string<T> $responseClass
* @param string $resultId ID of the result.
* @return BaseResponse A response containing parsing results.
*/
public function getResult(
string $responseClass,
string $resultId
): BaseResponse {
return $this->mindeeApi->reqGetResult($responseClass, $resultId);
}
/**
* Get the status of an inference that was previously enqueued.
* Can be used for polling.
*
* @param string $jobId ID of the queue to poll.
* @return JobResponse A JobResponse containing a Job, which also contains a Document if the parsing is complete.
* @category Asynchronous
*/
public function getJob(string $jobId): JobResponse
{
return $this->mindeeApi->reqGetJob($jobId);
}
/**
* Send a document to an endpoint and poll the server until the result is sent or
* until the maximum number of tries is reached.
*
* @template T of BaseResponse
* @param string $responseClass The response class to construct.
* @phpstan-param class-string<T> $responseClass
* @param InputSource $inputDoc Input document to parse.
* @param BaseParameters $params Parameters relating to prediction options.
* @param PollingOptions|null $pollingOptions Options to apply to the polling.
* @return BaseResponse A response containing parsing results.
* @throws MindeeException Throws if enqueueing fails, job fails, or times out.
*/
public function enqueueAndGetResult(
string $responseClass,
InputSource $inputDoc,
BaseParameters $params,
?PollingOptions $pollingOptions = null
): BaseResponse {
if (!$pollingOptions) {
$pollingOptions = new PollingOptions();
}
$enqueueResponse = $this->enqueue($inputDoc, $params);
if (empty($enqueueResponse->job->id)) {
error_log("Failed enqueueing:\n" . json_encode($enqueueResponse));
throw new MindeeException("Enqueueing of the document failed.");
}
$jobId = $enqueueResponse->job->id;
error_log("Successfully enqueued document with job ID: " . $jobId);
$this->customSleep($pollingOptions->initialDelaySec);
$retryCounter = 1;
$pollResults = $this->getJob($jobId);
while ($retryCounter < $pollingOptions->maxRetries) {
if ($pollResults->job->status === "Failed") {
break;
}
if ($pollResults->job->status === "Processed") {
return $this->getResultFromUrl($responseClass, $pollResults->job->resultUrl);
}
error_log(
"Polling server for parsing result with job ID: " . $jobId .
". Attempt number " . $retryCounter . " of " . $pollingOptions->maxRetries .
". Job status: " . $pollResults->job->status
);
$this->customSleep($pollingOptions->delaySec);
$pollResults = $this->getJob($jobId);
$retryCounter++;
}
if ($pollResults->job->error) {
throw new MindeeException(
"Job failed: " . ($pollResults->job->error->detail ?? 'Unknown error')
);
}
throw new MindeeException(
"Asynchronous parsing request timed out after " .
($pollingOptions->delaySec * $retryCounter) . " seconds"
);
}
}