]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OneClickdigital.pm
LP#1673870: Add basic ebook API title lookup
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / EbookAPI / OneClickdigital.pm
1 #!/usr/bin/perl
2
3 # Copyright (C) 2015 BC Libraries Cooperative
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 package OpenILS::Application::EbookAPI::OneClickdigital;
20
21 use strict;
22 use warnings;
23
24 use OpenILS::Application;
25 use OpenILS::Application::EbookAPI;
26 use base qw/OpenILS::Application::EbookAPI/;
27 use OpenSRF::AppSession;
28 use OpenSRF::EX qw(:try);
29 use OpenSRF::Utils::SettingsClient;
30 use OpenSRF::Utils::Logger qw($logger);
31 use OpenSRF::Utils::Cache;
32 use OpenILS::Application::AppUtils;
33 use Data::Dumper;
34
35 sub new {
36     my( $class, $args ) = @_;
37     $class = ref $class || $class;
38     return bless $args, $class;
39 }
40
41 sub ou {
42     my $self = shift;
43     return $self->{ou};
44 }
45
46 sub vendor {
47     my $self = shift;
48     return $self->{vendor};
49 }
50
51 sub session_id {
52     my $self = shift;
53     return $self->{session_id};
54 }
55
56 sub base_uri {
57     my $self = shift;
58     return $self->{base_uri};
59 }
60
61 sub library_id {
62     my $self = shift;
63     return $self->{library_id};
64 }
65
66 sub basic_token {
67     my $self = shift;
68     return $self->{basic_token};
69 }
70
71 sub patron_id {
72     my $self = shift;
73     return $self->{patron_id};
74 }
75
76 sub initialize {
77     my $self = shift;
78     my $ou = $self->{ou};
79
80     my $base_uri = 'https://api.oneclickdigital.com/v1';
81     $self->{base_uri} = OpenILS::Application::AppUtils->ou_ancestor_setting_value($ou, 'ebook_api.oneclickdigital.base_uri') || $base_uri;
82
83     my $library_id = OpenILS::Application::AppUtils->ou_ancestor_setting_value($ou, 'ebook_api.oneclickdigital.library_id');
84     if ($library_id) {
85         $self->{library_id} = $library_id;
86     } else {
87         $logger->error("EbookAPI: no OneClickdigital library ID found for org unit $ou");
88         return;
89     }
90
91     my $basic_token = OpenILS::Application::AppUtils->ou_ancestor_setting_value($ou, 'ebook_api.oneclickdigital.basic_token');
92     if ($basic_token) {
93         $self->{basic_token} = $basic_token;
94     } else {
95         $logger->error("EbookAPI: no OneClickdigital basic token found for org unit $ou");
96         return;
97     }
98
99     return $self;
100
101 }
102
103 # OneClickdigital API does not require separate client auth;
104 # we just need to include our basic auth token in requests
105 sub do_client_auth {
106     my $self = shift;
107     return;
108 }
109
110 # retrieve OneClickdigital patron ID (if any) based on patron barcode
111 # GET http://api.oneclickdigital.us/v1/rpc/libraries/{libraryID}/patrons/{barcode}
112 sub do_patron_auth {
113     my ($self, $barcode) = @_;
114     my $base_uri = $self->{base_uri};
115     my $library_id = $self->{library_id};
116     my $session_id = $self->{session_id};
117     my $req = {
118         method => 'GET',
119         uri    => "$base_uri/rpc/libraries/$library_id/patrons/$barcode"
120     };
121     my $res = $self->request($req, $session_id);
122     # TODO distinguish between unregistered patrons and patron auth failure
123     if (defined ($res) && $res->{content}->{patronId}) {
124         return $res->{content}->{patronId};
125     }
126     $logger->error("EbookAPI: no OneClickdigital patron ID found for barcode $barcode");
127     return;
128 }
129
130 # get basic metadata for an item (title, author, cover image if any)
131 # GET http://api.oneclickdigital.us/v1/libraries/{libraryId}/titles/{isbn}
132 sub get_title_info {
133     my ($self, $isbn) = @_;
134     my $base_uri = $self->{base_uri};
135     my $library_id = $self->{library_id};
136     my $session_id = $self->{session_id};
137     my $req = {
138         method => 'GET',
139         uri    => "$base_uri/libraries/$library_id/titles/$isbn"
140     };
141     my $res = $self->request($req, $session_id);
142     if (defined ($res)) {
143         return {
144             title  => $res->{content}->{title},
145             author => $res->{content}->{authors}[0]{text}
146         };
147     } else {
148         $logger->error("EbookAPI: could not retrieve OneClickdigital title details for ISBN $isbn");
149         return;
150     }
151 }
152
153 # does this title have available "copies"? y/n
154 # GET http://api.oneclickdigital.us/v1/libraries/{libraryID}/media/{isbn}/availability
155 sub do_availability_lookup {
156     my ($self, $isbn) = @_;
157     my $base_uri = $self->{base_uri};
158     my $library_id = $self->{library_id};
159     my $session_id = $self->{session_id};
160     my $req = {
161         method => 'GET',
162         uri    => "$base_uri/libraries/$library_id/media/$isbn/availability"
163     };
164     my $res = $self->request($req, $session_id);
165     if (defined ($res)) {
166         $logger->info("EbookAPI: received availability response for ISBN $isbn: " . Dumper $res);
167         return $res->{content}->{availability};
168     } else {
169         $logger->error("EbookAPI: could not retrieve OneClickdigital availability for ISBN $isbn");
170         return;
171     }
172 }
173
174 # OneClickdigital API does not support detailed holdings lookup,
175 # so we return basic availability information.
176 sub do_holdings_lookup {
177     my ($self, $isbn) = @_;
178     my $avail = $self->do_availability_lookup($isbn);
179     return { available => $avail };
180 }
181
182 # checkout an item to a patron
183 # item is identified by ISBN, patron ID is their barcode
184 # POST //api.{domain}/v1/libraries/{libraryId}/patrons/{patronId}/checkouts/{isbn}
185 sub checkout {
186     my ($self, $isbn, $patron_id) = @_;
187     my $base_uri = $self->{base_uri};
188     my $library_id = $self->{library_id};
189     my $session_id = $self->{session_id};
190     my $req = {
191         method => 'POST',
192         uri    => "$base_uri/libraries/$library_id/patrons/$patron_id/checkouts/$isbn"
193     };
194     my $res = $self->request($req, $session_id);
195
196     # TODO: more sophisticated response handling
197     # HTTP 200 response indicates success, HTTP 409 indicates checkout limit reached
198     if (defined ($res)) {
199         if ($res->{is_success}) {
200             return {
201                 xact_id => $res->{content}->{transactionId},
202                 due_date => $res->{content}->{expiration}
203             };
204         } else {
205             $logger->error("EbookAPI: checkout failed for OneClickdigital title $isbn");
206             return { error_msg => $res->{content} };
207         }
208     } else {
209         $logger->error("EbookAPI: no response received from OneClickdigital server");
210         return;
211     }
212 }
213
214 # renew a checked-out item
215 # item id = ISBN, patron id = barcode
216 # PUT //api.{domain}/v1/libraries/{libraryId}/patrons/{patronId}/checkouts/{isbn}
217 sub renew {
218     my ($self, $isbn, $patron_id) = @_;
219     my $base_uri = $self->{base_uri};
220     my $library_id = $self->{library_id};
221     my $session_id = $self->{session_id};
222     my $req = {
223         method => 'PUT',
224         uri    => "$base_uri/libraries/$library_id/patrons/$patron_id/checkouts/$isbn"
225     };
226     my $res = $self->request($req, $session_id);
227
228     # TODO: more sophisticated response handling
229     # HTTP 200 response indicates success
230     if (defined ($res)) {
231         if ($res->{is_success}) {
232             return {
233                 xact_id => $res->{content}->{transactionId},
234                 due_date => $res->{content}->{expiration}
235             };
236         } else {
237             $logger->error("EbookAPI: renewal failed for OneClickdigital title $isbn");
238             return { error_msg => $res->{content} };
239         }
240     } else {
241         $logger->error("EbookAPI: no response received from OneClickdigital server");
242         return;
243     }
244 }
245
246 # checkin a checked-out item
247 # item id = ISBN, patron id = barcode
248 # XXX API docs indicate that a bearer token is required!
249 # DELETE //api.{domain}/v1/libraries/{libraryId}/patrons/{patronId}/checkouts/{isbn}
250 sub checkin {
251 }
252
253 sub place_hold {
254 }
255
256 sub cancel_hold {
257 }
258
259 # GET //api.{domain}/v1/libraries/{libraryId}/patrons/{patronId}/checkouts/all
260 sub get_patron_checkouts {
261     my ($self, $patron_id) = @_;
262     my $base_uri = $self->{base_uri};
263     my $library_id = $self->{library_id};
264     my $session_id = $self->{session_id};
265     my $req = {
266         method => 'GET',
267         uri    => "$base_uri/libraries/$library_id/patrons/$patron_id/checkouts/all"
268     };
269     my $res = $self->request($req, $session_id);
270
271     my $checkouts = [];
272     if (defined ($res)) {
273         $logger->info("EbookAPI: received response for OneClickdigital checkouts: " . Dumper $res);
274         foreach my $checkout (@{$res->{content}}) {
275             push @$checkouts, {
276                 xact_id => $checkout->{transactionID},
277                 title_id => $checkout->{isbn},
278                 due_date => $checkout->{expiration},
279                 download_url => $checkout->{downloadURL},
280                 title => $checkout->{title},
281                 author => $checkout->{authors}
282             };
283         };
284         $logger->info("EbookAPI: retrieved " . scalar(@$checkouts) . " OneClickdigital checkouts for patron $patron_id");
285         $self->{checkouts} = $checkouts;
286         return $self->{checkouts};
287     } else {
288         $logger->error("EbookAPI: failed to retrieve OneClickdigital checkouts for patron $patron_id");
289         return;
290     }
291 }
292
293 # GET //api.{domain}/v1/libraries/{libraryId}/patrons/{patronId}/holds/all
294 sub get_patron_holds {
295     my ($self, $patron_id) = @_;
296     my $base_uri = $self->{base_uri};
297     my $library_id = $self->{library_id};
298     my $session_id = $self->{session_id};
299     my $req = {
300         method => 'GET',
301         uri    => "$base_uri/libraries/$library_id/patrons/$patron_id/holds/all"
302     };
303     my $res = $self->request($req, $session_id);
304
305     my $holds = [];
306     if (defined ($res)) {
307         $logger->info("EbookAPI: received response for OneClickdigital holds: " . Dumper $res);
308         foreach my $hold (@{$res->{content}}) {
309             push @$holds, {
310                 xact_id => $hold->{transactionID},
311                 title_id => $hold->{isbn},
312                 expire_date => $hold->{expiration},
313                 title => $hold->{title},
314                 author => $hold->{authors},
315                 # XXX queue position/size and pending vs ready info not available via API
316                 queue_position => '-',
317                 queue_size => '-',
318                 is_ready => 0
319             };
320         };
321         $logger->info("EbookAPI: retrieved " . scalar(@$holds) . " OneClickdigital holds for patron $patron_id");
322         $self->{holds} = $holds;
323         return $self->{holds};
324     } else {
325         $logger->error("EbookAPI: failed to retrieve OneClickdigital holds for patron $patron_id");
326         return;
327     }
328 }
329
330 1;