Skip to content

Commit 7d97621

Browse files
committed
share handler instead of client to ensure responsereceived event is sent
1 parent f8b9392 commit 7d97621

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -790,20 +790,32 @@ protected function populateResponse(Response $response)
790790
*/
791791
public function buildClient()
792792
{
793-
return $this->client = $this->client ?: new Client([
794-
'handler' => $this->buildHandlerStack(),
793+
return $this->client = $this->client ?: $this->createClient($this->buildHandlerStack());
794+
}
795+
796+
/**
797+
* Create new Guzzle client.
798+
*
799+
* @param \GuzzleHttp\HandlerStack $handlerStack
800+
* @return \GuzzleHttp\Client
801+
*/
802+
public function createClient($handlerStack)
803+
{
804+
return new Client([
805+
'handler' => $handlerStack,
795806
'cookies' => true,
796807
]);
797808
}
798809

799810
/**
800-
* Build the before sending handler stack.
811+
* add handlers to the handler stack
801812
*
813+
* @param \GuzzleHttp\HandlerStack $handlerStack
802814
* @return \GuzzleHttp\HandlerStack
803815
*/
804-
public function buildHandlerStack()
816+
public function pushHandlers($handlerStack)
805817
{
806-
return tap(HandlerStack::create(), function ($stack) {
818+
return tap($handlerStack, function ($stack) {
807819
$stack->push($this->buildBeforeSendingHandler());
808820
$stack->push($this->buildRecorderHandler());
809821
$stack->push($this->buildStubHandler());
@@ -814,6 +826,16 @@ public function buildHandlerStack()
814826
});
815827
}
816828

829+
/**
830+
* Build the before sending handler stack.
831+
*
832+
* @return \GuzzleHttp\HandlerStack
833+
*/
834+
public function buildHandlerStack()
835+
{
836+
return $this->pushHandlers(HandlerStack::create());
837+
}
838+
817839
/**
818840
* Build the before sending handler.
819841
*
@@ -1022,4 +1044,17 @@ public function setClient(Client $client)
10221044

10231045
return $this;
10241046
}
1047+
1048+
/**
1049+
* set the handler function
1050+
*
1051+
* @param callable $handler
1052+
* @return $this
1053+
*/
1054+
public function setHandler($handler)
1055+
{
1056+
$this->client = $this->createClient($this->pushHandlers(HandlerStack::create($handler)));
1057+
1058+
return $this;
1059+
}
10251060
}

src/Illuminate/Http/Client/Pool.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Illuminate\Http\Client;
44

5+
use GuzzleHttp\Utils;
6+
57
/**
68
* @mixin \Illuminate\Http\Client\Factory
79
*/
@@ -15,11 +17,11 @@ class Pool
1517
protected $factory;
1618

1719
/**
18-
* The client instance.
20+
* The handler function for Guzzle client
1921
*
20-
* @var \GuzzleHttp\Client
22+
* @var callable
2123
*/
22-
protected $client;
24+
protected $handler;
2325

2426
/**
2527
* The pool of requests.
@@ -38,7 +40,7 @@ public function __construct(Factory $factory = null)
3840
{
3941
$this->factory = $factory ?: new Factory();
4042

41-
$this->client = $this->factory->buildClient();
43+
$this->handler = Utils::chooseHandler();
4244
}
4345

4446
/**
@@ -59,7 +61,7 @@ public function as(string $key)
5961
*/
6062
protected function asyncRequest()
6163
{
62-
return $this->factory->setClient($this->client)->async();
64+
return $this->factory->setHandler($this->handler)->async();
6365
}
6466

6567
/**

0 commit comments

Comments
 (0)