Skip to content

Commit 9421780

Browse files
committed
Allow base url and extra headers to be set differently for IZ/NZ
1 parent ee93c73 commit 9421780

File tree

4 files changed

+49
-38
lines changed

4 files changed

+49
-38
lines changed

config/alma.php

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,20 @@
1515
|--------------------------------------------------------------------------
1616
*/
1717
'iz' => [
18+
// API key for institution zone
1819
'key' => env('ALMA_IZ_KEY'),
20+
21+
// SRU URL for institution zone
1922
'sru' => env('ALMA_IZ_SRU_URL'),
23+
24+
// Base URL for institution zone. This only needs to be specified if you
25+
// use a proxy or other non-standard URL.
26+
'baseUrl' => null,
27+
28+
// Optional list of extra headers to send with each request.
29+
'extraHeaders' => [
30+
// 'x-proxy-auth' => 'custom proxy auth'
31+
],
2032
],
2133

2234
/*
@@ -25,27 +37,19 @@
2537
|--------------------------------------------------------------------------
2638
*/
2739
'nz' => [
40+
// API key for institution zone
2841
'key' => env('ALMA_NZ_KEY'),
42+
43+
// SRU URL for institution zone
2944
'sru' => env('ALMA_NZ_SRU_URL'),
30-
],
3145

32-
/*
33-
|--------------------------------------------------------------------------
34-
| Base URL
35-
|--------------------------------------------------------------------------
36-
| This is only necessary to set if you connect to a non-standard API URL,
37-
| for instance though a proxy.
38-
*/
39-
'baseUrl' => null,
46+
// Base URL for institution zone. This only needs to be specified if you
47+
// use a proxy or other non-standard URL.
48+
'baseUrl' => null,
4049

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'
50+
// Optional list of extra headers to send with each request.
51+
'extraHeaders' => [
52+
// 'x-proxy-auth' => 'custom proxy auth'
53+
],
4954
],
50-
5155
];

spec/ClientSpec.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function let()
2828
return $http;
2929
}
3030

31-
public function it_can_be_constructed_with_custom_base_url()
31+
public function it_can_be_configured_with_custom_base_url()
3232
{
3333
$this->beConstructedWith('DummyKey');
3434
$this->setBaseUrl('http://proxy.foxy');

src/Client.php

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ class Client
120120
* @param ?HttpClientInterface $http
121121
* @param ?RequestFactoryInterface $requestFactory
122122
* @param ?UriFactoryInterface $uriFactory
123-
* @param ?string $baseUrl
124-
* @param array $extraHeaders
125123
*
126124
* @throws \ErrorException
127125
*/
@@ -131,9 +129,7 @@ public function __construct(
131129
$zone = Zones::INSTITUTION,
132130
HttpClientInterface $http = null,
133131
RequestFactoryInterface $requestFactory = null,
134-
UriFactoryInterface $uriFactory = null,
135-
string $baseUrl = null,
136-
array $extraHeaders = []
132+
UriFactoryInterface $uriFactory = null
137133
) {
138134
$this->http = new PluginClient(
139135
$http ?: HttpClient::client(),
@@ -147,14 +143,10 @@ public function __construct(
147143

148144
$this->key = $key;
149145

150-
if (!is_null($baseUrl)) {
151-
$this->setBaseUrl($baseUrl);
152-
} else {
146+
if (!is_null($region)) {
153147
$this->setRegion($region);
154148
}
155149

156-
$this->extraHeaders = $extraHeaders;
157-
158150
$this->zone = $zone;
159151

160152
$this->bibs = new Bibs($this);
@@ -176,9 +168,7 @@ public function __construct(
176168
Zones::NETWORK,
177169
$this->http,
178170
$this->requestFactory,
179-
$this->uriFactory,
180-
$baseUrl,
181-
$extraHeaders
171+
$this->uriFactory
182172
);
183173
} elseif ($zone != Zones::NETWORK) {
184174
throw new AlmaClientException('Invalid zone name.');
@@ -253,9 +243,18 @@ public function setBaseUrl(string $baseUrl)
253243
{
254244
$this->baseUrl = $baseUrl;
255245

256-
if (!is_null($this->nz)) {
257-
$this->nz->setBaseUrl($baseUrl);
258-
}
246+
return $this;
247+
}
248+
249+
/**
250+
* Set extra request headers.
251+
*
252+
* @param array $extraHeaders
253+
* @return $this
254+
*/
255+
public function setExtraHeaders(array $extraHeaders)
256+
{
257+
$this->extraHeaders = $extraHeaders;
259258

260259
return $this;
261260
}

src/Laravel/ServiceProvider.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,22 @@ public function register()
5151
Zones::INSTITUTION,
5252
isset($app[HttpClientInterface::class]) ? $app[HttpClientInterface::class] : null,
5353
isset($app[RequestFactoryInterface::class]) ? $app[RequestFactoryInterface::class] : null,
54-
isset($app[UriFactoryInterface::class]) ? $app[UriFactoryInterface::class] : null,
55-
$app['config']->get('alma.baseUrl'),
56-
$app['config']->get('alma.extraHeaders', [])
54+
isset($app[UriFactoryInterface::class]) ? $app[UriFactoryInterface::class] : null
5755
);
5856

57+
if ($app['config']->get('alma.iz.baseUrl')) {
58+
$alma->setBaseUrl($app['config']->get('alma.iz.baseUrl'));
59+
}
60+
$alma->setExtraHeaders($app['config']->get('alma.extraHeaders', []));
61+
5962
// Set network zone key, if any
6063
$alma->nz->setKey($app['config']->get('alma.nz.key'));
6164

65+
if ($app['config']->get('alma.nz.baseUrl')) {
66+
$alma->nz->setBaseUrl($app['config']->get('alma.nz.baseUrl'));
67+
}
68+
$alma->nz->setExtraHeaders($app['config']->get('alma.nz.extraHeaders', []));
69+
6270
// Optionally, attach SRU client for institution zone
6371
if ($app['config']->get('alma.iz.sru')) {
6472
$alma->setSruClient(new SruClient(

0 commit comments

Comments
 (0)