-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.php
More file actions
executable file
·293 lines (247 loc) · 7.36 KB
/
HttpRequest.php
File metadata and controls
executable file
·293 lines (247 loc) · 7.36 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
291
292
293
<?php declare(strict_types = 1);
namespace Fapi\HttpClient;
use Fapi\HttpClient\Utils\Json;
use Fapi\HttpClient\Utils\JsonException;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;
use function base64_encode;
use function count;
use function file_exists;
use function http_build_query;
use function is_array;
use function is_bool;
use function is_int;
use function is_string;
use function json_encode;
use function var_dump;
class HttpRequest extends Request
{
/** @var array<mixed> */
private static array $defaults = ['verify' => true];
/**
* @param array<mixed> $headers
*/
public function __construct(
string $method,
UriInterface|string $uri,
array $headers = [],
StreamInterface|string|null $body = null,
string $version = '1.1',
)
{
if (!HttpMethod::isValid($method)) {
throw new InvalidArgumentException('Parameter method must be an HTTP method.');
}
parent::__construct($method, $uri, $headers, $body, $version);
}
/**
* @param array<mixed> $options
*/
public static function from(UriInterface|string $uri, string $method = HttpMethod::GET, array $options = []): self
{
$body = '';
$options = self::preProcessHeaders($options, $body);
return new self($method, $uri, $options, $body);
}
/**
* @param array<mixed> $options
* @return array<mixed>
*/
private static function preProcessHeaders(array $options, StreamInterface|string &$body): array
{
$data = self::$defaults;
if (isset($options['form_params'])) {
$value = $options['form_params'];
self::validateFormParamsOption($value);
$body = http_build_query($value, '', '&');
$data['Content-Type'] = 'application/x-www-form-urlencoded';
}
if (isset($options['headers'])) {
$value = $options['headers'];
self::validateHeadersOption($value);
$data += $value;
}
if (isset($options['auth'])) {
$value = $options['auth'];
self::validateAuthOption($value);
$data['Authorization'] = 'Basic ' . base64_encode($value[0] . ':' . $value[1]);
}
if (isset($options['body'])) {
$value = $options['body'];
self::validateBodyOption($value);
$body = $value;
}
if (isset($options['json'])) {
$value = $options['json'];
self::validateJsonOption($value);
$body = Json::encode($value);
$data['Content-Type'] = 'application/json';
}
if (isset($options['timeout'])) {
$value = $options['timeout'];
self::validateTimeoutOption($value);
$data['timeout'] = $value;
}
if (isset($options['connect_timeout'])) {
$value = $options['connect_timeout'];
self::validateConnectTimeoutOption($value);
$data['connect_timeout'] = $value;
}
if (isset($options['verify'])) {
$value = $options['verify'];
self::validateVerify($value);
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
$data['verify'] = $value;
}
if (isset($options['cert'])) {
$value = $options['cert'];
self::validateCertOption($value);
$data['cert'] = $value;
}
if (isset($options['ssl_key'])) {
$value = $options['ssl_key'];
self::validateSslKeyOption($value);
$data['ssl_key'] = $value;
}
return $data;
}
private static function validateFormParamsOption(mixed $formParams): void
{
if (!is_array($formParams)) {
throw new InvalidArgumentException('Form params must be an array.');
}
foreach ($formParams as $value) {
if (!is_string($value)) {
throw new InvalidArgumentException('Form param must be a string.');
}
}
}
private static function validateHeadersOption(mixed $headers): void
{
if (!is_array($headers)) {
throw new InvalidArgumentException('Headers must be an array.');
}
foreach ($headers as $values) {
if (is_array($values)) {
foreach ($values as $value) {
if (!is_string($value)) {
throw new InvalidArgumentException('Header value must be a string.');
}
}
} elseif (!is_string($values)) {
throw new InvalidArgumentException('Header must be an array or string.');
}
}
}
private static function validateAuthOption(mixed $auth): void
{
if (!is_array($auth)) {
throw new InvalidArgumentException('Parameter auth must be an array.');
}
if (count($auth) !== 2 || !isset($auth[0], $auth[1])) {
throw new InvalidArgumentException(
'Parameter auth must be an array of two elements (username and password).',
);
}
if (!is_string($auth[0])) {
throw new InvalidArgumentException('Username is not a string.');
}
if (!is_string($auth[1])) {
throw new InvalidArgumentException('Password is not a string.');
}
}
private static function validateBodyOption(mixed $body): void
{
if (!is_string($body)) {
throw new InvalidArgumentException('Body must be a string.');
}
}
private static function validateJsonOption(mixed $json): void
{
try {
Json::encode($json);
} catch (JsonException $e) {
throw new InvalidArgumentException('Option json must be serializable to JSON.', 0, $e);
}
}
private static function validateTimeoutOption(mixed $timeout): void
{
if ($timeout !== null && !is_int($timeout)) {
throw new InvalidArgumentException('Option timeout must be an integer or null.');
}
}
private static function validateConnectTimeoutOption(mixed $connectTimeout): void
{
if ($connectTimeout !== null && !is_int($connectTimeout)) {
throw new InvalidArgumentException('Option connectTimeout must be an integer or null.');
}
}
private static function validateVerify(mixed $verify): void
{
if (!(is_bool($verify) || (is_string($verify) && file_exists($verify)))) {
throw new InvalidArgumentException(
'Option verify must be an bool or a string path to file, ' . $verify . ' given.',
);
}
}
private static function validateCertOption(mixed $value): void
{
if (!is_string($value) && !is_array($value)) {
throw new InvalidArgumentException('Option cert must be a string.');
}
if (is_array($value)) {
if (!isset($value[0]) || !isset($value[1])) {
throw new InvalidArgumentException(
'Option cert must be an array of two elements (cert and key). Provided array: ' . json_encode(
$value,
),
);
}
if (!is_string($value[0]) || !is_string($value[1])) {
throw new InvalidArgumentException(
'Option cert must be an array of two strings (cert and key). Provided array: ' . json_encode(
$value,
),
);
}
$file = $value[0];
} else {
$file = $value;
}
var_dump(file_exists($file));
if (!file_exists($file)) {
throw new InvalidArgumentException('Option cert file not found. Provided file: ' . $file);
}
}
private static function validateSslKeyOption(mixed $value): void
{
if (!is_string($value) && !is_array($value)) {
throw new InvalidArgumentException('Option ssl_key must be a string.');
}
if (is_array($value)) {
if (!isset($value[0]) || !isset($value[1])) {
throw new InvalidArgumentException(
'Option ssl_key must be an array of two elements (key and password). Provided array: ' . json_encode(
$value,
) . '.',
);
}
if (!is_string($value[0]) || !is_string($value[1])) {
throw new InvalidArgumentException(
'Option ssl_key must be an array of two strings (key and password). Provided array: ' . json_encode(
$value,
) . '.',
);
}
$file = $value[0];
} else {
$file = $value;
}
if (!file_exists($file)) {
throw new InvalidArgumentException('Option ssl_key file not found. Provided file: ' . $file);
}
}
}