1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
|
package WebService::ILS;
use Modern::Perl;
our $VERSION = "0.18";
=encoding utf-8
=head1 NAME
WebService::ILS - Standardised library discovery/circulation services
=head1 SYNOPSIS
use WebService::ILS::<Provider Subclass>;
my $ils = WebService::ILS::<Provider Subclass>->new({
client_id => $client_id,
client_secret => $client_secret
});
my %search_params = (
query => "Some keyword",
sort => "rating",
);
my $result = $ils->search(\%search_params);
foreach (@{ $result->{items} }) {
...
}
foreach (2..$result->{pages}) {
$search_params{page} = $_;
my $next_results = $ils->search(\%search_params);
...
}
or
my $native_result = $ils->native_search(\%native_search_params);
=head1 DESCRIPTION
WebService::ILS is an attempt to create a standardised interface for
online library services providers.
In addition, native API interface is provided.
Here we will describe constructor parameters and methods common to all
service providers. Diversions and native interfaces are documented
in corresponding modules.
=head2 Supported service providers
=over 4
=item B<WebService::ILS::OverDrive::Library>
OverDrive Library API L<https://developer.overdrive.com/discovery-apis>
=item B<WebService::ILS::OverDrive::Patron>
OverDrive Circulation API L<https://developer.overdrive.com/circulation-apis>
=back
=head1 INTERFACE
=head2 Error handling
Method calls will die on error. $@ will contain a multi-line string.
See C<error_message()> below.
=head2 Item record
Item record is returned by many methods, so we specify it here.
=over 12
=item C<id>
=item C<isbn>
=item C<title>
=item C<subtitle>
=item C<description>
=item C<author>
=item C<publisher>
=item C<publication_date>
=item C<language>
=item C<rating> => user ratings metrics
=item C<popularity> => checkout metrics
=item C<subjects> => subject categories (tags)
=item C<facets> => a hashref of facet => [values]
=item C<media> => book, e-book, video, audio etc
=item C<formats> => an arrayref of available formats
=item C<images> => a hashref of size => url
=item C<encryption_key> => for decryption purposes
=item C<drm> => subject to drm
=back
Not all fields are available for all service providers.
Field values are not standardised.
=cut
use Carp;
use Hash::Merge;
use Params::Check;
use LWP::UserAgent;
use HTTP::Status qw(:constants);
use MIME::Base64 qw();
use JSON qw(from_json);
our $DEBUG;
my %CONSTRUCTOR_PARAMS_SPEC;
sub _set_param_spec {
my $class = shift;
my $param_spec = shift;
$CONSTRUCTOR_PARAMS_SPEC{$class} = $param_spec;
}
sub _get_param_spec {
my $class = shift;
if (my $ref = ref($class)) {
$class = $ref;
}
my $p_s = $CONSTRUCTOR_PARAMS_SPEC{$class};
return $p_s if $class eq __PACKAGE__;
(my $superclass = $class) =~ s/::\w+$//o;
return Hash::Merge::merge($p_s || {}, $superclass->_get_param_spec);
}
=head1 CONSTRUCTOR
=head2 new (%params_hash or $params_hashref)
=head3 Client (vendor) related constructor params, given by service provider:
=over 12
=item C<client_id> => client (vendor) identifier
=item C<client_secret> => secret key (password)
=item C<library_id> => sometimes service providers provide access
to different "libraries"
=back
=head3 General constructor params:
=over 12
=item C<user_agent> => LWP::UserAgent or a derivative;
usually not needed, one is created for you.
=item C<user_agent_params> => LWP::UserAgent constructor params
so you don't need to create user agent yourself
=item C<access_token> => as returned from the provider authentication system
=item C<access_token_type> => as returned from the provider authentication system
=back
These are also read-only attributes
Not all of client/library params are required for all service providers.
=cut
use Class::Tiny qw(
user_agent
client_id client_secret library_id
access_token access_token_type
);
__PACKAGE__->_set_param_spec({
client_id => { required => 1, defined => 1 },
client_secret => { required => 1, defined => 1 },
library_id => { required => 0, defined => 1 },
access_token => { required => 0 },
access_token_type => { required => 0 },
user_agent => { required => 0 },
user_agent_params => { required => 0 },
});
sub BUILDARGS {
my $self = shift;
my $params = shift || {};
if (!ref( $params )) {
$params = {$params, @_};
}
local $Params::Check::WARNINGS_FATAL = 1;
$params = Params::Check::check($self->_get_param_spec, $params)
or croak "Invalid parameters: ".Params::Check::last_error();
return $params;
}
sub BUILD {
my $self = shift;
my $params = shift;
my $ua_params = delete $params->{user_agent_params} || {};
$self->user_agent( LWP::UserAgent->new(%$ua_params) ) unless $self->user_agent;
delete $self->{user_agent_params};
}
=head1 ATTRIBUTES
=head2 user_agent
As provided to constructor, or auto created. Useful if one wants to
change user agent attributes on the fly, eg
$ils->user_agent->timeout(120);
=head1 DISCOVERY METHODS
=head2 search ($params_hashref)
=head3 Input params:
=over 12
=item C<query> => query (search) string
=item C<page_size> => number of items per results page
=item C<page> => wanted page number
=item C<sort> => resultset sort option (see below)
=back
Sort options are either an array or a comma separated string of options:
=over 12
=item C<publication_date> => date title was published
=item C<available_date> => date title became available for users
=item C<rating> => number of items per results page
=back
Sort order can be added after option with ":", eg
"publication_date:desc,rating:desc"
=head3 Returns search results record:
=over 12
=item C<items> => an array of item records
=item C<page_size> => number of items per results page
=item C<page> => results page number
=item C<pages> => total number of pages
=item C<total> => total number of items found by the search
=back
=head2 item_metadata ($item_id)
=head3 Returns item record
=head2 item_availability ($item_id)
=head3 Returns item availability record:
=over 12
=item C<id>
=item C<available> => boolean
=item C<copies_available> => number of copies available
=item C<copies_owned> => number of copies owned
=item C<type> => availability type, provider dependant
=back
Not all fields are available for all service providers.
For example, some will provide "copies_available", making "available"
redundant, whereas others will just provide "available".
=head2 is_item_available ($item_id)
=head3 Returns boolean
Simplified version of L<item_availability()>
=cut
sub search {
die "search() not implemented";
}
# relevancy availability available_date title author popularity rating price publisher publication_date
sub _parse_sort_string {
my $self = shift;
my $sort = shift or croak "No sort options";
my $xlate_table = shift || {};
my $camelise = shift;
$sort = [split /\s*,\s*/, $sort] unless ref $sort;
foreach (@$sort) {
my ($s,$d) = split ':';
if (exists $xlate_table->{$s}) {
next unless $xlate_table->{$s};
$_ = $xlate_table->{$s};
}
else {
$_ = $s;
}
# join('', map{ ucfirst $_ } split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s));
$_ = join '', map ucfirst, split /(?<=[A-Za-z])_(?=[A-Za-z])|\b/ if $camelise;
$_ = "$_:$d" if $d;
}
return $sort;
}
sub item_metadata {
die "item_metadata() not implemented";
}
sub item_availability {
die "item_availability() not implemented";
}
=head1 INDIVIDUAL USER AUTHENTICATION AND METHODS
=head2 user_id / password
Provider authentication API is used to get an authorized session.
=head3 auth_by_user_id($user_id, $password)
An example:
my $ils = WebService::ILS::Provider({
client_id => $client_id,
client_secret => $client_secret,
});
eval { $ils->auth_by_user_id( $user_id, $password ) };
if ($@) { some_error_handling(); return; }
$session{ils_access_token} = $ils->access_token;
$session{ils_access_token_type} = $ils->access_token_type;
...
Somewhere else in your app:
my $ils = WebService::ILS::Provider({
client_id => $client_id,
client_secret => $client_secret,
access_token => $session{ils_access_token},
access_token_type => $session{ils_access_token_type},
});
my $checkouts = $ils->checkouts;
=head2 Authentication at the provider
User is redirected to the provider authentication url, and after
authenticating at the provider redirected back with some kind of auth token.
Requires url to handle return redirect from the provider.
It can be used as an alternative to FB and Google auth.
This is just to give an idea, specifics heavily depend on the provider
=head3 auth_url ($redirect_back_uri)
Returns provider authentication url to redirect to
=head3 auth_token_param_name ()
Returns auth code url param name
=head3 auth_by_token ($provider_token)
An example:
my $ils = WebService::ILS::Provider({
client_id => $client_id,
client_secret => $client_secret,
});
my $redirect_url = $ils->auth_url("http://myapp.com/ils-auth");
$response->redirect($redirect_url);
...
After successful authentication at the provider, provider redirects
back to specified app url (http://myapp.com/ils-auth)
/ils-auth handler:
my $auth_token = $req->param( $ils->auth_token_param_name )
or some_error_handling(), return;
local $@;
eval { $ils->auth_by_token( $auth_token ) };
if ($@) { some_error_handling(); return; }
$session{ils_access_token} = $ils->access_token;
$session{ils_access_token_type} = $ils->access_token_type;
...
Somewhere else in your app:
passing access token to the constructor as above
=cut
=head1 CIRCULATION METHODS
=head2 patron ()
=head3 Returns patron record:
=over 12
=item C<id>
=item C<active> => boolean
=item C<copies_available> => number of copies available
=item C<checkout_limit> => number of checkouts allowed
=item C<hold_limit> => number of holds allowed
=back
=head2 holds ()
=head3 Returns holds record:
=over 12
=item C<total> => number of items on hold
=item C<items> => list of individual items
=back
In addition to Item record fields described above,
item records will have:
=over 12
=item C<placed_datetime> => hold timestamp, with or without timezone
=item C<queue_position> => user's position in the waiting queue,
if available
=back
=head2 place_hold ($item_id)
=head3 Returns holds item record (as described above)
In addition, C<total> field will be incorported as well.
=head2 remove_hold ($item_id)
=head3 Returns true to indicate success
Returns true in case user does not have a hold on the item.
Throws exception in case of any other failure.
=head2 checkouts ()
=head3 Returns checkout record:
=over 12
=item C<total> => number of items on hold
=item C<items> => list of individual items
=back
In addition to Item record fields described above,
item records will have:
=over 12
=item C<checkout_datetime> => checkout timestamp, with or without timezone
=item C<expires> => date (time) checkout expires
=item C<url> => download/stream url
=item C<files> => an arrayref of downloadable file details
title, url, size
=back
=head2 checkout ($item_id)
=head3 Returns checkout item record (as described above)
In addition, C<total> field will be incorported as well.
=head2 return ($item_id)
=head3 Returns true to indicate success
Returns true in case user does not have the item checked out.
Throws exception in case of any other failure.
=cut
=head1 NATIVE METHODS
All Discovery and Circulation methods (with exception of remove_hold()
and return(), where it does not make sense) have native_*() counterparts,
eg native_search(), native_item_availability(), native_checkout() etc.
In case of single item methods, native_item_availability(),
native_checkout() etc, they take item_id as parameter. Otherwise, it's a
hashref of HTTP request params (GET or POST).
Return value is a record as returned by API.
Individual provider subclasses provide additional provider specific
native methods.
=head1 UTILITY METHODS
=head2 Error constants
=over 4
=item C<ERROR_ACCESS_TOKEN>
=item C<ERROR_NOT_AUTHENTICATED>
=back
=cut
use constant ERROR_ACCESS_TOKEN => "Error: Authorization Failed";
use constant ERROR_NOT_AUTHENTICATED => "Error: User Not Authenticated";
sub invalid_response_exception_string {
my $self = shift;
my $response = shift;
return join "\n",
$response->message,
"Request:" => $response->request->as_string,
"Response:" => $response->as_string
;
}
sub check_response {
my $self = shift;
my $response = shift;
die $self->invalid_response_exception_string($response) unless $response->is_success;
}
=head2 error_message ($exception_string)
=head3 Returns error message probably suitable for displaying to the user
Example:
my $res = eval { $ils->checkout($id) };
if ($@) {
my $msg = $ils->error_message($@);
display($msg);
log_error($@);
}
=head2 is_access_token_error ($exception_string)
=head3 Returns true if the error is access token related
=head2 is_not_authenticated_error ($exception_string)
=head3 Returns true if the error is "Not authenticated"
=cut
sub error_message {
my $self = shift;
my $die_string = shift or return;
$die_string =~ m/(.*?)\n/o;
(my $msg = $1 || $die_string) =~ s! at /.* line \d+\.$!!;
return $msg;
}
sub is_access_token_error {
my $self = shift;
my $die_string = shift or croak "No error message";
return $self->error_message($die_string) eq ERROR_ACCESS_TOKEN;
}
sub is_not_authenticated_error {
my $self = shift;
my $die_string = shift or croak "No error message";
return $self->error_message($die_string) eq ERROR_NOT_AUTHENTICATED;
}
# Client access authorization
#
sub _request_with_auth {
my $self = shift;
my $request = shift or croak "No request";
my $has_token = $self->access_token;
my $response = $self->_request_with_token($request);
# token expired?
$response = $self->_request_with_token($request, "FRESH TOKEN")
if $response->code == HTTP_UNAUTHORIZED && $has_token;
return $response;
}
sub make_access_token_request {
die "make_access_token_request() not implemented";
}
sub _request_access_token {
my $self = shift;
my $request = shift or croak "No request";
$request->header(
Authorization => "Basic " . $self->_access_auth_string
);
my $response = $self->user_agent->request( $request );
# XXX check content type
return $self->process_json_response(
$response,
sub {
my ($data) = @_;
my ($token, $token_type) = $self->_extract_token_from_response($data);
$token or die "No access token\n";
$self->access_token($token);
$self->access_token_type($token_type || 'Bearer');
return $data;
},
sub {
my ($data) = @_;
die join "\n", ERROR_ACCESS_TOKEN, $self->_error_from_json($data) || $response->decoded_content;
}
);
}
sub _access_auth_string {
my $self = shift;
return MIME::Base64::encode( join(":", $self->client_id, $self->client_secret) );
}
sub _extract_token_from_response {
my $self = shift;
my $data = shift;
return ($data->{access_token}, $data->{token_type});
}
sub _request_with_token {
my $self = shift;
my $request = shift or croak "No request";
my $force_fresh = shift;
my $token = $force_fresh ? undef : $self->access_token;
unless ($token) {
my $request = $self->make_access_token_request;
$self->_request_access_token($request);
$token = $self->access_token;
}
die "No access token" unless $token;
my $token_type = $self->access_token_type;
$request->header( Authorization => "$token_type $token" );
return $self->user_agent->request( $request );
}
# Strictly speaking process_json_response() and process_json_error_response()
# should go to ::JSON. However, JSON is used for authentication services even for
# APIs that are XML, so need to be available
sub process_json_response {
my $self = shift;
my $response = shift or croak "No response";
my $success_callback = shift;
my $error_callback = shift;
unless ($response->is_success) {
return $self->process_json_error_response($response, $error_callback);
}
my $content_type = $response->header('Content-Type');
die "Invalid Content-Type\n".$response->as_string
unless $content_type && $content_type =~ m!application/json!;
my $content = $response->decoded_content
or die $self->invalid_response_exception_string($response);
local $@;
my $data = $content ? eval { from_json( $content ) } : {};
die "$@\nResponse:\n".$response->as_string if $@;
return $data unless $success_callback;
my $res = eval {
$success_callback->($data);
};
die "$@\nResponse:\n$content" if $@;
return $res;
}
sub process_json_error_response {
my $self = shift;
my $response = shift or croak "No response";
my $error_callback = shift;
my $content_type = $response->header('Content-Type');
if ($content_type && $content_type =~ m!application/json!) {
my $content = $response->decoded_content
or die $self->invalid_response_exception_string($response);
my $data = eval { from_json( $content ) };
die $content || $self->invalid_response_exception_string($response) if $@;
if ($error_callback) {
return $error_callback->($data);
}
die $self->_error_from_json($data) || "Invalid response:\n$content";
}
die $self->invalid_response_exception_string($response);
}
sub _error_from_json {};
# wrapper around error response handlers to include some debugging if the debug flag is set
sub _error_result {
my $self = shift;
my $process_sub = shift or croak "No process sub";
my $request = shift or croak "No HTTP request";
my $response = shift or croak "No HTTP response";
return $process_sub->() unless $DEBUG;
local $@;
my $ret = eval { $process_sub->() };
die join "\n", $@, "Request:", $request->as_string, "Response:", $response->as_string
if $@;
return $ret;
}
sub _result_xlate {
my $self = shift;
my $res = shift;
my $xlate_table = shift;
return {
map {
my $val = $res->{$_};
defined($val) ? ($xlate_table->{$_} => $val) : ()
} keys %$xlate_table
};
}
=head1 TODO
Federated search
=cut
1;
__END__
=head1 LICENSE
Copyright (C) Catalyst IT NZ Ltd
Copyright (C) Bywater Solutions
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHOR
Srdjan Janković E<lt>srdjan@catalyst.net.nzE<gt>
=cut
|