Skip to content

Commit 812fe88

Browse files
committed
Introduce a draft for a generic reusable HTTP client builder
1 parent 5b1fa94 commit 812fe88

13 files changed

+901
-347
lines changed

README.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,55 @@ $middlewares = [
8080

8181
**Remember**: [There are a shitload of HTTPlug middleware available already.](http://docs.php-http.org/en/latest/plugins/) Try on of them before writing your own one!
8282

83+
84+
#### Enhance your setup by using a Client Builder
85+
86+
Configuring an HTTP client can be cumbersome if you have a lot of plugins.
87+
In order to make this easier, we've provided a Client Builder that helps you set-up your client step by step:
88+
89+
```php
90+
use Http\Message\Authentication\BasicAuth;
91+
use Phpro\HttpTools\Client\ClientBuilder;
92+
use Phpro\HttpTools\Client\Factory\AutoDiscoveredClientFactory;
93+
94+
$client = ClientBuilder::default(AutoDiscoveredClientFactory::create([]))
95+
->addBaseUri('https://www.google.com')
96+
->addHeaders([
97+
'x-Foo' => 'bar',
98+
])
99+
->addAuthentication(new BasicAuth('user', 'pass'))
100+
->addPlugin(new YourPlugin(), priority: 99)
101+
// -> and many more ...
102+
->build();
103+
```
104+
105+
Our suggested approach is to create a configuration object for all variable client configurations and services.
106+
This can be used in combination with a PHP factory class that builds your client through this builder:
107+
108+
```php
109+
final readonly class YourClientConfig {
110+
public function __construct(
111+
public string $apiUrl,
112+
#[\SensitiveParameter] public string $apiKey,
113+
public LoggerInterface $logger
114+
) {
115+
}
116+
}
117+
```
118+
119+
```php
120+
final readonly class YourClientFactory
121+
{
122+
public static function create(YourClientConfig $config): ClientInterface
123+
{
124+
return ClientBuilder::default()
125+
->addBaseUri($config->apiUrl)
126+
->addHeaders(['x-Api-Key' => $config->apiKey])
127+
->build();
128+
}
129+
}
130+
```
131+
83132
### Logging
84133

85134
This package contains the `php-http/logger-plugin`.
@@ -104,6 +153,37 @@ $middlewares[] = new Http\Client\Common\Plugin\LoggerPlugin(
104153
);
105154
```
106155

156+
#### Log formatter builder
157+
158+
Since logging is a common requirement, we've added a formatter builder which can be used directly from the client builder:
159+
160+
```php
161+
use Http\Message\Formatter;
162+
use Phpro\HttpTools\Client\ClientBuilder;
163+
use Phpro\HttpTools\Formatter\FormatterBuilder;
164+
use Phpro\HttpTools\Formatter\RemoveSensitiveHeadersFormatter;
165+
use Phpro\HttpTools\Request\Request;
166+
use Phpro\HttpTools\Transport\Presets\RawPreset;
167+
use Phpro\HttpTools\Uri\RawUriBuilder;
168+
use Symfony\Component\Console\Logger\ConsoleLogger;
169+
use Symfony\Component\Console\Output\ConsoleOutput;
170+
use Symfony\Component\Console\Output\OutputInterface;
171+
172+
$client = ClientBuilder::default()
173+
->addLogger(
174+
new ConsoleLogger(new ConsoleOutput(OutputInterface::VERBOSITY_DEBUG)),
175+
FormatterBuilder::default()
176+
->withDebug(true)
177+
->withMaxBodyLength(1000)
178+
->addDecorator(RemoveSensitiveHeadersFormatter::createDecorator([
179+
'X-SENSITIVE-HEADER',
180+
]))
181+
->build()
182+
)
183+
->build();
184+
```
185+
186+
107187
[More info ...](http://docs.php-http.org/en/latest/plugins/logger.html)
108188

109189
## Using the HTTP-Client

docs/framework/magento2.md

Lines changed: 0 additions & 215 deletions
This file was deleted.

0 commit comments

Comments
 (0)