From d163c5d5306c451051e9e241d25dd6a10b664c54 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 --- Koha/ExternalContent/OverDrive.pm | 24 +++++++++++++++++++ koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js | 10 +++++++- opac/svc/overdrive | 4 +--- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Koha/ExternalContent/OverDrive.pm b/Koha/ExternalContent/OverDrive.pm index 6247dfee6e..6c6ccf1ae8 100644 --- a/Koha/ExternalContent/OverDrive.pm +++ b/Koha/ExternalContent/OverDrive.pm @@ -23,6 +23,7 @@ use Carp qw( croak ); 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 f71fd13fae..9a9bef3e90 100644 --- a/koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js +++ b/koha-tmpl/opac-tmpl/bootstrap/js/overdrive.js @@ -290,7 +290,7 @@ KOHA.OverDriveCirculation = new function() { .text( __( "Checked out until: " ) + " " + expires.toLocaleString()) .appendTo(el); $(el).append(" "); - +/* if (item.format) { var download = $('').appendTo(el); decorate_button(download, __("Download") + " " + item.format); @@ -325,6 +325,14 @@ KOHA.OverDriveCirculation = new function() { } if (item.format) return item; +*/ + var access = $('').appendTo(el); + decorate_button(access, __("Get item") ); + svc_ajax('get', {action: "download-url", id: id}, function(data) { + console.log(data); + access.attr("href", data.action.redirect); + }); + $(el).append(" "); $(el).append( ajax_button( __("Check in"), function() { if( confirm( __("Are you sure you want to return this item?") ) ) { diff --git a/opac/svc/overdrive b/opac/svc/overdrive index 8945fc386d..3266e5765d 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.20.1