From 554d6ff354435037b189e1e4c2c614166fa99ffd Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Mon, 13 Dec 2021 13:08:28 +0000 Subject: [PATCH] Bug 29686: Adapt Koha to use new fulfillment API for OverDrive This patch modifies the checkout_download_url routine in WebSerivce::ILS::OverDrive::Patron We now directly hit the fulfillment endpoint with redirects disabled and fetch the URL The overdrive.js is modified to use a single 'Get item' button for all checked out items and to refer to the fulfillment page To test: 1 - Enable all OverDrive system preferences 2 - Search on opac and confirm OD results returned 3 - Checkout an item 4 - Confirm you have the new 'Get item' button on 'OverDrive account' tab on opac-user.pl 5 - Confirm the 'Get item' button works NOTE: Most items will also show the 'Get item' button in results, however, magazines may not as each checkout has a unique 'reserve id' and the 'parent' id is not checked in our current code Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Owen Leonard Signed-off-by: Katrin Fischer Signed-off-by: Fridolin Somers Signed-off-by: Kyle M Hall (cherry picked from commit 09a51041cdd241a78abf88321daac14e54af6f01) Signed-off-by: Andrew Fuerste-Henry --- Koha/ExternalContent/OverDrive.pm | 24 +++++++++++ koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js | 40 +++---------------- opac/svc/overdrive | 4 +- 3 files changed, 31 insertions(+), 37 deletions(-) diff --git a/Koha/ExternalContent/OverDrive.pm b/Koha/ExternalContent/OverDrive.pm index ad36c18ce8..7f39f41d15 100644 --- a/Koha/ExternalContent/OverDrive.pm +++ b/Koha/ExternalContent/OverDrive.pm @@ -23,6 +23,7 @@ use Carp; use base qw(Koha::ExternalContent); use WebService::ILS::OverDrive::Patron; use C4::Context; +use LWP::UserAgent; =head1 NAME @@ -208,6 +209,29 @@ sub set_token_in_koha_session { ); } +=head2 checkout_download_url($item_id) + + Input: id of the item to download + + Returns: Fulfillment URL for reidrection + +=cut + +sub checkout_download_url { + my $self = shift; + my $item_id = shift or croak "Item ID not specified"; + + my $ua = LWP::UserAgent->new; + $ua->max_redirect(0); + my $response = $ua->get( + "https://patron.api.overdrive.com/v1/patrons/me/checkouts/".$item_id."/formats/downloadredirect", + 'Authorization' => "Bearer ".$self->client->access_token, + ); + + my $redirect = { redirect => $response->{_headers}->{location} }; + return $redirect; +} + =head1 OTHER METHODS =head2 is_logged_in() diff --git a/koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js b/koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js index 6f586e069f..ccc489dc39 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js @@ -283,40 +283,12 @@ KOHA.OverDriveCirculation = new function() { .appendTo(el); $(el).append(" "); - if (item.format) { - var download = $('').appendTo(el); - decorate_button(download, MSG_DOWNLOAD + " " + item.format); - svc_ajax('get', {action: "download-url", id: id, format: item.format}, function(data) { - download.attr("href", data.action); - }); - $(el).append(" "); - } - - if (item.formats) { - var lockable_formats = []; - for (var f in item.formats) { - if (f == item.format) continue; - - if (item.formats[f]) { - var access = $('').appendTo(el); - decorate_button(access, MSG_OVERDRIVE_ACCESS_ONLINE + " " + f); - svc_ajax('get', {action: "download-url", id: id, format: f}, function(data) { - access.attr("href", data.action); - }); - $(el).append(" "); - } - else { - lockable_formats.push(f); - } - } - if (lockable_formats.length > 0 && checkout_popup) { - $(el).append( ajax_button( MSG_OVERDRIVE_DOWNLOAD_AS, function() { - checkout_format(el, id, lockable_formats, copies_available); - }) ).append(" "); - } - } - - if (item.format) return item; + var access = $('').appendTo(el); + decorate_button(access, __("Get item") ); + svc_ajax('get', {action: "download-url", id: id}, function(data) { + access.attr("href", data.action.redirect); + }); + $(el).append(" "); $(el).append( ajax_button( MSG_CHECK_IN, function() { if( confirm( MSG_CHECK_IN_CONFIRM ) ) { diff --git a/opac/svc/overdrive b/opac/svc/overdrive index 89d0865dd8..8e9c1be9ae 100755 --- a/opac/svc/overdrive +++ b/opac/svc/overdrive @@ -101,9 +101,7 @@ eval { $action eq 'download-url' && do { my $id = $cgi->param('id') or response_bad_request("No 'id' specified"); - my $format = $cgi->param('format') - or response_bad_request("No 'format' specified"); - $data{action} = $od->checkout_download_url($id, $format, $page_url, $page_url); + $data{action} = $od->checkout_download_url($id); last; }; -- 2.39.5