ILS.pm

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