Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Apr 12, 2021
1 parent ce69e55 commit 245a712
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
66 changes: 33 additions & 33 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PendingRequest
protected $factory;

/**
* The client instance.
* The Guzzle client instance.
*
* @var \GuzzleHttp\Client
*/
Expand Down Expand Up @@ -595,6 +595,27 @@ public function delete($url, $data = [])
]);
}

/**
* Send a pool of asynchronous requests concurrently.
*
* @param callable $callback
* @return array
*/
public function pool(callable $callback)
{
$results = [];

$requests = tap(new Pool($this->factory), $callback)->getRequests();

foreach ($requests as $key => $item) {
$results[$key] = $item instanceof static ? $item->getPromise()->wait() : $item->wait();
}

ksort($results);

return $results;
}

/**
* Send the request to the given URL.
*
Expand Down Expand Up @@ -742,19 +763,6 @@ protected function populateResponse(Response $response)
return $response;
}

/**
* Set the client instance.
*
* @param \GuzzleHttp\Client $client
* @return $this
*/
public function setClient(Client $client)
{
$this->client = $client;

return $this;
}

/**
* Build the Guzzle client.
*
Expand Down Expand Up @@ -932,33 +940,25 @@ public function async(bool $async = true)
}

/**
* Send a pool of asynchronous requests concurrently.
* Retrieve the pending request promise.
*
* @param callable $callback
* @return array
* @return \GuzzleHttp\Promise\PromiseInterface|null
*/
public function pool(callable $callback)
public function getPromise()
{
$results = [];

$requests = tap(new Pool($this->factory), $callback)->getRequests();

foreach ($requests as $key => $item) {
$results[$key] = $item instanceof static ? $item->getPromise()->wait() : $item->wait();
}

ksort($results);

return $results;
return $this->promise;
}

/**
* Retrieve the pending request promise.
* Set the client instance.
*
* @return \GuzzleHttp\Promise\PromiseInterface|null
* @param \GuzzleHttp\Client $client
* @return $this
*/
public function getPromise()
public function setClient(Client $client)
{
return $this->promise;
$this->client = $client;

return $this;
}
}
3 changes: 1 addition & 2 deletions src/Illuminate/Http/Client/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(Factory $factory = null)
* @param string $key
* @return \Illuminate\Http\Client\PendingRequest
*/
public function add(string $key)
public function as(string $key)
{
return $this->pool[$key] = $this->asyncRequest();
}
Expand All @@ -56,7 +56,6 @@ public function add(string $key)
*/
protected function asyncRequest()
{
// the same client instance needs to be shared across all async requests
return $this->factory->setClient($this->client)->async();
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -892,9 +892,9 @@ public function testMultipleRequestsAreSentInThePoolWithKeys()

$responses = $this->factory->pool(function (Pool $pool) {
return [
$pool->add('test200')->get('200.com'),
$pool->add('test400')->get('400.com'),
$pool->add('test500')->get('500.com'),
$pool->as('test200')->get('200.com'),
$pool->as('test400')->get('400.com'),
$pool->as('test500')->get('500.com'),
];
});

Expand Down

0 comments on commit 245a712

Please sign in to comment.