-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeaderSettingClient.php
More file actions
59 lines (50 loc) · 2.18 KB
/
Copy pathHeaderSettingClient.php
File metadata and controls
59 lines (50 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
declare(strict_types=1);
namespace TinyBlocks\Http\Client;
use Closure;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* PSR-18 decorator that sets headers on every outbound request, resolving each value at send time.
*
* <p>Each header value is a deferred resolution invoked per request, so values that change between
* requests (a correlation identifier, a rotating token) are always current. A resolved value
* replaces any header of the same name already present on the outbound request. A header whose
* value resolves to an empty string is omitted, and the request keeps whatever it already
* carried under that name.</p>
*/
final readonly class HeaderSettingClient implements ClientInterface
{
/**
* @param ClientInterface $client The underlying client to delegate sends to.
* @param array<string, Closure(): string> $headerValues The header names, each mapped to the
* resolution producing its value.
*/
private function __construct(private ClientInterface $client, private array $headerValues)
{
}
/**
* Creates a HeaderSettingClient from a PSR-18 client and the headers to set on each request.
*
* @param ClientInterface $client The underlying client to delegate sends to.
* @param array<string, Closure(): string> $headerValues The header names, each mapped to the
* resolution producing its value.
* @return HeaderSettingClient The created instance.
*/
public static function with(ClientInterface $client, array $headerValues): HeaderSettingClient
{
return new HeaderSettingClient(client: $client, headerValues: $headerValues);
}
public function sendRequest(RequestInterface $request): ResponseInterface
{
foreach ($this->headerValues as $name => $resolveValue) {
$value = $resolveValue();
if ($value === '') {
continue;
}
$request = $request->withHeader($name, $value);
}
return $this->client->sendRequest($request);
}
}