From da17a15eb931f82a9b38f2d579c0fed1215a0962 Mon Sep 17 00:00:00 2001 From: Jeff Davis Date: Tue, 21 Mar 2017 15:32:48 -0700 Subject: [PATCH] LP#1673870: Checkout/renew/checkin methods for OverDrive API Signed-off-by: Jeff Davis Signed-off-by: Galen Charlton Signed-off-by: Bill Erickson --- .../lib/OpenILS/Application/EbookAPI/OverDrive.pm | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OverDrive.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OverDrive.pm index 97a301a9b2..613459459c 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OverDrive.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OverDrive.pm @@ -29,6 +29,7 @@ use OpenSRF::EX qw(:try); use OpenSRF::Utils::SettingsClient; use OpenSRF::Utils::Logger qw($logger); use OpenSRF::Utils::Cache; +use OpenSRF::Utils::JSON; use OpenILS::Application::AppUtils; use Data::Dumper; @@ -464,6 +465,96 @@ sub do_holdings_lookup { return $holdings; } +# POST https://patron.api.overdrive.com/v1/patrons/me/checkouts +# Authorization: Bearer {OAuth patron access token} +# Content-Type: application/json; charset=utf-8 +# +# Request content looks like this: +# { +# "fields": [ +# { +# "name": "reserveId", +# "value": "76C1B7D0-17F4-4C05-8397-C66C17411584" +# } +# ] +# } +# +# Response looks like this: +# { +# "reserveId": "76C1B7D0-17F4-4C05-8397-C66C17411584", +# "expires": "10/14/2013 10:56:00 AM", +# "isFormatLockedIn": false, +# "formats": [ +# { +# "reserveId": "76C1B7D0-17F4-4C05-8397-C66C17411584", +# "formatType": "ebook-overdrive", +# "linkTemplates": { +# "downloadLink": { +# "href": "https://patron.api.overdrive.com/v1/patrons/me/checkouts/76C1B7D0-17F4-4C05-8397-C66C17411584/formats/ebook-overdrive/downloadlink?errorpageurl={errorpageurl}&odreadauthurl={odreadauthurl}", +# ... +# }, +# ... +# }, +# ... +# } +# ], +# ... +# } +sub checkout { + my ($self, $title_id, $patron_token) = @_; + my $request_content = { + fields => [ + { + name => 'reserveId', + value => $title_id + } + ] + }; + my $req = { + method => 'POST', + uri => $self->{circulation_base_uri} . "/patrons/me/checkouts", + content => OpenSRF::Utils::JSON->perl2JSON($request_content) + }; + if (my $res = $self->handle_http_request($req, $self->{session_id})) { + if ($res->{content}->{expires}) { + return { due_date => $res->{content}->{expires} }; + } + $logger->error("EbookAPI: checkout failed for OverDrive title $title_id"); + return { error_msg => ( (defined $res->{content}) ? $res->{content} : 'Unknown checkout error' ) }; + } + $logger->error("EbookAPI: no response received from OverDrive server"); + return; +} + +# renew is not supported by OverDrive API +sub renew { + $logger->error("EbookAPI: OverDrive API does not support renewals"); + return { error_msg => "Title cannot be renewed." }; +} + +# NB: A title cannot be checked in once a format has been locked in. +# Successful checkin returns an HTTP 204 response with no content. +# DELETE https://patron.api.overdrive.com/v1/patrons/me/checkouts/08F7D7E6-423F-45A6-9A1E-5AE9122C82E7 +# Authorization: Bearer {OAuth patron access token} +# Host: patron.api.overdrive.com +sub checkin { + my ($self, $title_id, $patron_token) = @_; + my $req = { + method => 'DELETE', + uri => $self->{circulation_base_uri} . "/patrons/me/checkouts/$title_id" + }; + if (my $res = $self->handle_http_request($req, $self->{session_id})) { + if ($res->{status} =~ /^204/) { + return {}; + } else { + $logger->error("EbookAPI: checkin failed for OverDrive title $title_id"); + return { error_msg => ( (defined $res->{content}) ? $res->{content} : 'Checkin failed' ) }; + } + } + $logger->error("EbookAPI: no response received from OverDrive server"); + return; +} + # List of patron checkouts: # GET http://patron.api.overdrive.com/v1/patrons/me/checkouts # User-Agent: {Your application} -- 2.11.0