-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBaseEndpoint.php
More file actions
85 lines (76 loc) · 2.57 KB
/
Copy pathBaseEndpoint.php
File metadata and controls
85 lines (76 loc) · 2.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
<?php
declare(strict_types=1);
namespace Mindee\V1\Http;
use CurlHandle;
/**
* Abstract class for endpoints.
*/
abstract class BaseEndpoint
{
/**
* @param MindeeApi|MindeeWorkflowApi $settings Input settings.
*/
public function __construct(public MindeeApi|MindeeWorkflowApi $settings) {}
/**
* Starts a CURL session using GET.
*
* @param string $queueId ID of the queue to poll.
* @return array{data: string|bool, code: int}
*/
protected function initCurlSessionGet(string $queueId): array
{
$ch = curl_init();
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
[
'Authorization: Token ' . $this->settings->apiKey,
]
);
curl_setopt($ch, CURLOPT_URL, $this->settings->urlRoot . "/documents/queue/$queueId");
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->settings->requestTimeout);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, getUserAgent());
$resp = [
'data' => curl_exec($ch),
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
];
curl_close($ch);
return $resp;
}
/**
* @param CurlHandle $ch Curl Channel.
* @param string $suffix Optional suffix for the url call.
* @param array<string, string|array<mixed>|boolean>|null $postFields Post fields.
* @param string|null $workflowId Optional ID of the workflow.
* @return array{data: string|bool, code: int} Final response.
*/
public function setFinalCurlOpts(
CurlHandle $ch,
string $suffix,
?array $postFields,
?string $workflowId = null
): array {
if (isset($workflowId)) {
$url = $this->settings->baseUrl . "/v1/workflows/" . $workflowId . $suffix;
} else {
$url = $this->settings->urlRoot . $suffix;
}
curl_setopt($ch, CURLOPT_URL, $url);
if ($postFields !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, getUserAgent());
$resp = [
'data' => curl_exec($ch),
'code' => curl_getinfo($ch, CURLINFO_HTTP_CODE),
];
curl_close($ch);
return $resp;
}
}