Skip to content

Commit 574cbec

Browse files
Created a client factory. We cannot use the client directly because the middleware stack of guzzle contains closures that cannot be serialized when passing them between async processes
1 parent 72589f8 commit 574cbec

3 files changed

Lines changed: 69 additions & 3 deletions

File tree

packages/http/src/Check/HttpCheck.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
use de\codenamephp\deploymentchecks\base\Check\WithNameInterface;
2323
use de\codenamephp\deploymentchecks\http\Check\Result\HttpCheckResult;
2424
use de\codenamephp\deploymentchecks\http\Check\Test\TestInterface;
25-
use GuzzleHttp\Client;
25+
use de\codenamephp\deploymentchecks\http\ClientFactory\ClientFactoryInterface;
26+
use de\codenamephp\deploymentchecks\http\ClientFactory\GuzzleClient;
2627
use Psr\Http\Message\RequestInterface;
2728

2829
final class HttpCheck implements CheckInterface, WithNameInterface {
@@ -32,21 +33,23 @@ final class HttpCheck implements CheckInterface, WithNameInterface {
3233
*/
3334
public array $tests = [];
3435

36+
public ClientFactoryInterface $clientFactory;
37+
3538
public function __construct(
3639
public readonly RequestInterface $request,
3740
public readonly string $name,
3841
TestInterface ...$tests
3942
) {
4043
$this->tests = $tests;
44+
$this->clientFactory = new GuzzleClient();
4145
}
4246

4347
public function name() : string {
4448
return $this->name;
4549
}
4650

4751
public function run() : ResultInterface {
48-
$client = new Client();
49-
$response = $client->send($this->request);
52+
$response = $this->clientFactory->create()->send($this->request);
5053
return new HttpCheckResult($this->name(), ...array_map(fn(TestInterface $test) => $test->test($response), $this->tests));
5154
}
5255
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2023 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deploymentchecks\http\ClientFactory;
19+
20+
use Psr\Http\Client\ClientInterface;
21+
22+
interface ClientFactoryInterface {
23+
24+
public function create() : ClientInterface;
25+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* Copyright 2023 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace de\codenamephp\deploymentchecks\http\ClientFactory;
19+
20+
use GuzzleHttp\Client;
21+
use Psr\Http\Client\ClientInterface;
22+
23+
final class GuzzleClient implements ClientFactoryInterface {
24+
25+
public function __construct(public readonly array $config = []) {}
26+
27+
public function create() : ClientInterface {
28+
return new Client($this->config);
29+
}
30+
31+
public function withConfig(array $config) : self {
32+
return new self($config);
33+
}
34+
35+
public function withMergedOptions(array $config) : self {
36+
return new self(array_merge($this->config, $config));
37+
}
38+
}

0 commit comments

Comments
 (0)