Skip to content

Commit

Permalink
Allow base url and extra headers to be set differently for IZ/NZ
Browse files Browse the repository at this point in the history
  • Loading branch information
danmichaelo committed Apr 14, 2021
1 parent ee93c73 commit 9421780
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 38 deletions.
40 changes: 22 additions & 18 deletions config/alma.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,20 @@
|--------------------------------------------------------------------------
*/
'iz' => [
// API key for institution zone
'key' => env('ALMA_IZ_KEY'),

// SRU URL for institution zone
'sru' => env('ALMA_IZ_SRU_URL'),

// Base URL for institution zone. This only needs to be specified if you
// use a proxy or other non-standard URL.
'baseUrl' => null,

// Optional list of extra headers to send with each request.
'extraHeaders' => [
// 'x-proxy-auth' => 'custom proxy auth'
],
],

/*
Expand All @@ -25,27 +37,19 @@
|--------------------------------------------------------------------------
*/
'nz' => [
// API key for institution zone
'key' => env('ALMA_NZ_KEY'),

// SRU URL for institution zone
'sru' => env('ALMA_NZ_SRU_URL'),
],

/*
|--------------------------------------------------------------------------
| Base URL
|--------------------------------------------------------------------------
| This is only necessary to set if you connect to a non-standard API URL,
| for instance though a proxy.
*/
'baseUrl' => null,
// Base URL for institution zone. This only needs to be specified if you
// use a proxy or other non-standard URL.
'baseUrl' => null,

/*
|--------------------------------------------------------------------------
| Extra request headers
|--------------------------------------------------------------------------
| An associated array of extra headers to be sent with each request.
*/
'extraHeaders' => [
// 'x-proxy-auth' => 'custom proxy auth'
// Optional list of extra headers to send with each request.
'extraHeaders' => [
// 'x-proxy-auth' => 'custom proxy auth'
],
],

];
2 changes: 1 addition & 1 deletion spec/ClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function let()
return $http;
}

public function it_can_be_constructed_with_custom_base_url()
public function it_can_be_configured_with_custom_base_url()
{
$this->beConstructedWith('DummyKey');
$this->setBaseUrl('http://proxy.foxy');
Expand Down
31 changes: 15 additions & 16 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ class Client
* @param ?HttpClientInterface $http
* @param ?RequestFactoryInterface $requestFactory
* @param ?UriFactoryInterface $uriFactory
* @param ?string $baseUrl
* @param array $extraHeaders
*
* @throws \ErrorException
*/
Expand All @@ -131,9 +129,7 @@ public function __construct(
$zone = Zones::INSTITUTION,
HttpClientInterface $http = null,
RequestFactoryInterface $requestFactory = null,
UriFactoryInterface $uriFactory = null,
string $baseUrl = null,
array $extraHeaders = []
UriFactoryInterface $uriFactory = null
) {
$this->http = new PluginClient(
$http ?: HttpClient::client(),
Expand All @@ -147,14 +143,10 @@ public function __construct(

$this->key = $key;

if (!is_null($baseUrl)) {
$this->setBaseUrl($baseUrl);
} else {
if (!is_null($region)) {
$this->setRegion($region);
}

$this->extraHeaders = $extraHeaders;

$this->zone = $zone;

$this->bibs = new Bibs($this);
Expand All @@ -176,9 +168,7 @@ public function __construct(
Zones::NETWORK,
$this->http,
$this->requestFactory,
$this->uriFactory,
$baseUrl,
$extraHeaders
$this->uriFactory
);
} elseif ($zone != Zones::NETWORK) {
throw new AlmaClientException('Invalid zone name.');
Expand Down Expand Up @@ -253,9 +243,18 @@ public function setBaseUrl(string $baseUrl)
{
$this->baseUrl = $baseUrl;

if (!is_null($this->nz)) {
$this->nz->setBaseUrl($baseUrl);
}
return $this;
}

/**
* Set extra request headers.
*
* @param array $extraHeaders
* @return $this
*/
public function setExtraHeaders(array $extraHeaders)
{
$this->extraHeaders = $extraHeaders;

return $this;
}
Expand Down
14 changes: 11 additions & 3 deletions src/Laravel/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,22 @@ public function register()
Zones::INSTITUTION,
isset($app[HttpClientInterface::class]) ? $app[HttpClientInterface::class] : null,
isset($app[RequestFactoryInterface::class]) ? $app[RequestFactoryInterface::class] : null,
isset($app[UriFactoryInterface::class]) ? $app[UriFactoryInterface::class] : null,
$app['config']->get('alma.baseUrl'),
$app['config']->get('alma.extraHeaders', [])
isset($app[UriFactoryInterface::class]) ? $app[UriFactoryInterface::class] : null
);

if ($app['config']->get('alma.iz.baseUrl')) {
$alma->setBaseUrl($app['config']->get('alma.iz.baseUrl'));
}
$alma->setExtraHeaders($app['config']->get('alma.extraHeaders', []));

// Set network zone key, if any
$alma->nz->setKey($app['config']->get('alma.nz.key'));

if ($app['config']->get('alma.nz.baseUrl')) {
$alma->nz->setBaseUrl($app['config']->get('alma.nz.baseUrl'));
}
$alma->nz->setExtraHeaders($app['config']->get('alma.nz.extraHeaders', []));

// Optionally, attach SRU client for institution zone
if ($app['config']->get('alma.iz.sru')) {
$alma->setSruClient(new SruClient(
Expand Down

0 comments on commit 9421780

Please sign in to comment.