Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions src/Illuminate/Http/Client/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,24 @@ public function as(string $key)
return $this->requests[$key] = $this->asyncRequest();
}

/**
* Add a request to the batch with a numeric index.
*
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
*
* @throws \Illuminate\Http\Client\BatchInProgressException
*/
public function newRequest()
{
if ($this->inProgress) {
throw new BatchInProgressException();
}

$this->incrementPendingRequests();

return $this->requests[] = $this->asyncRequest();
}

/**
* Register a callback to run before the first request from the batch runs.
*
Expand Down Expand Up @@ -423,15 +441,11 @@ public function getRequests(): array
* @param string $method
* @param array $parameters
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
*
* @throws \Illuminate\Http\Client\BatchInProgressException
*/
public function __call(string $method, array $parameters)
{
if ($this->inProgress) {
throw new BatchInProgressException();
}

$this->incrementPendingRequests();

return $this->requests[] = $this->asyncRequest()->$method(...$parameters);
return $this->newRequest()->{$method}(...$parameters);
}
}
14 changes: 12 additions & 2 deletions src/Illuminate/Http/Client/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ public function __construct(?Factory $factory = null)
$this->handler = Utils::chooseHandler();
}

/**
* Add a request to the pool with a numeric index.
*
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
*/
public function newRequest()
{
return $this->pool[] = $this->asyncRequest();
}

/**
* Add a request to the pool with a key.
*
Expand Down Expand Up @@ -73,14 +83,14 @@ public function getRequests()
}

/**
* Add a request to the pool with a numeric index.
* Add a request to the pool with a numeric index and forward the method call to the request.
*
* @param string $method
* @param array $parameters
* @return \Illuminate\Http\Client\PendingRequest|\GuzzleHttp\Promise\Promise
*/
public function __call($method, $parameters)
{
return $this->pool[] = $this->asyncRequest()->$method(...$parameters);
return $this->newRequest()->{$method}(...$parameters);
}
}
2 changes: 2 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1906,10 +1906,12 @@ public function testMultipleRequestsAreSentInThePoolWithKeys()
$pool->as('test200')->get('200.com'),
$pool->as('test400')->get('400.com'),
$pool->as('test500')->get('500.com'),
$pool->newRequest()->get('200.com'),
];
});

$this->assertSame(200, $responses['test200']->status());
$this->assertSame(200, $responses[0]->status());
$this->assertSame(400, $responses['test400']->status());
$this->assertSame(500, $responses['test500']->status());
}
Expand Down