]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OneClickdigital.pm
LP#1541559: org setting for OneClickdigital API base URI
[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 # does this title have available "copies"? y/n
131 # GET http://api.oneclickdigital.us/v1/libraries/{libraryID}/media/{isbn}/availability
132 sub do_availability_lookup {
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/media/$isbn/availability"
140     };
141     my $res = $self->request($req, $session_id);
142     if (defined ($res)) {
143         $logger->info("EbookAPI: received availability response for ISBN $isbn: " . Dumper $res);
144         return $res->{content}->{availability};
145     } else {
146         $logger->error("EbookAPI: could not retrieve OneClickdigital availability for ISBN $isbn");
147         return;
148     }
149 }
150
151 # OneClickdigital API does not support detailed holdings lookup,
152 # so we return basic availability information.
153 sub do_holdings_lookup {
154     my ($self, $isbn) = @_;
155     my $avail = $self->do_availability_lookup($isbn);
156     return { available => $avail };
157 }
158
159 # checkout an item to a patron
160 # item is identified by ISBN, patron ID is their barcode
161 # POST //api.{domain}/v1/libraries/{libraryId}/patrons/{patronId}/checkouts/{isbn}
162 sub checkout {
163     my ($self, $isbn, $patron_id) = @_;
164     my $base_uri = $self->{base_uri};
165     my $library_id = $self->{library_id};
166     my $session_id = $self->{session_id};
167     my $req = {
168         method => 'POST',
169         uri    => "$base_uri/libraries/$library_id/patrons/$patron_id/checkouts/$isbn"
170     };
171     my $res = $self->request($req, $session_id);
172
173     # TODO: more sophisticated response handling
174     # HTTP 200 response indicates success, HTTP 409 indicates checkout limit reached
175     if (defined ($res)) {
176         if ($res->{is_success}) {
177             return {
178                 xact_id => $res->{content}->{transactionId},
179                 due_date => $res->{content}->{expiration}
180             };
181         } else {
182             $logger->error("EbookAPI: checkout failed for OneClickdigital title $isbn");
183             return { error_msg => $res->{content} };
184         }
185     } else {
186         $logger->error("EbookAPI: no response received from OneClickdigital server");
187         return;
188     }
189 }
190
191 # renew a checked-out item
192 # item id = ISBN, patron id = barcode
193 # PUT //api.{domain}/v1/libraries/{libraryId}/patrons/{patronId}/checkouts/{isbn}
194 sub renew {
195     my ($self, $isbn, $patron_id) = @_;
196     my $base_uri = $self->{base_uri};
197     my $library_id = $self->{library_id};
198     my $session_id = $self->{session_id};
199     my $req = {
200         method => 'PUT',
201         uri    => "$base_uri/libraries/$library_id/patrons/$patron_id/checkouts/$isbn"
202     };
203     my $res = $self->request($req, $session_id);
204
205     # TODO: more sophisticated response handling
206     # HTTP 200 response indicates success
207     if (defined ($res)) {
208         if ($res->{is_success}) {
209             return {
210                 xact_id => $res->{content}->{transactionId},
211                 due_date => $res->{content}->{expiration}
212             };
213         } else {
214             $logger->error("EbookAPI: renewal failed for OneClickdigital title $isbn");
215             return { error_msg => $res->{content} };
216         }
217     } else {
218         $logger->error("EbookAPI: no response received from OneClickdigital server");
219         return;
220     }
221 }
222
223 # checkin a checked-out item
224 # item id = ISBN, patron id = barcode
225 # XXX API docs indicate that a bearer token is required!
226 # DELETE //api.{domain}/v1/libraries/{libraryId}/patrons/{patronId}/checkouts/{isbn}
227 sub checkin {
228 }
229
230 sub place_hold {
231 }
232
233 sub cancel_hold {
234 }
235
236 # GET //api.{domain}/v1/libraries/{libraryId}/patrons/{patronId}/checkouts/all
237 sub get_patron_checkouts {
238     my ($self, $patron_id) = @_;
239     my $base_uri = $self->{base_uri};
240     my $library_id = $self->{library_id};
241     my $session_id = $self->{session_id};
242     my $req = {
243         method => 'GET',
244         uri    => "$base_uri/libraries/$library_id/patrons/$patron_id/checkouts/all"
245     };
246     my $res = $self->request($req, $session_id);
247
248     my $checkouts = [];
249     if (defined ($res)) {
250         $logger->info("EbookAPI: received response for OneClickdigital checkouts: " . Dumper $res);
251         foreach my $checkout (@{$res->{content}}) {
252             push @$checkouts, {
253                 xact_id => $checkout->{transactionID},
254                 title_id => $checkout->{isbn},
255                 due_date => $checkout->{expiration},
256                 download_url => $checkout->{downloadURL},
257                 title => $checkout->{title},
258                 author => $checkout->{authors}
259             };
260         };
261         $logger->info("EbookAPI: retrieved " . scalar(@$checkouts) . " OneClickdigital checkouts for patron $patron_id");
262         $self->{checkouts} = $checkouts;
263         return $self->{checkouts};
264     } else {
265         $logger->error("EbookAPI: failed to retrieve OneClickdigital checkouts for patron $patron_id");
266         return;
267     }
268 }
269
270 # GET //api.{domain}/v1/libraries/{libraryId}/patrons/{patronId}/holds/all
271 sub get_patron_holds {
272     my ($self, $patron_id) = @_;
273     my $base_uri = $self->{base_uri};
274     my $library_id = $self->{library_id};
275     my $session_id = $self->{session_id};
276     my $req = {
277         method => 'GET',
278         uri    => "$base_uri/libraries/$library_id/patrons/$patron_id/holds/all"
279     };
280     my $res = $self->request($req, $session_id);
281
282     my $holds = [];
283     if (defined ($res)) {
284         $logger->info("EbookAPI: received response for OneClickdigital holds: " . Dumper $res);
285         foreach my $hold (@{$res->{content}}) {
286             push @$holds, {
287                 xact_id => $hold->{transactionID},
288                 title_id => $hold->{isbn},
289                 expire_date => $hold->{expiration},
290                 title => $hold->{title},
291                 author => $hold->{authors},
292                 # XXX queue position/size and pending vs ready info not available via API
293                 queue_position => '-',
294                 queue_size => '-',
295                 is_ready => 0
296             };
297         };
298         $logger->info("EbookAPI: retrieved " . scalar(@$holds) . " OneClickdigital holds for patron $patron_id");
299         $self->{holds} = $holds;
300         return $self->{holds};
301     } else {
302         $logger->error("EbookAPI: failed to retrieve OneClickdigital holds for patron $patron_id");
303         return;
304     }
305 }
306
307 1;