Skip to content

Commit f3cdaf3

Browse files
Add docs for Http::batch (#10834)
* Add docs for Http::batch * Code Review changes * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 53dfa18 commit f3cdaf3

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

http-client.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
- [Guzzle Middleware](#guzzle-middleware)
1212
- [Guzzle Options](#guzzle-options)
1313
- [Concurrent Requests](#concurrent-requests)
14+
- [Request Pooling](#request-pooling)
15+
- [Request Batching](#request-batching)
1416
- [Macros](#macros)
1517
- [Testing](#testing)
1618
- [Faking Responses](#faking-responses)
@@ -500,6 +502,9 @@ public function boot(): void
500502

501503
Sometimes, you may wish to make multiple HTTP requests concurrently. In other words, you want several requests to be dispatched at the same time instead of issuing the requests sequentially. This can lead to substantial performance improvements when interacting with slow HTTP APIs.
502504

505+
<a name="request-pooling"></a>
506+
### Request Pooling
507+
503508
Thankfully, you may accomplish this using the `pool` method. The `pool` method accepts a closure which receives an `Illuminate\Http\Client\Pool` instance, allowing you to easily add requests to the request pool for dispatching:
504509

505510
```php
@@ -552,6 +557,71 @@ $responses = Http::pool(fn (Pool $pool) => [
552557
]);
553558
```
554559

560+
<a name="request-batching"></a>
561+
### Request Batching
562+
563+
Another way of working with concurrent requests in Laravel is to use the `batch` method. Like the `pool` method, it accepts a closure which receives an `Illuminate\Http\Client\Batch` instance, allowing you to easily add requests to the request pool for dispatching, but it also allows you to define completion callbacks:
564+
565+
```php
566+
use Illuminate\Http\Client\Batch;
567+
use Illuminate\Http\Client\RequestException;
568+
use Illuminate\Http\Client\Response;
569+
use Illuminate\Support\Facades\Http;
570+
571+
$responses = Http::batch(fn (Batch $batch) => [
572+
$batch->get('http://localhost/first'),
573+
$batch->get('http://localhost/second'),
574+
$batch->get('http://localhost/third'),
575+
])->before(function (Batch $batch) {
576+
// The batch has been created but no requests have been initialized...
577+
})->progress(function (Batch $batch, int|string $key, Response $response) {
578+
// An individual request has completed successfully...
579+
})->then(function (Batch $batch, array $results) {
580+
// All requests completed successfully...
581+
})->catch(function (Batch $batch, int|string $key, Response|RequestException $response) {
582+
// First batch request failure detected...
583+
})->finally(function (Batch $batch, array $results) {
584+
// The batch has finished executing...
585+
})->send();
586+
```
587+
588+
Like the `pool` method, you can use the `as` method to name your requests:
589+
590+
```php
591+
$responses = Http::batch(fn (Batch $batch) => [
592+
$batch->as('first')->get('http://localhost/first'),
593+
$batch->as('second')->get('http://localhost/second'),
594+
$batch->as('third')->get('http://localhost/third'),
595+
])->send();
596+
```
597+
598+
After a `batch` is started by calling the `send` method, you can't add new requests to it. Trying to do so will result in a `Illuminate\Http\Client\BatchInProgressException` exception being thrown.
599+
600+
<a name="inspecting-batches"></a>
601+
#### Inspecting Batches
602+
603+
The `Illuminate\Http\Client\Batch` instance that is provided to batch completion callbacks has a variety of properties and methods to assist you in interacting with and inspecting a given batch of requests:
604+
605+
```php
606+
// The number of requests assigned to the batch...
607+
$batch->totalRequests;
608+
609+
// The number of requests that have not been processed yet...
610+
$batch->pendingRequests;
611+
612+
// The number of requests that have failed...
613+
$batch->failedRequests;
614+
615+
// The number of requests that have been processed thus far...
616+
$batch->processedRequests();
617+
618+
// Indicates if the batch has finished executing...
619+
$batch->finished();
620+
621+
// Indicates if the batch has request failures...
622+
$batch->hasFailures();
623+
```
624+
555625
<a name="macros"></a>
556626
## Macros
557627

0 commit comments

Comments
 (0)