-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ISSUE-31: Add Middlewares to HandlerStack (#37)
* ISSUE-31: Add Middlewares to HandlerStack * ClientBuilder moved inside Guzzle namespace * FIX --------- Co-authored-by: Fabrizio Gargiulo <[email protected]>
- Loading branch information
1 parent
2ecde9b
commit 2a6cda0
Showing
12 changed files
with
151 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoppioGancio\MockedClient\Guzzle; | ||
|
||
use DoppioGancio\MockedClient\HandlerBuilder; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\HandlerStack; | ||
|
||
class ClientBuilder | ||
{ | ||
/** @param array<callable> $middlewares */ | ||
public function __construct( | ||
private readonly HandlerBuilder $handlerBuilder, | ||
private array $middlewares = [], | ||
) { | ||
} | ||
|
||
public function addMiddleware(callable $middleware): self | ||
{ | ||
$this->middlewares[] = $middleware; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<string,mixed> $options */ | ||
public function build(array $options = []): Client | ||
{ | ||
$handler = $this->handlerBuilder->build(); | ||
|
||
$handlerStack = HandlerStack::create($handler); | ||
|
||
foreach ($this->middlewares as $middleware) { | ||
$handlerStack->push($middleware); | ||
} | ||
|
||
$options['handler'] = $handlerStack; | ||
|
||
return new Client($options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DoppioGancio\MockedClient\Guzzle\Middleware; | ||
|
||
use GuzzleHttp\Promise\PromiseInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class Middleware | ||
{ | ||
protected RequestInterface $request; | ||
|
||
public function __invoke(callable $handler): callable | ||
{ | ||
return function (RequestInterface $request, array $options) use ($handler) { | ||
$this->request = $this->mapRequest($request); | ||
|
||
$response = $handler($this->request, $options); | ||
|
||
return $this->mapResponse($response); | ||
}; | ||
} | ||
|
||
protected function mapRequest(RequestInterface $request): RequestInterface | ||
{ | ||
return $request; | ||
} | ||
|
||
protected function mapResponse(PromiseInterface $response): PromiseInterface | ||
{ | ||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"manufacturers": [ | ||
{ | ||
"value": "a-1111", | ||
"label": "Manufacturer #1, Inc.", | ||
"count": 12, | ||
"categoryCount": 4, | ||
"partsWithOffersCount": 1111 | ||
}, | ||
{ | ||
"value": "b-222", | ||
"label": "Manufacturer #2, Inc.", | ||
"count": 22, | ||
"categoryCount": 5, | ||
"partsWithOffersCount": 2222 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[ | ||
{ | ||
"id": "+39", | ||
"code": "IT", | ||
"name": "Italy" | ||
}, | ||
{ | ||
"id": "+49", | ||
"code": "DE", | ||
"name": "Germany" | ||
} | ||
] |