Skip to content

Commit 7661547

Browse files
committed
clients default request headers
1 parent 1c52616 commit 7661547

File tree

9 files changed

+100
-31
lines changed

9 files changed

+100
-31
lines changed

.github/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ Laravel config load automatically configuration from `services.php` config, with
3333
```php
3434
'some_api_name' => [ // this key is value of first parameter ApiClient::request method
3535
'base_url' => 'https://some-host/some-base-url',
36-
'token' => 'tokenexample', // optional default null, authentication token for https://swagger.io/docs/specification/authentication/bearer-authentication/
36+
'default_headers' => [ // optional default [], http headers send in every request
37+
'Authorization' => 'Bearer tokenexample', // https://swagger.io/docs/specification/authentication/bearer-authentication/
38+
],
3739
'verify' => true, // optional default true, turn on/off certificates verification
3840
'deprecated_header' => 'Deprecated', // optional default 'Deprecated', define name of deprecated response header logged into deprecation log
3941
],

config/laravel/webhook.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
return [
66
/* configuration for webhook dispatcher */
77
'dispatch' => [
8-
// authentication token for https://swagger.io/docs/specification/authentication/bearer-authentication/
9-
// if not null token is in every webhook call for authentication of application that serving event
10-
'token' => null,
8+
// http request headers send in every webhook call
9+
'default_headers' => [],
1110
// connection name where are asynchronous webhook calls dispatched, if null default laravel queue connection is used
1211
'queue-connection' => null,
1312
// if connection support named queue, specific name for webhooks can be defined here, if null default queue name is used

src/Model/Client/Request.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ public function withBaseUrl(string $baseUrl): self
5454
return $request;
5555
}
5656

57+
/**
58+
* @param string $name
59+
* @param string|array<string> $value
60+
*/
61+
public function withHeader(string $name, string|array $value): self
62+
{
63+
$headers = $this->headers;
64+
$headers[$name] = $value;
65+
$request = new self($this->method, $this->url, [], $this->body, $headers);
66+
$request->baseUrl = $this->baseUrl;
67+
return $request;
68+
}
69+
5770
/**
5871
* @template TData
5972
* @param TData $jsonData
@@ -79,6 +92,11 @@ public function hasBaseUrl(): bool
7992
return $this->baseUrl !== null;
8093
}
8194

95+
public function hasHeader(string $name): bool
96+
{
97+
return array_key_exists($name, $this->headers);
98+
}
99+
82100
public function createPsr(RequestFactoryInterface $factory): RequestInterface
83101
{
84102
if ($this->baseUrl === null) {

src/Service/Client/ApiClient.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,19 @@ public function requestAsync(string $apiName, Request $request, array $options =
148148
$request = $request->withBaseUrl($this->config->getBaseUrl($apiName));
149149
}
150150

151-
$psrRequest = $request->createPsr($this->requestFactory);
152-
153-
$token = $this->config->getBearerToken($apiName);
154-
if (! $psrRequest->hasHeader('Authorization') && $token !== null) {
155-
$psrRequest = $psrRequest->withHeader('Authorization', 'Bearer '.$token);
151+
$defaultHeaders = $this->config->getDefaultHeaders($apiName);
152+
foreach ($defaultHeaders as $defaultHeader => $value) {
153+
$defaultHeader = (string) $defaultHeader;
154+
if (! $request->hasHeader($defaultHeader)) {
155+
$request = $request->withHeader($defaultHeader, $value);
156+
}
156157
}
157158

159+
$psrRequest = $request->createPsr($this->requestFactory);
160+
158161
$defaultOptions = [
159162
RequestOptions::VERIFY => $this->config->getVerifyCerts($apiName),
160163
];
161-
162164
foreach ($defaultOptions as $key => $defaultOption) {
163165
if (! array_key_exists($key, $options)) {
164166
$options[$key] = $defaultOption;

src/Service/Client/Config.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,27 @@ abstract class Config
1313
abstract public function getBaseUrl(string $apiName): string;
1414

1515
/**
16+
* @deprecated in 0.6 will be removed use $this->getDefaultHeaders
1617
* @param non-empty-string $apiName
1718
* @return non-empty-string|null
1819
*/
1920
abstract public function getBearerToken(string $apiName): ?string;
2021

22+
/**
23+
* @param non-empty-string $apiName
24+
* @return array<string>
25+
*/
26+
public function getDefaultHeaders(string $apiName): array
27+
{
28+
/** @phpstan-ignore-next-line */
29+
$token = $this->getBearerToken($apiName);
30+
if ($token !== null) {
31+
return ['Authorization' => 'Bearer '.$token];
32+
}
33+
34+
return [];
35+
}
36+
2137
/**
2238
* @param non-empty-string $apiName
2339
*/

src/Service/Client/LaravelConfig.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44

55
namespace SimpleAsFuck\ApiToolkit\Service\Client;
66

7-
use Illuminate\Contracts\Config\Repository;
8-
use SimpleAsFuck\Validator\Factory\Validator;
9-
use SimpleAsFuck\Validator\Rule\General\Rules;
7+
use SimpleAsFuck\ApiToolkit\Service\Config\LaravelAdapter;
108

119
final class LaravelConfig extends Config
1210
{
1311
public function __construct(
14-
private Repository $repository
12+
private readonly LaravelAdapter $laravelAdapter
1513
) {
1614
}
1715

@@ -21,40 +19,45 @@ public function __construct(
2119
*/
2220
public function getBaseUrl(string $apiName): string
2321
{
24-
return $this->getValue('services.'.$apiName.'.base_url')->string()->notEmpty()->notNull();
22+
return $this->laravelAdapter->get('services.'.$apiName.'.base_url')->string()->notEmpty()->notNull();
2523
}
2624

2725
/**
26+
* @deprecated in 0.6 will be removed use $this->getDefaultHeaders
2827
* @param non-empty-string $apiName
2928
* @return non-empty-string|null
3029
*/
3130
public function getBearerToken(string $apiName): ?string
3231
{
33-
return $this->getValue('services.'.$apiName.'.token')->string()->notEmpty()->nullable();
32+
return $this->laravelAdapter->get('services.'.$apiName.'.token')->string()->notEmpty()->nullable();
3433
}
3534

3635
/**
3736
* @param non-empty-string $apiName
37+
* @return array<string>
3838
*/
39-
public function getVerifyCerts(string $apiName): bool
39+
public function getDefaultHeaders(string $apiName): array
4040
{
41-
return $this->getValue('services.'.$apiName.'.verify')->bool()->nullable() ?? parent::getVerifyCerts($apiName);
41+
return [
42+
...parent::getDefaultHeaders($apiName),
43+
...$this->laravelAdapter->get('services.'.$apiName.'.default_headers')->array()->ofString()->nullable() ?? [],
44+
];
4245
}
4346

4447
/**
4548
* @param non-empty-string $apiName
46-
* @return non-empty-string
4749
*/
48-
public function getDeprecatedHeader(string $apiName): string
50+
public function getVerifyCerts(string $apiName): bool
4951
{
50-
return $this->getValue('services.'.$apiName.'deprecated_header')->string()->notEmpty()->nullable() ?? parent::getDeprecatedHeader($apiName);
52+
return $this->laravelAdapter->get('services.'.$apiName.'.verify')->bool()->nullable() ?? parent::getVerifyCerts($apiName);
5153
}
5254

5355
/**
54-
* @param non-empty-string $key
56+
* @param non-empty-string $apiName
57+
* @return non-empty-string
5558
*/
56-
private function getValue(string $key): Rules
59+
public function getDeprecatedHeader(string $apiName): string
5760
{
58-
return Validator::make($this->repository->get($key), 'Config key '.$key);
61+
return $this->laravelAdapter->get('services.'.$apiName.'deprecated_header')->string()->notEmpty()->nullable() ?? parent::getDeprecatedHeader($apiName);
5962
}
6063
}

src/Service/Webhook/Client.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
abstract class Client
1414
{
1515
public function __construct(
16-
private Config $config,
17-
private \GuzzleHttp\Client $client,
18-
private ?LoggerInterface $logger,
16+
private readonly Config $config,
17+
private readonly \GuzzleHttp\Client $client,
18+
private readonly ?LoggerInterface $logger,
1919
) {
2020
}
2121

@@ -29,9 +29,12 @@ final public function callWebhooks(iterable $webhooks, int $tries, array $header
2929
$requestHeaders = $headers;
3030
$requestOptions = $options;
3131

32-
$token = $this->config->getBearerToken();
33-
if (! array_key_exists('Authorization', $requestHeaders) && $token !== null) {
34-
$requestHeaders['Authorization'] = 'Bearer '.$token;
32+
$defaultHeaders = $this->config->getDefaultHeaders();
33+
foreach ($defaultHeaders as $defaultHeader => $value) {
34+
$defaultHeader = (string) $defaultHeader;
35+
if (! array_key_exists($defaultHeader, $requestHeaders)) {
36+
$requestHeaders[$defaultHeader] = $value;
37+
}
3538
}
3639

3740
$requestOptions[RequestOptions::HEADERS] = $requestHeaders;

src/Service/Webhook/Config.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,25 @@
77
abstract class Config
88
{
99
/**
10+
* @deprecated in 0.6 will be removed use $this->getDefaultHeaders
1011
* @return non-empty-string|null
1112
*/
1213
abstract public function getBearerToken(): ?string;
1314

15+
/**
16+
* @return array<string>
17+
*/
18+
public function getDefaultHeaders(): array
19+
{
20+
/** @phpstan-ignore-next-line */
21+
$token = $this->getBearerToken();
22+
if ($token !== null) {
23+
return ['Authorization' => 'Bearer '.$token];
24+
}
25+
26+
return [];
27+
}
28+
1429
/**
1530
* @return positive-int
1631
*/

src/Service/Webhook/LaravelConfig.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
final class LaravelConfig extends Config
1010
{
1111
public function __construct(
12-
private LaravelAdapter $laravelAdapter,
12+
private readonly LaravelAdapter $laravelAdapter,
1313
) {
1414
}
1515

@@ -21,6 +21,17 @@ public function getBearerToken(): ?string
2121
return $this->laravelAdapter->get('webhook.dispatch.token')->string()->notEmpty()->nullable();
2222
}
2323

24+
/**
25+
* @return array<string>
26+
*/
27+
public function getDefaultHeaders(): array
28+
{
29+
return [
30+
...parent::getDefaultHeaders(),
31+
...$this->laravelAdapter->get('webhook.dispatch.default_headers')->array()->ofString()->nullable() ?? [],
32+
];
33+
}
34+
2435
/**
2536
* @return positive-int
2637
*/

0 commit comments

Comments
 (0)