-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEndpoint.php
More file actions
145 lines (131 loc) · 4.03 KB
/
Copy pathEndpoint.php
File metadata and controls
145 lines (131 loc) · 4.03 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
<?php
namespace Mindee\Http;
use Mindee\Input\InputSource;
use Mindee\Input\LocalInputSource;
use Mindee\Input\PredictMethodOptions;
use Mindee\Input\URLInputSource;
/**
* Endpoint management.
*/
class Endpoint extends BaseEndpoint
{
/**
* @var string Url (name) of then endpoint.
*/
public string $urlName;
/**
* @var string Name of the endpoint's owner.
*/
public string $owner;
/**
* @var string Version of the endpoint.
*/
public string $version;
/**
* @param string $urlName Url (name) of the endpoint.
* @param string $owner Name of the endpoint's owner.
* @param string $version Version of the endpoint.
* @param MindeeApi $settings Settings for the endpoint.
*/
public function __construct(
string $urlName,
string $owner,
string $version,
MindeeApi $settings
) {
parent::__construct($settings);
$this->urlName = $urlName;
$this->owner = $owner;
$this->version = $version;
}
/**
* Retrieves a document from its queue ID.
*
* @param string $queueId ID of the queue to poll.
* @return array
*/
public function documentQueueReqGet(string $queueId): array
{
return $this->initCurlSessionGet($queueId);
}
/**
* Sends a document for asynchronous enqueuing.
*
* @param InputSource $fileCurl File to upload.
* @param PredictMethodOptions $options Prediction Options.
* @return array
*/
public function predictRequestPost(
InputSource $fileCurl,
PredictMethodOptions $options
): array {
return $this->initCurlSessionPost($fileCurl, $options, false);
}
/**
* Sends a document for synchronous enqueuing.
*
* @param InputSource $fileCurl File to upload.
* @param PredictMethodOptions $options Prediction Options.
* @return array
*/
public function predictAsyncRequestPost(
InputSource $fileCurl,
PredictMethodOptions $options
): array {
return $this->initCurlSessionPost(
$fileCurl,
$options,
true
);
}
/**
* Starts a CURL session, using POST.
*
* @param InputSource $inputSource File to upload.
* @param PredictMethodOptions $options Prediction Options.
* @param boolean $async Whether to use the async endpoint.
* @return array
*/
private function initCurlSessionPost(
InputSource $inputSource,
PredictMethodOptions $options,
bool $async
): array {
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
[
'Authorization: Token ' . $this->settings->apiKey,
]
);
$suffix = $async ? '/predict_async' : '/predict';
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->settings->requestTimeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$postFields = null;
if ($inputSource instanceof URLInputSource) {
$postFields = ['document' => $inputSource->url];
} elseif ($inputSource instanceof LocalInputSource) {
$inputSource->checkNeedsFix();
$postFields = ['document' => $inputSource->fileObject];
}
if ($options->predictOptions->includeWords) {
$postFields['include_mvision'] = 'true';
}
if ($options->predictOptions->fullText) {
$params['full_text_ocr'] = 'true';
}
if ($options->rag) {
$params['rag'] = 'true';
}
if ($options->predictOptions->cropper) {
$params['cropper'] = 'true';
}
if (!empty($params)) {
$suffix .= '?' . http_build_query($params);
}
return $this->setFinalCurlOpts($ch, $suffix, $postFields, $options->workflowId);
}
}