-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBaseApi.php
More file actions
143 lines (127 loc) · 3.19 KB
/
Copy pathBaseApi.php
File metadata and controls
143 lines (127 loc) · 3.19 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
<?php
declare(strict_types=1);
/**
* Settings and variables linked to all API usage.
*/
namespace Mindee\V1\Http;
/**
* Default key name for the API key entry in environment variables.
*/
const API_KEY_ENV_NAME = 'MINDEE_API_KEY';
/**
* Default key name for the Base URL in environment variables.
*/
const BASE_URL_ENV_NAME = 'MINDEE_BASE_URL';
/**
* Default URL prefix for API calls.
*/
const BASE_URL_DEFAULT = 'https://api.mindee.net';
/**
* Default key name for CURL request timeout in environment variables.
*/
const REQUEST_TIMEOUT_ENV_NAME = 'MINDEE_REQUEST_TIMEOUT';
/**
* Default timeout value for curl requests.
*/
const TIMEOUT_DEFAULT = 120;
// phpcs:disable
include_once(dirname(__DIR__, 2) . '/version.php');
// phpcs:enable
use function call_user_func;
use function dirname;
use const Mindee\VERSION;
/**
* Get the User Agent to send for API calls.
*/
function getUserAgent(): string
{
$os = match (PHP_OS_FAMILY) {
"Darwin" => "macos",
default => strtolower(PHP_OS_FAMILY),
};
return 'mindee-api-php@v' . VERSION . ' php-v' . PHP_VERSION . ' ' . $os;
}
/**
* Base class for API settings.
*/
abstract class BaseApi
{
/**
* @var string|null API key.
*/
public ?string $apiKey = null;
/**
* @var integer Timeout for the request, in ms.
*/
public int $requestTimeout;
/**
* @var string Root of the URL to use for API calls.
*/
public string $urlRoot;
/**
* @var string Base for the root url. Used for testing purposes.
*/
public string $baseUrl;
/**
* Sets the base url.
*
* @param string $value Value for the base Url.
*/
protected function setBaseUrl(string $value): void
{
$this->baseUrl = $value;
}
/**
* Sets the default timeout.
*
* @param integer $value Value for the CURL timeout.
*/
protected function setTimeout(int $value): void
{
$this->requestTimeout = $value;
}
/**
* Sets values from the environment, if needed.
*
*/
protected function setFromEnv(): void
{
$envVars = [
BASE_URL_ENV_NAME => $this->setBaseUrl(...),
REQUEST_TIMEOUT_ENV_NAME => $this->setTimeout(...),
];
foreach ($envVars as $key => $func) {
$envVal = getenv($key) ?: '';
if ($envVal) {
call_user_func($func, $envVal);
error_log('Value ' . $key . ' was set from env.');
}
}
}
/**
* Sets the API key.
*
* @param string|null $apiKey Optional API key.
*/
protected function setAPIKey(?string $apiKey = null): void
{
$envVal = !getenv(API_KEY_ENV_NAME) ? '' : getenv(API_KEY_ENV_NAME);
if (!$apiKey) {
error_log('API key set from environment');
$this->apiKey = $envVal;
} else {
$this->apiKey = $apiKey;
}
}
/**
* @param string|null $apiKey API key.
*/
public function __construct(
?string $apiKey
) {
$this->setAPIKey($apiKey);
$this->baseUrl = BASE_URL_DEFAULT;
$this->requestTimeout = TIMEOUT_DEFAULT;
$this->setFromEnv();
}
}