Skip to content

Commit 40506d8

Browse files
authored
Pool and Batch newRequest() (#58038)
1 parent ed061e2 commit 40506d8

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/Illuminate/Http/Client/Batch.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ public function as(string $key)
150150
return $this->requests[$key] = $this->asyncRequest();
151151
}
152152

153+
/**
154+
* Add a request to the batch with a numeric index.
155+
*
156+
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
157+
*
158+
* @throws \Illuminate\Http\Client\BatchInProgressException
159+
*/
160+
public function newRequest()
161+
{
162+
if ($this->inProgress) {
163+
throw new BatchInProgressException();
164+
}
165+
166+
$this->incrementPendingRequests();
167+
168+
return $this->requests[] = $this->asyncRequest();
169+
}
170+
153171
/**
154172
* Register a callback to run before the first request from the batch runs.
155173
*
@@ -423,15 +441,11 @@ public function getRequests(): array
423441
* @param string $method
424442
* @param array $parameters
425443
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
444+
*
445+
* @throws \Illuminate\Http\Client\BatchInProgressException
426446
*/
427447
public function __call(string $method, array $parameters)
428448
{
429-
if ($this->inProgress) {
430-
throw new BatchInProgressException();
431-
}
432-
433-
$this->incrementPendingRequests();
434-
435-
return $this->requests[] = $this->asyncRequest()->$method(...$parameters);
449+
return $this->newRequest()->{$method}(...$parameters);
436450
}
437451
}

src/Illuminate/Http/Client/Pool.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ public function __construct(?Factory $factory = null)
4141
$this->handler = Utils::chooseHandler();
4242
}
4343

44+
/**
45+
* Add a request to the pool with a numeric index.
46+
*
47+
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
48+
*/
49+
public function newRequest()
50+
{
51+
return $this->pool[] = $this->asyncRequest();
52+
}
53+
4454
/**
4555
* Add a request to the pool with a key.
4656
*
@@ -73,14 +83,14 @@ public function getRequests()
7383
}
7484

7585
/**
76-
* Add a request to the pool with a numeric index.
86+
* Add a request to the pool with a numeric index and forward the method call to the request.
7787
*
7888
* @param string $method
7989
* @param array $parameters
8090
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
8191
*/
8292
public function __call($method, $parameters)
8393
{
84-
return $this->pool[] = $this->asyncRequest()->$method(...$parameters);
94+
return $this->newRequest()->{$method}(...$parameters);
8595
}
8696
}

tests/Http/HttpClientTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,10 +1906,12 @@ public function testMultipleRequestsAreSentInThePoolWithKeys()
19061906
$pool->as('test200')->get('200.com'),
19071907
$pool->as('test400')->get('400.com'),
19081908
$pool->as('test500')->get('500.com'),
1909+
$pool->newRequest()->get('200.com'),
19091910
];
19101911
});
19111912

19121913
$this->assertSame(200, $responses['test200']->status());
1914+
$this->assertSame(200, $responses[0]->status());
19131915
$this->assertSame(400, $responses['test400']->status());
19141916
$this->assertSame(500, $responses['test500']->status());
19151917
}

0 commit comments

Comments
 (0)