Skip to content

Commit 410243f

Browse files
committed
Add option to pass extra headers
1 parent ff3c472 commit 410243f

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

config/alma.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,14 @@
3838
*/
3939
'baseUrl' => null,
4040

41+
/*
42+
|--------------------------------------------------------------------------
43+
| Extra request headers
44+
|--------------------------------------------------------------------------
45+
| An associated array of extra headers to be sent with each request.
46+
*/
47+
'extraHeaders' => [
48+
// 'x-proxy-auth' => 'custom proxy auth'
49+
],
50+
4151
];

src/Client.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ class Client
8888
/** @var float Number of seconds to sleep before retrying after a server error */
8989
public $sleepTimeOnServerError = 10;
9090

91+
/** @var array Extra request headers */
92+
public $extraHeaders = [];
93+
9194
/**
9295
* @var Conf
9396
*/
@@ -118,6 +121,7 @@ class Client
118121
* @param ?RequestFactoryInterface $requestFactory
119122
* @param ?UriFactoryInterface $uriFactory
120123
* @param ?string $baseUrl
124+
* @param array $extraHeaders
121125
*
122126
* @throws \ErrorException
123127
*/
@@ -128,7 +132,8 @@ public function __construct(
128132
HttpClientInterface $http = null,
129133
RequestFactoryInterface $requestFactory = null,
130134
UriFactoryInterface $uriFactory = null,
131-
string $baseUrl = null
135+
string $baseUrl = null,
136+
array $extraHeaders = []
132137
) {
133138
$this->http = new PluginClient(
134139
$http ?: HttpClient::client(),
@@ -148,6 +153,8 @@ public function __construct(
148153
$this->setRegion($region);
149154
}
150155

156+
$this->extraHeaders = $extraHeaders;
157+
151158
$this->zone = $zone;
152159

153160
$this->bibs = new Bibs($this);
@@ -163,7 +170,16 @@ public function __construct(
163170
$this->taskLists = new TaskLists($this);
164171

165172
if ($zone == Zones::INSTITUTION) {
166-
$this->nz = new self(null, $region, Zones::NETWORK, $this->http, $this->requestFactory, $this->uriFactory, $baseUrl);
173+
$this->nz = new self(
174+
null,
175+
$region,
176+
Zones::NETWORK,
177+
$this->http,
178+
$this->requestFactory,
179+
$this->uriFactory,
180+
$baseUrl,
181+
$extraHeaders
182+
);
167183
} elseif ($zone != Zones::NETWORK) {
168184
throw new AlmaClientException('Invalid zone name.');
169185
}
@@ -287,6 +303,9 @@ public function request(RequestInterface $request, $attempt = 1)
287303
if (!$this->key) {
288304
throw new AlmaClientException('No API key defined for ' . $this->zone);
289305
}
306+
foreach ($this->extraHeaders as $key => $val) {
307+
$request = $request->withHeader($key, $val);
308+
}
290309

291310
try {
292311
return $this->http->sendRequest($request);

src/Laravel/ServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public function register()
5252
isset($app[HttpClientInterface::class]) ? $app[HttpClientInterface::class] : null,
5353
isset($app[RequestFactoryInterface::class]) ? $app[RequestFactoryInterface::class] : null,
5454
isset($app[UriFactoryInterface::class]) ? $app[UriFactoryInterface::class] : null,
55-
$app['config']->get('alma.baseUrl')
55+
$app['config']->get('alma.baseUrl'),
56+
$app['config']->get('alma.extraHeaders', [])
5657
);
5758

5859
// Set network zone key, if any

0 commit comments

Comments
 (0)