| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WebService::ILS; |
|
2
|
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
1979
|
use Modern::Perl; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
27
|
|
|
4
|
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = "0.16"; |
|
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::; |
|
16
|
|
|
|
|
|
|
my $ils = WebService::ILS::->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 |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
OverDrive Library API L |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=item B |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
OverDrive Circulation API L |
|
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 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 |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=item C |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=item C |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=item C |
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=item C |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=item C |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=item C |
|
89
|
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item C |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=item C |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=item C => user ratings metrics |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=item C => checkout metrics |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=item C => subject categories (tags) |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=item C => a hashref of facet => [values] |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=item C => book, e-book, video, audio etc |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=item C => an arrayref of available formats |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item C => a hashref of size => url |
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item C => for decryption purposes |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=item C => 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
|
|
|
5
|
|
666
|
use Carp; |
|
|
5
|
|
|
|
|
10
|
|
|
|
5
|
|
|
|
|
229
|
|
|
120
|
5
|
|
|
5
|
|
1441
|
use Hash::Merge; |
|
|
5
|
|
|
|
|
10041
|
|
|
|
5
|
|
|
|
|
211
|
|
|
121
|
5
|
|
|
5
|
|
1480
|
use Params::Check; |
|
|
5
|
|
|
|
|
15505
|
|
|
|
5
|
|
|
|
|
226
|
|
|
122
|
5
|
|
|
5
|
|
2195
|
use LWP::UserAgent; |
|
|
5
|
|
|
|
|
104462
|
|
|
|
5
|
|
|
|
|
204
|
|
|
123
|
5
|
|
|
5
|
|
54
|
use HTTP::Status qw(:constants); |
|
|
5
|
|
|
|
|
22
|
|
|
|
5
|
|
|
|
|
1857
|
|
|
124
|
5
|
|
|
5
|
|
1607
|
use MIME::Base64 qw(); |
|
|
5
|
|
|
|
|
2583
|
|
|
|
5
|
|
|
|
|
120
|
|
|
125
|
5
|
|
|
5
|
|
467
|
use JSON qw(from_json); |
|
|
5
|
|
|
|
|
5473
|
|
|
|
5
|
|
|
|
|
30
|
|
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
our $DEBUG; |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
my %CONSTRUCTOR_PARAMS_SPEC; |
|
130
|
|
|
|
|
|
|
sub _set_param_spec { |
|
131
|
14
|
|
|
14
|
|
33
|
my $class = shift; |
|
132
|
14
|
|
|
|
|
23
|
my $param_spec = shift; |
|
133
|
|
|
|
|
|
|
|
|
134
|
14
|
|
|
|
|
36
|
$CONSTRUCTOR_PARAMS_SPEC{$class} = $param_spec; |
|
135
|
|
|
|
|
|
|
} |
|
136
|
|
|
|
|
|
|
sub _get_param_spec { |
|
137
|
2
|
|
|
2
|
|
5
|
my $class = shift; |
|
138
|
2
|
50
|
|
|
|
7
|
if (my $ref = ref($class)) { |
|
139
|
0
|
|
|
|
|
0
|
$class = $ref; |
|
140
|
|
|
|
|
|
|
} |
|
141
|
|
|
|
|
|
|
|
|
142
|
2
|
|
|
|
|
6
|
my $p_s = $CONSTRUCTOR_PARAMS_SPEC{$class}; |
|
143
|
2
|
50
|
|
|
|
14
|
return $p_s if $class eq __PACKAGE__; |
|
144
|
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
0
|
(my $superclass = $class) =~ s/::\w+$//o; |
|
146
|
0
|
|
0
|
|
|
0
|
return Hash::Merge::merge($p_s || {}, $superclass->_get_param_spec); |
|
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 (vendor) identifier |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item C => secret key (password) |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=item C => 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 => LWP::UserAgent or a derivative; |
|
171
|
|
|
|
|
|
|
usually not needed, one is created for you. |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item C => LWP::UserAgent constructor params |
|
174
|
|
|
|
|
|
|
so you don't need to create user agent yourself |
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item C => as returned from the provider authentication system |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=item C => 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
|
|
|
|
|
21
|
use Class::Tiny qw( |
|
189
|
|
|
|
|
|
|
user_agent |
|
190
|
|
|
|
|
|
|
client_id client_secret library_id |
|
191
|
|
|
|
|
|
|
access_token access_token_type |
|
192
|
5
|
|
|
5
|
|
2442
|
); |
|
|
5
|
|
|
|
|
6734
|
|
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
__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
|
|
|
|
|
|
|
sub BUILDARGS { |
|
205
|
2
|
|
|
2
|
0
|
973
|
my $self = shift; |
|
206
|
2
|
|
50
|
|
|
10
|
my $params = shift || {}; |
|
207
|
2
|
100
|
|
|
|
9
|
if (!ref( $params )) { |
|
208
|
1
|
|
|
|
|
5
|
$params = {$params, @_}; |
|
209
|
|
|
|
|
|
|
} |
|
210
|
|
|
|
|
|
|
|
|
211
|
2
|
|
|
|
|
5
|
local $Params::Check::WARNINGS_FATAL = 1; |
|
212
|
2
|
50
|
|
|
|
7
|
$params = Params::Check::check($self->_get_param_spec, $params) |
|
213
|
|
|
|
|
|
|
or croak "Invalid parameters: ".Params::Check::last_error(); |
|
214
|
2
|
|
|
|
|
428
|
return $params; |
|
215
|
|
|
|
|
|
|
} |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
sub BUILD { |
|
218
|
2
|
|
|
2
|
0
|
51
|
my $self = shift; |
|
219
|
2
|
|
|
|
|
3
|
my $params = shift; |
|
220
|
|
|
|
|
|
|
|
|
221
|
2
|
|
50
|
|
|
11
|
my $ua_params = delete $params->{user_agent_params} || {}; |
|
222
|
2
|
50
|
|
|
|
45
|
$self->user_agent( LWP::UserAgent->new(%$ua_params) ) unless $self->user_agent; |
|
223
|
2
|
|
|
|
|
2361
|
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 (search) string |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=item C => number of items per results page |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
=item C => wanted page number |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=item C => 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 => date title was published |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=item C => date title became available for users |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=item C => 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 => an array of item records |
|
273
|
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=item C => number of items per results page |
|
275
|
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
=item C => results page number |
|
277
|
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
=item C => total number of pages |
|
279
|
|
|
|
|
|
|
|
|
280
|
|
|
|
|
|
|
=item C => 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 |
|
295
|
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
=item C => boolean |
|
297
|
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=item C => number of copies available |
|
299
|
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
=item C => number of copies owned |
|
301
|
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
=item C => 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 |
|
315
|
|
|
|
|
|
|
|
|
316
|
|
|
|
|
|
|
=cut |
|
317
|
|
|
|
|
|
|
|
|
318
|
|
|
|
|
|
|
sub search { |
|
319
|
0
|
|
|
0
|
1
|
|
die "search() not implemented"; |
|
320
|
|
|
|
|
|
|
} |
|
321
|
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
# relevancy availability available_date title author popularity rating price publisher publication_date |
|
323
|
|
|
|
|
|
|
sub _parse_sort_string { |
|
324
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
325
|
0
|
0
|
|
|
|
|
my $sort = shift or croak "No sort options"; |
|
326
|
0
|
|
0
|
|
|
|
my $xlate_table = shift || {}; |
|
327
|
0
|
|
|
|
|
|
my $camelise = shift; |
|
328
|
|
|
|
|
|
|
|
|
329
|
0
|
0
|
|
|
|
|
$sort = [split /\s*,\s*/, $sort] unless ref $sort; |
|
330
|
|
|
|
|
|
|
|
|
331
|
0
|
|
|
|
|
|
foreach (@$sort) { |
|
332
|
0
|
|
|
|
|
|
my ($s,$d) = split ':'; |
|
333
|
0
|
0
|
|
|
|
|
if (exists $xlate_table->{$s}) { |
|
334
|
0
|
0
|
|
|
|
|
next unless $xlate_table->{$s}; |
|
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
|
0
|
|
|
|
|
$_ = join '', map ucfirst, split /(?<=[A-Za-z])_(?=[A-Za-z])|\b/ if $camelise; |
|
342
|
0
|
0
|
|
|
|
|
$_ = "$_:$d" if $d; |
|
343
|
|
|
|
|
|
|
} |
|
344
|
|
|
|
|
|
|
|
|
345
|
0
|
|
|
|
|
|
return $sort; |
|
346
|
|
|
|
|
|
|
} |
|
347
|
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
sub item_metadata { |
|
349
|
0
|
|
|
0
|
1
|
|
die "item_metadata() not implemented"; |
|
350
|
|
|
|
|
|
|
} |
|
351
|
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
sub item_availability { |
|
353
|
0
|
|
|
0
|
1
|
|
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 |
|
440
|
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
=item C => boolean |
|
442
|
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
=item C => number of copies available |
|
444
|
|
|
|
|
|
|
|
|
445
|
|
|
|
|
|
|
=item C => number of checkouts allowed |
|
446
|
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
=item C => 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 => number of items on hold |
|
458
|
|
|
|
|
|
|
|
|
459
|
|
|
|
|
|
|
=item C => 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 => hold timestamp, with or without timezone |
|
469
|
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
=item C => 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 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 => number of items on hold |
|
495
|
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
=item C => 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 timestamp, with or without timezone |
|
506
|
|
|
|
|
|
|
|
|
507
|
|
|
|
|
|
|
=item C => date (time) checkout expires |
|
508
|
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
=item C => download/stream url |
|
510
|
|
|
|
|
|
|
|
|
511
|
|
|
|
|
|
|
=item C => 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 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 |
|
553
|
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
=item C |
|
555
|
|
|
|
|
|
|
|
|
556
|
|
|
|
|
|
|
=back |
|
557
|
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
=cut |
|
559
|
|
|
|
|
|
|
|
|
560
|
5
|
|
|
5
|
|
5792
|
use constant ERROR_ACCESS_TOKEN => "Error: Authorization Failed"; |
|
|
5
|
|
|
|
|
11
|
|
|
|
5
|
|
|
|
|
312
|
|
|
561
|
5
|
|
|
5
|
|
31
|
use constant ERROR_NOT_AUTHENTICATED => "Error: User Not Authenticated"; |
|
|
5
|
|
|
|
|
12
|
|
|
|
5
|
|
|
|
|
6570
|
|
|
562
|
|
|
|
|
|
|
|
|
563
|
|
|
|
|
|
|
sub invalid_response_exception_string { |
|
564
|
0
|
|
|
0
|
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
|
|
|
|
|
|
|
sub check_response { |
|
575
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
|
576
|
0
|
|
|
|
|
|
my $response = shift; |
|
577
|
|
|
|
|
|
|
|
|
578
|
0
|
0
|
|
|
|
|
die $self->invalid_response_exception_string($response) unless $response->is_success; |
|
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
|
|
|
|
|
|
|
sub error_message { |
|
605
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
606
|
0
|
0
|
|
|
|
|
my $die_string = shift or return; |
|
607
|
0
|
|
|
|
|
|
$die_string =~ m/(.*?)\n/o; |
|
608
|
0
|
|
0
|
|
|
|
(my $msg = $1 || $die_string) =~ s! at /.* line \d+\.$!!; |
|
609
|
0
|
|
|
|
|
|
return $msg; |
|
610
|
|
|
|
|
|
|
} |
|
611
|
|
|
|
|
|
|
|
|
612
|
|
|
|
|
|
|
sub is_access_token_error { |
|
613
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
614
|
0
|
0
|
|
|
|
|
my $die_string = shift or croak "No error message"; |
|
615
|
0
|
|
|
|
|
|
return $self->error_message($die_string) eq ERROR_ACCESS_TOKEN; |
|
616
|
|
|
|
|
|
|
} |
|
617
|
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
sub is_not_authenticated_error { |
|
619
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
620
|
0
|
0
|
|
|
|
|
my $die_string = shift or croak "No error message"; |
|
621
|
0
|
|
|
|
|
|
return $self->error_message($die_string) eq ERROR_NOT_AUTHENTICATED; |
|
622
|
|
|
|
|
|
|
} |
|
623
|
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
# Client access authorization |
|
625
|
|
|
|
|
|
|
# |
|
626
|
|
|
|
|
|
|
sub _request_with_auth { |
|
627
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
628
|
0
|
0
|
|
|
|
|
my $request = shift or croak "No request"; |
|
629
|
|
|
|
|
|
|
|
|
630
|
0
|
|
|
|
|
|
my $has_token = $self->access_token; |
|
631
|
0
|
|
|
|
|
|
my $response = $self->_request_with_token($request); |
|
632
|
|
|
|
|
|
|
# token expired? |
|
633
|
0
|
0
|
0
|
|
|
|
$response = $self->_request_with_token($request, "FRESH TOKEN") |
|
634
|
|
|
|
|
|
|
if $response->code == HTTP_UNAUTHORIZED && $has_token; |
|
635
|
0
|
|
|
|
|
|
return $response; |
|
636
|
|
|
|
|
|
|
} |
|
637
|
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
sub make_access_token_request { |
|
639
|
0
|
|
|
0
|
0
|
|
die "make_access_token_request() not implemented"; |
|
640
|
|
|
|
|
|
|
} |
|
641
|
|
|
|
|
|
|
|
|
642
|
|
|
|
|
|
|
sub _request_access_token { |
|
643
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
644
|
0
|
0
|
|
|
|
|
my $request = shift or croak "No request"; |
|
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
|
|
|
|
|
|
|
sub { |
|
655
|
0
|
|
|
0
|
|
|
my ($data) = @_; |
|
656
|
|
|
|
|
|
|
|
|
657
|
0
|
|
|
|
|
|
my ($token, $token_type) = $self->_extract_token_from_response($data); |
|
658
|
0
|
0
|
|
|
|
|
$token or die "No access token\n"; |
|
659
|
0
|
|
|
|
|
|
$self->access_token($token); |
|
660
|
0
|
|
0
|
|
|
|
$self->access_token_type($token_type || 'Bearer'); |
|
661
|
0
|
|
|
|
|
|
return $data; |
|
662
|
|
|
|
|
|
|
}, |
|
663
|
|
|
|
|
|
|
sub { |
|
664
|
0
|
|
|
0
|
|
|
my ($data) = @_; |
|
665
|
|
|
|
|
|
|
|
|
666
|
0
|
|
0
|
|
|
|
die join "\n", ERROR_ACCESS_TOKEN, $self->_error_from_json($data) || $response->decoded_content; |
|
667
|
|
|
|
|
|
|
} |
|
668
|
0
|
|
|
|
|
|
); |
|
669
|
|
|
|
|
|
|
} |
|
670
|
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
sub _access_auth_string { |
|
672
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
673
|
0
|
|
|
|
|
|
return MIME::Base64::encode( join(":", $self->client_id, $self->client_secret) ); |
|
674
|
|
|
|
|
|
|
} |
|
675
|
|
|
|
|
|
|
|
|
676
|
|
|
|
|
|
|
sub _extract_token_from_response { |
|
677
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
678
|
0
|
|
|
|
|
|
my $data = shift; |
|
679
|
|
|
|
|
|
|
|
|
680
|
0
|
|
|
|
|
|
return ($data->{access_token}, $data->{token_type}); |
|
681
|
|
|
|
|
|
|
} |
|
682
|
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
sub _request_with_token { |
|
684
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
685
|
0
|
0
|
|
|
|
|
my $request = shift or croak "No request"; |
|
686
|
0
|
|
|
|
|
|
my $force_fresh = shift; |
|
687
|
|
|
|
|
|
|
|
|
688
|
0
|
0
|
|
|
|
|
my $token = $force_fresh ? undef : $self->access_token; |
|
689
|
0
|
0
|
|
|
|
|
unless ($token) { |
|
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
|
0
|
|
|
|
|
die "No access token" unless $token; |
|
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
|
|
|
|
|
|
|
sub process_json_response { |
|
705
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
|
706
|
0
|
0
|
|
|
|
|
my $response = shift or croak "No response"; |
|
707
|
0
|
|
|
|
|
|
my $success_callback = shift; |
|
708
|
0
|
|
|
|
|
|
my $error_callback = shift; |
|
709
|
|
|
|
|
|
|
|
|
710
|
0
|
0
|
|
|
|
|
unless ($response->is_success) { |
|
711
|
0
|
|
|
|
|
|
return $self->process_json_error_response($response, $error_callback); |
|
712
|
|
|
|
|
|
|
} |
|
713
|
|
|
|
|
|
|
|
|
714
|
0
|
|
|
|
|
|
my $content_type = $response->header('Content-Type'); |
|
715
|
0
|
0
|
0
|
|
|
|
die "Invalid Content-Type\n".$response->as_string |
|
716
|
|
|
|
|
|
|
unless $content_type && $content_type =~ m!application/json!; |
|
717
|
0
|
0
|
|
|
|
|
my $content = $response->decoded_content |
|
718
|
|
|
|
|
|
|
or die $self->invalid_response_exception_string($response); |
|
719
|
|
|
|
|
|
|
|
|
720
|
0
|
|
|
|
|
|
local $@; |
|
721
|
|
|
|
|
|
|
|
|
722
|
0
|
0
|
|
|
|
|
my $data = $content ? eval { from_json( $content ) } : {}; |
|
|
0
|
|
|
|
|
|
|
|
723
|
0
|
0
|
|
|
|
|
die "$@\nResponse:\n".$response->as_string if $@; |
|
724
|
|
|
|
|
|
|
|
|
725
|
0
|
0
|
|
|
|
|
return $data unless $success_callback; |
|
726
|
|
|
|
|
|
|
|
|
727
|
0
|
|
|
|
|
|
my $res = eval { |
|
728
|
0
|
|
|
|
|
|
$success_callback->($data); |
|
729
|
|
|
|
|
|
|
}; |
|
730
|
0
|
0
|
|
|
|
|
die "$@\nResponse:\n$content" if $@; |
|
731
|
0
|
|
|
|
|
|
return $res; |
|
732
|
|
|
|
|
|
|
} |
|
733
|
|
|
|
|
|
|
|
|
734
|
|
|
|
|
|
|
sub process_json_error_response { |
|
735
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
|
736
|
0
|
0
|
|
|
|
|
my $response = shift or croak "No response"; |
|
737
|
0
|
|
|
|
|
|
my $error_callback = shift; |
|
738
|
|
|
|
|
|
|
|
|
739
|
0
|
|
|
|
|
|
my $content_type = $response->header('Content-Type'); |
|
740
|
0
|
0
|
0
|
|
|
|
if ($content_type && $content_type =~ m!application/json!) { |
|
741
|
0
|
0
|
|
|
|
|
my $content = $response->decoded_content |
|
742
|
|
|
|
|
|
|
or die $self->invalid_response_exception_string($response); |
|
743
|
|
|
|
|
|
|
|
|
744
|
0
|
|
|
|
|
|
my $data = eval { from_json( $content ) }; |
|
|
0
|
|
|
|
|
|
|
|
745
|
0
|
0
|
0
|
|
|
|
die $content || $self->invalid_response_exception_string($response) if $@; |
|
746
|
|
|
|
|
|
|
|
|
747
|
0
|
0
|
|
|
|
|
if ($error_callback) { |
|
748
|
0
|
|
|
|
|
|
return $error_callback->($data); |
|
749
|
|
|
|
|
|
|
} |
|
750
|
|
|
|
|
|
|
|
|
751
|
0
|
|
0
|
|
|
|
die $self->_error_from_json($data) || "Invalid response:\n$content"; |
|
752
|
|
|
|
|
|
|
} |
|
753
|
0
|
|
|
|
|
|
die $self->invalid_response_exception_string($response); |
|
754
|
|
|
|
|
|
|
} |
|
755
|
|
|
|
|
|
|
|
|
756
|
|
|
|
0
|
|
|
sub _error_from_json {}; |
|
757
|
|
|
|
|
|
|
|
|
758
|
|
|
|
|
|
|
# wrapper around error response handlers to include some debugging if the debug flag is set |
|
759
|
|
|
|
|
|
|
sub _error_result { |
|
760
|
0
|
|
|
0
|
|
|
my $self = shift; |
|
761
|
0
|
0
|
|
|
|
|
my $process_sub = shift or croak "No process sub"; |
|
762
|
0
|
0
|
|
|
|
|
my $request = shift or croak "No HTTP request"; |
|
763
|
0
|
0
|
|
|
|
|
my $response = shift or croak "No HTTP response"; |
|
764
|
|
|
|
|
|
|
|
|
765
|
0
|
0
|
|
|
|
|
return $process_sub->() unless $DEBUG; |
|
766
|
|
|
|
|
|
|
|
|
767
|
0
|
|
|
|
|
|
local $@; |
|
768
|
0
|
|
|
|
|
|
my $ret = eval { $process_sub->() }; |
|
|
0
|
|
|
|
|
|
|
|
769
|
0
|
0
|
|
|
|
|
die join "\n", $@, "Request:", $request->as_string, "Response:", $response->as_string |
|
770
|
|
|
|
|
|
|
if $@; |
|
771
|
0
|
|
|
|
|
|
return $ret; |
|
772
|
|
|
|
|
|
|
} |
|
773
|
|
|
|
|
|
|
|
|
774
|
|
|
|
|
|
|
sub _result_xlate { |
|
775
|
0
|
|
|
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->{$_}; |
|
|
0
|
|
|
|
|
|
|
|
782
|
0
|
0
|
|
|
|
|
defined($val) ? ($xlate_table->{$_} => $val) : () |
|
783
|
|
|
|
|
|
|
} keys %$xlate_table |
|
784
|
|
|
|
|
|
|
}; |
|
785
|
|
|
|
|
|
|
} |
|
786
|
|
|
|
|
|
|
|
|
787
|
|
|
|
|
|
|
|
|
788
|
|
|
|
|
|
|
=head1 TODO |
|
789
|
|
|
|
|
|
|
|
|
790
|
|
|
|
|
|
|
Federated search |
|
791
|
|
|
|
|
|
|
|
|
792
|
|
|
|
|
|
|
=cut |
|
793
|
|
|
|
|
|
|
|
|
794
|
|
|
|
|
|
|
1; |
|
795
|
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
__END__ |