Bug 19502: Retrieve index.max_result_window from ES
[koha.git] / opac / svc / overdrive
blobcf7df9dd637bff05e440a1c27d6119069190c81f
1 #!/usr/bin/perl
3 # script to action OverDrive API calls
5 # Copyright 2015 Catalyst IT
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21 use Modern::Perl;
22 use CGI qw ( -utf8 );
23 use JSON qw(encode_json);
24 use C4::Auth qw(checkauth);
25 use C4::Output;
26 use Koha::Logger;
27 use Koha::ExternalContent::OverDrive;
29 my $logger = Koha::Logger->get({ interface => 'opac' });
30 our $cgi = new CGI;
31 my $page_url = $cgi->referer();
33 my ( $user, $cookie, $sessionID, $flags ) = checkauth( $cgi, 1, {}, 'opac' );
34 $user && $sessionID or response_bad_request("User not logged in");
36 my $action = $cgi->param('action') or response_bad_request("No 'action' specified");
38 my $od = Koha::ExternalContent::OverDrive->new({ koha_session_id => $sessionID });
39 my %data = (
40 is_logged_in => JSON::false,
42 local $@;
43 eval {
45 $action eq 'login' && do {
46 $data{login_url} = $od->auth_url($page_url);
47 last;
50 if ($od->is_logged_in) {
51 $data{is_logged_in} = JSON::true;
53 $action eq 'logout' && do {
54 $od->forget();
55 $data{login_url} = $od->auth_url($page_url);
56 $data{is_logged_in} = JSON::false;
57 last;
60 $action eq 'account' && do {
61 $data{account} = $od->patron;
62 $data{checkouts} = $od->checkouts;
63 $data{holds} = $od->holds;
64 last;
67 $action eq 'checkout' && do {
68 my $id = $cgi->param('id')
69 or response_bad_request("No 'id' specified");
70 my $format = $cgi->param('format');
71 $data{action} = $od->checkout($id, $format);
72 $data{checkouts} = $od->checkouts;
73 $data{holds} = $od->holds;
74 last;
77 $action eq 'checkout-format' && do {
78 my $id = $cgi->param('id')
79 or response_bad_request("No 'id' specified");
80 my $format = $cgi->param('format')
81 or response_bad_request("No 'format' specified");
82 $data{action} = $od->lock_format($id, $format);
83 $data{checkouts} = $od->checkouts;
84 last;
87 $action eq 'download-url' && do {
88 my $id = $cgi->param('id')
89 or response_bad_request("No 'id' specified");
90 my $format = $cgi->param('format')
91 or response_bad_request("No 'format' specified");
92 $data{action} = $od->checkout_download_url($id, $format, $page_url, $page_url);
93 last;
96 $action eq 'return' && do {
97 my $id = $cgi->param('id')
98 or response_bad_request("No 'id' specified");
99 local $@;
100 $data{action} = eval { $od->return($id) };
101 $data{action} = $@ if $@;
102 $data{checkouts} = $od->checkouts;
103 last;
106 $action eq 'place-hold' && do {
107 my $id = $cgi->param('id')
108 or response_bad_request("No 'id' specified");
109 $data{action} = $od->place_hold($id);
110 $data{holds} = $od->holds;
111 last;
114 $action eq 'remove-hold' && do {
115 my $id = $cgi->param('id')
116 or response_bad_request("No 'id' specified");
117 local $@;
118 $data{action} = eval { $od->remove_hold($id) };
119 $data{action} = $@ if $@;
120 $data{holds} = $od->holds;
121 last;
124 response_bad_request("Invalid 'action': $action");
128 if ($@) {
129 if ($od->is_not_authenticated_error("$@")) {
130 $logger->debug("OverDrive session timeout");
131 $data{is_logged_in} = JSON::false;
132 } else {
133 $logger->error($@);
134 $data{error} = $od->error_message("$@");
138 response(\%data);
141 sub response_bad_request {
142 my ($error) = @_;
143 response({error => $error}, "400 $error");
145 sub response {
146 my ($data, $status_line) = @_;
147 $status_line ||= "200 OK";
148 output_with_http_headers $cgi, undef, encode_json($data), 'json', $status_line;
149 exit;