-
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathPerformsHttpRequests.php
More file actions
512 lines (445 loc) · 18.6 KB
/
PerformsHttpRequests.php
File metadata and controls
512 lines (445 loc) · 18.6 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php
declare(strict_types=1);
namespace Fetch\Concerns;
use Fetch\Enum\ContentType;
use Fetch\Enum\Method;
use Fetch\Events\RequestEvent;
use Fetch\Events\ResponseEvent;
use Fetch\Exceptions\RequestException as FetchRequestException;
use Fetch\Http\Response;
use Fetch\Interfaces\Response as ResponseInterface;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
use GuzzleHttp\Psr7\Request as GuzzleRequest;
use Matrix\Exceptions\AsyncException;
use React\Promise\PromiseInterface;
use function Matrix\Support\async;
trait PerformsHttpRequests
{
/**
* Handles an HTTP request with the given method, URI, and options.
*
* @param string $method The HTTP method to use
* @param string $uri The URI to request
* @param array<string, mixed> $options Additional options for the request
* @return Response|PromiseInterface Response or promise
*/
public static function handle(
string $method,
string $uri,
array $options = [],
): Response|PromiseInterface {
$handler = static::create();
$handler->withOptions($options);
return $handler->sendRequest($method, $uri);
}
/**
* Send a HEAD request.
*
* @param string $uri The URI to request
* @return ResponseInterface|PromiseInterface The response or promise
*/
public function head(string $uri): ResponseInterface|PromiseInterface
{
return $this->sendRequest(Method::HEAD, $uri);
}
/**
* Send a GET request.
*
* @param string $uri The URI to request
* @param array<string, mixed> $queryParams Optional query parameters
* @return ResponseInterface|PromiseInterface The response or promise
*/
public function get(string $uri, array $queryParams = []): ResponseInterface|PromiseInterface
{
$options = [];
if (! empty($queryParams)) {
$options['query'] = $queryParams;
}
return $this->sendRequest(Method::GET, $uri, $options);
}
/**
* Send a POST request.
*
* @param string $uri The URI to request
* @param mixed $body The request body
* @param ContentType|string $contentType The content type of the request
* @return ResponseInterface|PromiseInterface The response or promise
*/
public function post(
string $uri,
mixed $body = null,
ContentType|string $contentType = ContentType::JSON,
): ResponseInterface|PromiseInterface {
return $this->sendRequestWithBody(Method::POST, $uri, $body, $contentType);
}
/**
* Send a PUT request.
*
* @param string $uri The URI to request
* @param mixed $body The request body
* @param ContentType|string $contentType The content type of the request
* @return ResponseInterface|PromiseInterface The response or promise
*/
public function put(
string $uri,
mixed $body = null,
ContentType|string $contentType = ContentType::JSON,
): ResponseInterface|PromiseInterface {
return $this->sendRequestWithBody(Method::PUT, $uri, $body, $contentType);
}
/**
* Send a PATCH request.
*
* @param string $uri The URI to request
* @param mixed $body The request body
* @param ContentType|string $contentType The content type of the request
* @return ResponseInterface|PromiseInterface The response or promise
*/
public function patch(
string $uri,
mixed $body = null,
ContentType|string $contentType = ContentType::JSON,
): ResponseInterface|PromiseInterface {
return $this->sendRequestWithBody(Method::PATCH, $uri, $body, $contentType);
}
/**
* Send a DELETE request.
*
* @param string $uri The URI to request
* @param mixed $body Optional request body
* @param ContentType|string $contentType The content type of the request
* @return ResponseInterface|PromiseInterface The response or promise
*/
public function delete(
string $uri,
mixed $body = null,
ContentType|string $contentType = ContentType::JSON,
): ResponseInterface|PromiseInterface {
return $this->sendRequestWithBody(Method::DELETE, $uri, $body, $contentType);
}
/**
* Send an OPTIONS request.
*
* @param string $uri The URI to request
* @return ResponseInterface|PromiseInterface The response or promise
*/
public function options(string $uri): ResponseInterface|PromiseInterface
{
return $this->sendRequest(Method::OPTIONS, $uri);
}
/**
* Send an HTTP request.
*
* @param Method|string $method The HTTP method
* @param string $uri The URI to request
* @param array<string, mixed> $options Additional options
* @return ResponseInterface|PromiseInterface The response or promise
*/
public function sendRequest(
Method|string $method,
string $uri,
array $options = [],
): ResponseInterface|PromiseInterface {
// Create a new handler with the combined options
$handler = clone $this;
$handler->withOptions($options);
// Normalize method to string
$methodStr = $method instanceof Method ? $method->value : strtoupper($method);
// Store URI in handler options
$handler->options['uri'] = $uri;
$handler->options['method'] = $methodStr;
// Build the full URI
$fullUri = $handler->buildFullUri($uri);
// Prepare Guzzle options
$guzzleOptions = $handler->prepareGuzzleOptions();
// Check for mock response first (if HandlesMocking trait is available)
if (method_exists($handler, 'handleMockRequest')) {
$mockResponse = $handler->handleMockRequest($methodStr, $fullUri, $guzzleOptions);
if ($mockResponse !== null) {
return $mockResponse;
}
}
// Start timing for logging
$startTime = microtime(true);
// Generate a correlation ID for event tracking
$correlationId = method_exists($handler, 'generateCorrelationId')
? $handler->generateCorrelationId()
: bin2hex(random_bytes(16));
// Log the request if method exists
if (method_exists($handler, 'logRequest')) {
$handler->logRequest($methodStr, $fullUri, $guzzleOptions);
}
// Dispatch request event
if (method_exists($handler, 'dispatchEvent')) {
$psrRequest = new GuzzleRequest($methodStr, $fullUri, $guzzleOptions['headers'] ?? []);
$handler->dispatchEvent(new RequestEvent(
$psrRequest,
$correlationId,
$startTime,
[],
$guzzleOptions
));
}
// Send the request (async or sync)
if ($handler->isAsync) {
return $handler->executeAsyncRequest($methodStr, $fullUri, $guzzleOptions, $correlationId);
} else {
return $handler->executeSyncRequest($methodStr, $fullUri, $guzzleOptions, $startTime, $correlationId);
}
}
/**
* Sends an HTTP request with the specified parameters.
*
* @param string|Method $method HTTP method (e.g., GET, POST)
* @param string $uri URI to send the request to
* @param mixed $body Request body
* @param string|ContentType $contentType Content type of the request
* @param array<string, mixed> $options Additional request options
* @return Response|PromiseInterface Response or promise
*/
public function request(
string|Method $method,
string $uri,
mixed $body = null,
string|ContentType $contentType = ContentType::JSON,
array $options = [],
): Response|PromiseInterface {
// Normalize method to string
$methodStr = $method instanceof Method ? $method->value : strtoupper($method);
// Apply any additional options
if (! empty($options)) {
$this->withOptions($options);
}
// Configure request body if provided
if ($body !== null) {
$this->configureRequestBody($body, $contentType);
}
// Send the request using our unified method
return $this->sendRequest($methodStr, $uri);
}
/**
* Get the effective timeout for the request.
*
* @return int The timeout in seconds
*/
public function getEffectiveTimeout(): int
{
// Next check options array
if (isset($this->options['timeout']) && is_int($this->options['timeout'])) {
return $this->options['timeout'];
}
// First check explicitly set timeout property
if (isset($this->timeout) && is_int($this->timeout)) {
return $this->timeout;
}
// Fall back to default
return self::DEFAULT_TIMEOUT;
}
/**
* Send an HTTP request with a body.
*
* @param Method|string $method The HTTP method
* @param string $uri The URI to request
* @param mixed $body The request body
* @param ContentType|string $contentType The content type
* @param array<string, mixed> $options Additional options
* @return ResponseInterface|PromiseInterface The response or promise
*/
protected function sendRequestWithBody(
Method|string $method,
string $uri,
mixed $body = null,
ContentType|string $contentType = ContentType::JSON,
array $options = [],
): ResponseInterface|PromiseInterface {
// Skip if no body
if ($body === null) {
return $this->sendRequest($method, $uri, $options);
}
// Create a new handler instance with cloned options
$handler = clone $this;
// Merge options if provided
if (! empty($options)) {
$handler->withOptions($options);
}
// Configure the request body on the cloned handler
$handler->configureRequestBody($body, $contentType);
// Send the request using the configured handler
return $handler->sendRequest($method, $uri);
}
/**
* Prepare options for Guzzle.
*
* @return array<string, mixed> Options ready for Guzzle
*/
protected function prepareGuzzleOptions(): array
{
$guzzleOptions = [];
// Standard Guzzle options to include
$standardOptions = [
'headers', 'json', 'form_params', 'multipart', 'body',
'query', 'auth', 'verify', 'proxy', 'cookies', 'allow_redirects',
'cert', 'ssl_key', 'stream', 'connect_timeout', 'read_timeout',
'debug', 'sink', 'version', 'decode_content',
];
// Copy standard options if set
foreach ($standardOptions as $option) {
if (isset($this->options[$option])) {
$guzzleOptions[$option] = $this->options[$option];
}
}
// Set timeout consistently
if (isset($this->timeout)) {
$guzzleOptions['timeout'] = $this->timeout;
} elseif (isset($this->options['timeout'])) {
$guzzleOptions['timeout'] = $this->options['timeout'];
} else {
$guzzleOptions['timeout'] = $this->getEffectiveTimeout();
}
// Ensure connect_timeout defaults sensibly if not provided
if (! isset($guzzleOptions['connect_timeout'])) {
$guzzleOptions['connect_timeout'] = $guzzleOptions['connect_timeout']
?? ($this->options['connect_timeout'] ?? $guzzleOptions['timeout']);
}
return $guzzleOptions;
}
/**
* Execute a synchronous HTTP request.
*
* @param string $method The HTTP method
* @param string $uri The full URI
* @param array<string, mixed> $options The Guzzle options
* @param float $startTime The request start time
* @param string|null $correlationId The correlation ID for event tracking
* @return ResponseInterface The response
*/
protected function executeSyncRequest(
string $method,
string $uri,
array $options,
float $startTime,
?string $correlationId = null,
): ResponseInterface {
// Generate correlation ID if not provided
$correlationId = $correlationId ?? bin2hex(random_bytes(16));
// Start profiling if profiler is available
$requestId = null;
if (method_exists($this, 'startProfiling')) {
$requestId = $this->startProfiling($method, $uri);
}
// Track memory for debugging
$startMemory = memory_get_usage(true);
// Create the PSR request for events
$psrRequest = new GuzzleRequest($method, $uri, $options['headers'] ?? []);
return $this->retryRequestWithEvents(
function () use ($method, $uri, $options, $startTime, $requestId, $startMemory, $psrRequest, $correlationId): ResponseInterface {
try {
// Record request sent event for profiling
if ($requestId !== null && method_exists($this, 'recordProfilingEvent')) {
$this->recordProfilingEvent($requestId, 'request_sent');
}
// Send the request to Guzzle
$psrResponse = $this->getHttpClient()->request($method, $uri, $options);
// Record response received event for profiling
if ($requestId !== null && method_exists($this, 'recordProfilingEvent')) {
$this->recordProfilingEvent($requestId, 'response_start');
}
// Calculate duration
$duration = microtime(true) - $startTime;
// Create our response object
$response = Response::createFromBase($psrResponse);
// End profiling
if ($requestId !== null && method_exists($this, 'endProfiling')) {
$this->endProfiling($requestId, $response->getStatusCode());
}
// Create debug info if debug mode is enabled
if (method_exists($this, 'isDebugEnabled') && $this->isDebugEnabled()) {
$memoryUsage = memory_get_usage(true) - $startMemory;
$timings = [
'total_time' => round($duration * 1000, 3),
'start_time' => $startTime,
'end_time' => microtime(true),
];
if (method_exists($this, 'createDebugInfo')) {
$this->createDebugInfo($method, $uri, $options, $response, $timings, $memoryUsage);
}
}
// Trigger retry on configured retryable status codes
if (in_array($response->getStatusCode(), $this->getRetryableStatusCodes(), true)) {
throw new FetchRequestException('Retryable status: '.$response->getStatusCode(), $psrRequest, $psrResponse);
}
// Log response if method exists
if (method_exists($this, 'logResponse')) {
$this->logResponse($response, $duration);
}
// Dispatch response event
if (method_exists($this, 'dispatchEvent')) {
$this->dispatchEvent(new ResponseEvent(
$psrRequest,
$response,
$correlationId,
microtime(true),
$duration
));
}
return $response;
} catch (GuzzleException $e) {
// End profiling with error
if ($requestId !== null && method_exists($this, 'endProfiling')) {
$this->endProfiling($requestId, null);
}
// Normalize to Fetch RequestException to participate in retry logic
if ($e instanceof GuzzleRequestException) {
$req = $e->getRequest();
$res = $e->getResponse();
throw new FetchRequestException(sprintf('Request %s %s failed: %s', $method, $uri, $e->getMessage()), $req, $res, $e);
}
throw new FetchRequestException(sprintf('Request %s %s failed: %s', $method, $uri, $e->getMessage()), $psrRequest, null, $e);
}
},
$psrRequest,
$correlationId
);
}
/**
* Execute an asynchronous HTTP request.
*
* @param string $method The HTTP method
* @param string $uri The full URI
* @param array<string, mixed> $options The Guzzle options
* @param string|null $correlationId The correlation ID for event tracking
* @return PromiseInterface A promise that resolves with the response
*/
protected function executeAsyncRequest(
string $method,
string $uri,
array $options,
?string $correlationId = null,
): PromiseInterface {
return async(function () use ($method, $uri, $options, $correlationId): ResponseInterface {
$startTime = microtime(true);
$correlationId = $correlationId ?? bin2hex(random_bytes(16));
// Since this is in an async context, we can use try-catch for proper promise rejection
try {
// Execute the synchronous request inside the async function
$response = $this->executeSyncRequest($method, $uri, $options, $startTime, $correlationId);
return $response;
} catch (\Throwable $e) {
// Log the error without interfering with promise rejection
if (isset($this->logger)) {
$this->logger->error('Async request failed', [
'method' => $method,
'uri' => $uri,
'error' => $e->getMessage(),
'exception_class' => get_class($e),
]);
}
// Use withErrorContext to add request information to the error
$contextMessage = "Request $method $uri failed";
// Throw the exception - in the async context, this will properly reject the promise
throw new AsyncException($contextMessage.': '.$e->getMessage(), $e->getCode(), $e /* Preserve the original exception as previous */);
}
});
}
}