@@ -482,17 +482,39 @@ Note: As of 2018-10-13, there is a bug preventing you from retrieving more than
482
482
483
483
### Requested resources (pick from shelf)
484
484
485
- ```` php
485
+ ``` php
486
486
$library = $alma->conf->libraries['LIBRARY_CODE'];
487
487
$requests = $alma->taskLists->getRequestedResources($library, 'DEFAULT_CIRC_DESK', [
488
488
'printed' => 'N',
489
489
]);
490
490
foreach ($requests as $request) {
491
491
echo "- {$request->resource_metadata->title} {$request->resource_metadata->mms_id->value}\n";
492
492
}
493
- ````
493
+ ```
494
+
495
+ ## Automatic retries on errors
496
+
497
+ If the client receives a 429 (rate limiting) response from Alma, it will sleep for a short time (0.5 seconds by default)
498
+ and retry the request a configurable number of times (10 by default), before giving up and throwing a
499
+ ` Scriptotek\Alma\Exception\MaxNumberOfAttemptsExhausted ` .
500
+ Both the max number of attempts and the sleep time can be configured:
501
+
502
+ ``` php
503
+ $client->maxAttempts = 5;
504
+ $client->sleepTimeOnRetry = 3; // seconds
505
+ ```
494
506
495
- ## Laravel 5 integration
507
+ If the client receives a 5XX server error, it will by default not retry the request, but this can be configured.
508
+ This can be useful when retrieving large Analytics reports, which have a tendency to fail intermittently.
509
+
510
+ ``` php
511
+ $client->maxAttemptsOnServerError = 10;
512
+ $client->sleepTimeOnServerError = 10; // seconds
513
+ ```
514
+
515
+ When the number of retries have been exhausted, a ` Scriptotek\Alma\Exception\RequestFailed ` exception is thrown.
516
+
517
+ ## Laravel integration
496
518
497
519
This project ships with an auto-discoverable service provider and facade. Run
498
520
@@ -509,6 +531,48 @@ And the facade:
509
531
510
532
'Alma' => Scriptotek\Alma\Laravel\Facade::class,
511
533
534
+ ### Customizing the HTTP client stack
535
+
536
+ If the Laravel [ Service Container] ( https://laravel.com/docs/master/providers )
537
+ contains a PSR-18 HTTP Client bound to ` Psr\Http\Client\ClientInterface ` ,
538
+ Alma Client will use that implementation instead of instantiating its own HTTP
539
+ client.
540
+
541
+ Here's an example service provider that registers a HTTP client with some middleware
542
+ from [ php-http/client-common] ( http://docs.php-http.org/en/latest/plugins/introduction.html#install ) :
543
+
544
+ ``` php
545
+ <?php
546
+
547
+ namespace App\Providers;
548
+
549
+ use Http\Client\Common\Plugin\ContentLengthPlugin;
550
+ use Http\Client\Common\Plugin\ErrorPlugin;
551
+ use Http\Client\Common\Plugin\RetryPlugin;
552
+ use Http\Client\Common\PluginClient;
553
+ use Http\Factory\Discovery\HttpClient;
554
+ use Illuminate\Support\ServiceProvider;
555
+ use Psr\Http\Client\ClientInterface;
556
+
557
+ class HttpServiceProvider extends ServiceProvider
558
+ {
559
+ public function register()
560
+ {
561
+ $this->app->singleton(ClientInterface::class, function($app) {
562
+ return new PluginClient(
563
+ HttpClient::client(),
564
+ [
565
+ new ContentLengthPlugin(),
566
+ new RetryPlugin([
567
+ 'retries' => 10,
568
+ ]),
569
+ new ErrorPlugin(),
570
+ ]
571
+ );
572
+ });
573
+ }
574
+ }
575
+ ```
512
576
513
577
## Future plans
514
578
0 commit comments