|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma; |
|
4
|
|
|
|
|
5
|
|
|
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement; |
|
6
|
|
|
use GuzzleHttp\Client as HttpClient; |
|
7
|
|
|
use GuzzleHttp\Exception\RequestException; |
|
8
|
|
|
use Scriptotek\Alma\Analytics\Analytics; |
|
9
|
|
|
use Scriptotek\Alma\Bibs\Bibs; |
|
10
|
|
|
use Scriptotek\Alma\Exception\ClientException; |
|
11
|
|
|
use Scriptotek\Alma\Exception\SruClientNotSetException; |
|
12
|
|
|
use Scriptotek\Alma\Users\Users; |
|
13
|
|
|
use Scriptotek\Sru\Client as SruClient; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Alma client. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Client |
|
19
|
|
|
{ |
|
20
|
|
|
public $baseUrl; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string Alma zone (institution or network) */ |
|
23
|
|
|
public $zone; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string Alma Developers Network API key for this zone */ |
|
26
|
|
|
public $key; |
|
27
|
|
|
|
|
28
|
|
|
/** @var string Network zone instance */ |
|
29
|
|
|
public $nz; |
|
30
|
|
|
|
|
31
|
|
|
/** @var HttpClient */ |
|
32
|
|
|
protected $httpClient; |
|
33
|
|
|
|
|
34
|
|
|
/** @var SruClient */ |
|
35
|
|
|
public $sru; |
|
36
|
|
|
|
|
37
|
|
|
/** @var Bibs */ |
|
38
|
|
|
public $bibs; |
|
39
|
|
|
|
|
40
|
|
|
/** @var Analytics */ |
|
41
|
|
|
public $analytics; |
|
42
|
|
|
|
|
43
|
|
|
/** @var Users */ |
|
44
|
|
|
public $users; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Create a new client to connect to a given Alma instance. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $key API key |
|
50
|
|
|
* @param string $region Hosted region code, used to build base URL |
|
51
|
|
|
* @param string $zone Alma zone (Either Zones::INSTITUTION or Zones::NETWORK) |
|
52
|
|
|
* @param HttpClient $httpClient |
|
53
|
|
|
* |
|
54
|
|
|
* @throws \ErrorException |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct($key = null, $region = 'eu', $zone = Zones::INSTITUTION, HttpClient $httpClient = null) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->key = $key; |
|
59
|
|
|
$this->setRegion($region); |
|
60
|
|
|
$this->httpClient = $httpClient ?: new HttpClient(); |
|
61
|
|
|
$this->zone = $zone; |
|
62
|
|
|
$this->bibs = new Bibs($this); // Or do some magic instead? |
|
63
|
|
|
$this->analytics = new Analytics($this); // Or do some magic instead? |
|
64
|
|
|
$this->users = new Users($this); // Or do some magic instead? |
|
65
|
|
|
if ($zone == Zones::INSTITUTION) { |
|
66
|
|
|
$this->nz = new self(null, $region, Zones::NETWORK, $this->httpClient); |
|
|
|
|
|
|
67
|
|
|
} elseif ($zone != Zones::NETWORK) { |
|
68
|
|
|
throw new ClientException('Invalid zone name.'); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Attach an SRU client (so you can search for Bib records). |
|
74
|
|
|
* |
|
75
|
|
|
* @param SruClient $sru |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setSruClient(SruClient $sru) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->sru = $sru; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Assert that an SRU client is connected. Throws SruClientNotSetException if not. |
|
84
|
|
|
* |
|
85
|
|
|
* @throws SruClientNotSetException |
|
86
|
|
|
*/ |
|
87
|
|
|
public function assertHasSruClient() |
|
88
|
|
|
{ |
|
89
|
|
|
if (!isset($this->sru)) { |
|
90
|
|
|
throw new SruClientNotSetException(); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Set the API key for this Alma instance. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $key The API key |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setKey($key) |
|
101
|
|
|
{ |
|
102
|
|
|
$this->key = $key; |
|
103
|
|
|
|
|
104
|
|
|
return $this; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Set the Alma region code ('na' for North America, 'eu' for Europe, 'ap' for Asia Pacific). |
|
109
|
|
|
* |
|
110
|
|
|
* @param $regionCode |
|
111
|
|
|
* @return $this |
|
112
|
|
|
* @throws \ErrorException |
|
113
|
|
|
*/ |
|
114
|
|
|
public function setRegion($regionCode) |
|
115
|
|
|
{ |
|
116
|
|
|
if (!in_array($regionCode, ['na', 'eu', 'ap'])) { |
|
117
|
|
|
throw new ClientException('Invalid region code'); |
|
118
|
|
|
} |
|
119
|
|
|
$this->baseUrl = 'https://api-' . $regionCode . '.hosted.exlibrisgroup.com/almaws/v1'; |
|
120
|
|
|
|
|
121
|
|
|
return $this; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param $url |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function getFullUrl($url) |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->baseUrl . $url; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param array $options |
|
136
|
|
|
* |
|
137
|
|
|
* @return array |
|
138
|
|
|
*/ |
|
139
|
|
|
protected function getHttpOptions($options = []) |
|
140
|
|
|
{ |
|
141
|
|
|
if (!$this->key) { |
|
142
|
|
|
throw new ClientException('No API key defined for ' . $this->zone); |
|
143
|
|
|
} |
|
144
|
|
|
$defaultOptions = [ |
|
145
|
|
|
'headers' => ['Authorization' => 'apikey ' . $this->key], |
|
146
|
|
|
]; |
|
147
|
|
|
|
|
148
|
|
|
return array_merge_recursive($defaultOptions, $options); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Make a HTTP request. |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $method |
|
155
|
|
|
* @param string $url |
|
156
|
|
|
* @param array $options |
|
157
|
|
|
* |
|
158
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
159
|
|
|
*/ |
|
160
|
|
|
public function request($method, $url, $options = []) |
|
161
|
|
|
{ |
|
162
|
|
|
try { |
|
163
|
|
|
return $this->httpClient->request($method, $this->getFullUrl($url), $this->getHttpOptions($options)); |
|
164
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) { |
|
165
|
|
|
$this->handleError($e->getResponse()); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
public function handleError($response) |
|
170
|
|
|
{ |
|
171
|
|
|
$msg = $response->getBody(); |
|
172
|
|
|
throw new ClientException('Client error ' . $response->getStatusCode() . ': ' . $msg); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Make a GET request, accepting JSON. |
|
177
|
|
|
* |
|
178
|
|
|
* @param string $url |
|
179
|
|
|
* @param array $query |
|
180
|
|
|
* |
|
181
|
|
|
* @return mixed |
|
182
|
|
|
*/ |
|
183
|
|
View Code Duplication |
public function getJSON($url, $query = []) |
|
|
|
|
|
|
184
|
|
|
{ |
|
185
|
|
|
$response = $this->request('GET', $url, [ |
|
186
|
|
|
'query' => $query, |
|
187
|
|
|
'headers' => ['Accept' => 'application/json'], |
|
188
|
|
|
]); |
|
189
|
|
|
|
|
190
|
|
|
return json_decode($response->getBody()); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Make a GET request, accepting XML. |
|
195
|
|
|
* |
|
196
|
|
|
* @param string $url |
|
197
|
|
|
* @param array $query |
|
198
|
|
|
* |
|
199
|
|
|
* @return mixed |
|
200
|
|
|
*/ |
|
201
|
|
View Code Duplication |
public function getXML($url, $query = []) |
|
|
|
|
|
|
202
|
|
|
{ |
|
203
|
|
|
$response = $this->request('GET', $url, [ |
|
204
|
|
|
'query' => $query, |
|
205
|
|
|
'headers' => ['Accept' => 'application/xml'], |
|
206
|
|
|
]); |
|
207
|
|
|
|
|
208
|
|
|
return new QuiteSimpleXMLElement(strval($response->getBody())); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Make a PUT request. |
|
213
|
|
|
* |
|
214
|
|
|
* @param string $url |
|
215
|
|
|
* @param $data |
|
216
|
|
|
* @param string $contentType |
|
217
|
|
|
* @return bool |
|
218
|
|
|
*/ |
|
219
|
|
|
public function put($url, $data, $contentType = 'application/json') |
|
220
|
|
|
{ |
|
221
|
|
|
$data = json_encode($data); |
|
222
|
|
|
|
|
223
|
|
|
$response = $this->request('PUT', $url, [ |
|
224
|
|
|
'body' => $data, |
|
225
|
|
|
'headers' => [ |
|
226
|
|
|
'Content-Type' => $contentType, |
|
227
|
|
|
'Accept' => $contentType, |
|
228
|
|
|
], |
|
229
|
|
|
]); |
|
230
|
|
|
|
|
231
|
|
|
return $response->getStatusCode() == '200'; |
|
232
|
|
|
// TODO: Check if there are other success codes that can be returned |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
/** |
|
236
|
|
|
* Make a PUT request. |
|
237
|
|
|
* |
|
238
|
|
|
* @param string $url |
|
239
|
|
|
* @param $data |
|
240
|
|
|
* |
|
241
|
|
|
* @return bool |
|
242
|
|
|
*/ |
|
243
|
|
|
public function putXML($url, $data) |
|
244
|
|
|
{ |
|
245
|
|
|
return $this->put($url, $data, 'application/xml'); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Get the redirect target location of an URL, or null if not a redirect. |
|
250
|
|
|
* |
|
251
|
|
|
* @param string $url |
|
252
|
|
|
* @param array $query |
|
253
|
|
|
* |
|
254
|
|
|
* @return string|null |
|
255
|
|
|
*/ |
|
256
|
|
|
public function getRedirectLocation($url, $query = []) |
|
257
|
|
|
{ |
|
258
|
|
|
try { |
|
259
|
|
|
$response = $this->httpClient->request('GET', $this->getFullUrl($url), $this->getHttpOptions([ |
|
260
|
|
|
'query' => $query, |
|
261
|
|
|
'headers' => ['Accept' => 'application/json'], |
|
262
|
|
|
'allow_redirects' => false, |
|
263
|
|
|
])); |
|
264
|
|
|
} catch (RequestException $e) { |
|
265
|
|
|
// We receive a 400 if the barcode is invalid |
|
266
|
|
|
// if ($e->hasResponse()) { |
|
|
|
|
|
|
267
|
|
|
// echo $e->getResponse()->getStatusCode() . "\n"; |
|
|
|
|
|
|
268
|
|
|
// echo $e->getResponse()->getBody() . "\n"; |
|
|
|
|
|
|
269
|
|
|
// } |
|
270
|
|
|
return; |
|
271
|
|
|
} |
|
272
|
|
|
$locations = $response->getHeader('Location'); |
|
273
|
|
|
|
|
274
|
|
|
return count($locations) ? $locations[0] : null; |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..