Skip to content

Commit ff3c472

Browse files
committed
Add support for setting custom base url
1 parent 536c67f commit ff3c472

File tree

5 files changed

+55
-8
lines changed

5 files changed

+55
-8
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ $alma = new AlmaClient('MY_SECRET_API_KEY', 'eu');
7777
```
7878

7979
where `'eu'` is the region code for Europe (use `'na'` for North America or `'ap'` for Asia Pacific).
80+
By default, the API base URL is generated from the region code,
81+
but if you're connecting to the API through a proxy, you can set the base URL directly:
82+
83+
```
84+
$alma = new AlmaClient('MY_SECRET_API_KEY')
85+
->setBaseUrl('https://gw-uio.intark.uh-it.no/alma/v1');
86+
```
8087

8188
If your Alma instance is connected to a network zone and you want to work
8289
with bib records there, you can also add an API key for the network zone:

config/alma.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@
2929
'sru' => env('ALMA_NZ_SRU_URL'),
3030
],
3131

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,
40+
3241
];

spec/ClientSpec.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public function let()
2828
return $http;
2929
}
3030

31+
public function it_can_be_constructed_with_custom_base_url()
32+
{
33+
$this->beConstructedWith('DummyKey');
34+
$this->setBaseUrl('http://proxy.foxy');
35+
$this->baseUrl->shouldBe('http://proxy.foxy');
36+
}
37+
38+
3139
protected function httpWithResponseBody($body, $statusCode = 200, $headers = [])
3240
{
3341
$http = $this->let();

src/Client.php

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class Client
117117
* @param ?HttpClientInterface $http
118118
* @param ?RequestFactoryInterface $requestFactory
119119
* @param ?UriFactoryInterface $uriFactory
120+
* @param ?string $baseUrl
120121
*
121122
* @throws \ErrorException
122123
*/
@@ -126,7 +127,8 @@ public function __construct(
126127
$zone = Zones::INSTITUTION,
127128
HttpClientInterface $http = null,
128129
RequestFactoryInterface $requestFactory = null,
129-
UriFactoryInterface $uriFactory = null
130+
UriFactoryInterface $uriFactory = null,
131+
string $baseUrl = null
130132
) {
131133
$this->http = new PluginClient(
132134
$http ?: HttpClient::client(),
@@ -139,7 +141,12 @@ public function __construct(
139141
$this->uriFactory = $uriFactory ?: HttpFactory::uriFactory();
140142

141143
$this->key = $key;
142-
$this->setRegion($region);
144+
145+
if (!is_null($baseUrl)) {
146+
$this->setBaseUrl($baseUrl);
147+
} else {
148+
$this->setRegion($region);
149+
}
143150

144151
$this->zone = $zone;
145152

@@ -156,7 +163,7 @@ public function __construct(
156163
$this->taskLists = new TaskLists($this);
157164

158165
if ($zone == Zones::INSTITUTION) {
159-
$this->nz = new self(null, $region, Zones::NETWORK, $this->http, $this->requestFactory, $this->uriFactory);
166+
$this->nz = new self(null, $region, Zones::NETWORK, $this->http, $this->requestFactory, $this->uriFactory, $baseUrl);
160167
} elseif ($zone != Zones::NETWORK) {
161168
throw new AlmaClientException('Invalid zone name.');
162169
}
@@ -207,17 +214,32 @@ public function setKey($key)
207214
* Set the Alma region code ('na' for North America, 'eu' for Europe, 'ap' for Asia Pacific).
208215
*
209216
* @param $regionCode
210-
*
211-
* @throws \ErrorException
212-
*
217+
* @throws AlmaClientException
213218
* @return $this
214219
*/
215220
public function setRegion($regionCode)
216221
{
217222
if (!in_array($regionCode, ['na', 'eu', 'ap'])) {
218223
throw new AlmaClientException('Invalid region code');
219224
}
220-
$this->baseUrl = 'https://api-' . $regionCode . '.hosted.exlibrisgroup.com/almaws/v1';
225+
$this->setBaseUrl('https://api-' . $regionCode . '.hosted.exlibrisgroup.com/almaws/v1');
226+
227+
return $this;
228+
}
229+
230+
/**
231+
* Set the Alma API base url.
232+
*
233+
* @param string $baseUrl
234+
* @return $this
235+
*/
236+
public function setBaseUrl(string $baseUrl)
237+
{
238+
$this->baseUrl = $baseUrl;
239+
240+
if (!is_null($this->nz)) {
241+
$this->nz->setBaseUrl($baseUrl);
242+
}
221243

222244
return $this;
223245
}

src/Laravel/ServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ 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
54+
isset($app[UriFactoryInterface::class]) ? $app[UriFactoryInterface::class] : null,
55+
$app['config']->get('alma.baseUrl')
5556
);
5657

5758
// Set network zone key, if any

0 commit comments

Comments
 (0)