1 | <?php |
||
32 | class Client |
||
33 | { |
||
34 | public $baseUrl; |
||
35 | |||
36 | /** @var string Alma zone (institution or network) */ |
||
37 | public $zone; |
||
38 | |||
39 | /** @var string Alma Developers Network API key for this zone */ |
||
40 | public $key; |
||
41 | |||
42 | /** @var Client Network zone instance */ |
||
43 | public $nz; |
||
44 | |||
45 | /** @var HttpClient */ |
||
46 | protected $http; |
||
47 | |||
48 | /** @var MessageFactory */ |
||
49 | protected $messageFactory; |
||
50 | |||
51 | /** @var UriFactory */ |
||
52 | protected $uriFactory; |
||
53 | |||
54 | /** @var SruClient */ |
||
55 | public $sru; |
||
56 | |||
57 | /** @var Bibs */ |
||
58 | public $bibs; |
||
59 | |||
60 | /** @var Analytics */ |
||
61 | public $analytics; |
||
62 | |||
63 | /** @var Users */ |
||
64 | public $users; |
||
65 | |||
66 | /** @var int Max number of retries if we get 429 errors */ |
||
67 | public $maxAttempts = 10; |
||
68 | |||
69 | /** @var float Number of seconds to sleep before retrying */ |
||
70 | public $sleepTimeOnRetry = 0.5; |
||
71 | |||
72 | /** |
||
73 | * Create a new client to connect to a given Alma instance. |
||
74 | * |
||
75 | * @param string $key API key |
||
76 | * @param string $region Hosted region code, used to build base URL |
||
77 | * @param string $zone Alma zone (Either Zones::INSTITUTION or Zones::NETWORK) |
||
78 | * @param HttpClient $http |
||
79 | * @param MessageFactory $messageFactory |
||
80 | * @param UriFactory $uriFactory |
||
81 | * |
||
82 | * @throws \ErrorException |
||
83 | */ |
||
84 | public function __construct( |
||
114 | |||
115 | /** |
||
116 | * Attach an SRU client (so you can search for Bib records). |
||
117 | * |
||
118 | * @param SruClient $sru |
||
119 | */ |
||
120 | public function setSruClient(SruClient $sru) |
||
124 | |||
125 | /** |
||
126 | * Assert that an SRU client is connected. Throws SruClientNotSetException if not. |
||
127 | * |
||
128 | * @throws SruClientNotSetException |
||
129 | */ |
||
130 | public function assertHasSruClient() |
||
136 | |||
137 | /** |
||
138 | * Set the API key for this Alma instance. |
||
139 | * |
||
140 | * @param string $key The API key |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function setKey($key) |
||
150 | |||
151 | /** |
||
152 | * Set the Alma region code ('na' for North America, 'eu' for Europe, 'ap' for Asia Pacific). |
||
153 | * |
||
154 | * @param $regionCode |
||
155 | * |
||
156 | * @throws \ErrorException |
||
157 | * |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function setRegion($regionCode) |
||
169 | |||
170 | /** |
||
171 | * @param string $url |
||
172 | * @param array $query |
||
173 | * |
||
174 | * @return UriInterface |
||
175 | */ |
||
176 | protected function getFullUri($url, $query=[]) |
||
183 | |||
184 | /** |
||
185 | * Make a synchronous HTTP request and return a PSR7 response if successful. |
||
186 | * In the case of intermittent errors (connection problem, 429 or 5XX error), the request is |
||
187 | * attempted a maximum of {$this->maxAttempts} times with a sleep of {$this->sleepTimeOnRetry} |
||
188 | * between each attempt to avoid hammering the server. |
||
189 | * |
||
190 | * @param RequestInterface $request |
||
191 | * @param int $attempt |
||
192 | * |
||
193 | * @return ResponseInterface |
||
194 | */ |
||
195 | public function request(RequestInterface $request, $attempt = 1) |
||
228 | |||
229 | /** |
||
230 | * Make a GET request. |
||
231 | * |
||
232 | * @param string $url |
||
233 | * @param array $query |
||
234 | * @param string $contentType |
||
235 | * |
||
236 | * @return string Response body |
||
237 | */ |
||
238 | public function get($url, $query = [], $contentType = 'application/json') |
||
249 | |||
250 | /** |
||
251 | * Make a GET request, accepting JSON. |
||
252 | * |
||
253 | * @param string $url |
||
254 | * @param array $query |
||
255 | * |
||
256 | * @return \stdClass JSON response as an object. |
||
257 | */ |
||
258 | public function getJSON($url, $query = []) |
||
264 | |||
265 | /** |
||
266 | * Make a GET request, accepting XML. |
||
267 | * |
||
268 | * @param string $url |
||
269 | * @param array $query |
||
270 | * |
||
271 | * @return QuiteSimpleXMLElement |
||
272 | */ |
||
273 | public function getXML($url, $query = []) |
||
279 | |||
280 | /** |
||
281 | * Make a PUT request. |
||
282 | * |
||
283 | * @param string $url |
||
284 | * @param $data |
||
285 | * @param string $contentType |
||
286 | * |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function put($url, $data, $contentType = 'application/json') |
||
303 | |||
304 | /** |
||
305 | * Make a PUT request, sending JSON data. |
||
306 | * |
||
307 | * @param string $url |
||
308 | * @param $data |
||
309 | * |
||
310 | * @return bool |
||
311 | */ |
||
312 | public function putJSON($url, $data) |
||
318 | |||
319 | /** |
||
320 | * Make a PUT request, sending XML data. |
||
321 | * |
||
322 | * @param string $url |
||
323 | * @param $data |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | public function putXML($url, $data) |
||
331 | |||
332 | /** |
||
333 | * Get the redirect target location of an URL, or null if not a redirect. |
||
334 | * |
||
335 | * @param string $url |
||
336 | * @param array $query |
||
337 | * |
||
338 | * @return string|null |
||
339 | */ |
||
340 | public function getRedirectLocation($url, $query = []) |
||
355 | } |
||
356 |