From bddaee3a63eb5a3e3d1345899dbc5f9c30d52241 Mon Sep 17 00:00:00 2001 From: Jeff Davis Date: Fri, 17 Mar 2017 13:04:09 -0700 Subject: [PATCH] LP#1673870: Add basic ebook API title lookup Adds an API method to obtain the title and author (and, eventually, cover image URL) for a given ebook via the open-ils.ebook_api service. Signed-off-by: Jeff Davis Signed-off-by: Galen Charlton Signed-off-by: Bill Erickson --- .../perlmods/lib/OpenILS/Application/EbookAPI.pm | 31 ++++++++++++++++++ .../Application/EbookAPI/OneClickdigital.pm | 23 +++++++++++++ .../lib/OpenILS/Application/EbookAPI/Test.pm | 38 ++++++++++++++++++++++ .../src/perlmods/live_t/20-lp1541559-ebook-api.t | 14 +++++++- 4 files changed, 105 insertions(+), 1 deletion(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm index 1b3c8c6042..e09282eba9 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm @@ -380,6 +380,37 @@ sub request { } } +sub get_details { + my ($self, $conn, $session_id, $title_id) = @_; + my $handler = new_handler($session_id); + return $handler->get_title_info($title_id); +} +__PACKAGE__->register_method( + method => 'get_details', + api_name => 'open-ils.ebook_api.title.details', + api_level => 1, + argc => 2, + signature => { + desc => "Get basic metadata for an ebook title", + params => [ + { + name => 'session_id', + desc => 'The session ID (provided by open-ils.ebook_api.start_session)', + type => 'string' + }, + { + name => 'title_id', + desc => 'The title ID (ISBN, unique identifier, etc.)', + type => 'string' + } + ], + return => { + desc => 'Success: { title => "Title", author => "Author Name" } / Failure: { error => "Title not found" }', + type => 'hashref' + } + } +); + sub get_availability { my ($self, $conn, $session_id, $title_id) = @_; my $handler = new_handler($session_id); diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OneClickdigital.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OneClickdigital.pm index 166b7c9f7d..be424630eb 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OneClickdigital.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OneClickdigital.pm @@ -127,6 +127,29 @@ sub do_patron_auth { return; } +# get basic metadata for an item (title, author, cover image if any) +# GET http://api.oneclickdigital.us/v1/libraries/{libraryId}/titles/{isbn} +sub get_title_info { + my ($self, $isbn) = @_; + my $base_uri = $self->{base_uri}; + my $library_id = $self->{library_id}; + my $session_id = $self->{session_id}; + my $req = { + method => 'GET', + uri => "$base_uri/libraries/$library_id/titles/$isbn" + }; + my $res = $self->request($req, $session_id); + if (defined ($res)) { + return { + title => $res->{content}->{title}, + author => $res->{content}->{authors}[0]{text} + }; + } else { + $logger->error("EbookAPI: could not retrieve OneClickdigital title details for ISBN $isbn"); + return; + } +} + # does this title have available "copies"? y/n # GET http://api.oneclickdigital.us/v1/libraries/{libraryID}/media/{isbn}/availability sub do_availability_lookup { diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/Test.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/Test.pm index a20846c770..a6f3e3ffb8 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/Test.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/Test.pm @@ -42,6 +42,7 @@ # - do_client_auth: authenticate client with external API (e.g. get client # token if needed) # - do_patron_auth: get a patron-specific bearer token, or just the patron ID +# - get_title_info: get basic title details (title, author, optional cover image) # - do_holdings_lookup: how many total/available "copies" are there for this # title? (n/a for OneClickdigital) # - do_availability_lookup: does this title have available "copies"? y/n @@ -164,6 +165,43 @@ sub do_patron_auth { return undef; } +# get basic info (title, author, eventually a thumbnail URL) for a title +sub get_title_info { + my $self = shift; + + # External ID for title. Depending on the API, this could be an ISBN + # or an identifier unique to that vendor. + my $title_id = shift; + + # Prepare data structure to be used as return value. + my $title_info = { + title => '', + author => '' + }; + + # If title lookup fails or title is not found, our return value + # is somewhat different. + my $title_not_found = { + error => 'Title not found.' + }; + + # For testing purposes, we have only three valid titles (001, 002, 003). + # All other title IDs return an error message. + if ($title_id eq '001') { + $title_info->{title} = 'The Fellowship of the Ring'; + $title_info->{author} = 'J.R.R. Tolkien'; + } elsif ($title_id eq '002') { + $title_info->{title} = 'The Two Towers'; + $title_info->{author} = 'J.R.R. Tolkien'; + } elsif ($title_id eq '003') { + $title_info->{title} = 'The Return of the King'; + $title_info->{author} = 'J.R.R. Tolkien'; + } else { + return $title_not_found; + } + return $title_info; +} + # get detailed holdings information (copy counts and formats), OR basic # availability if detailed info is not provided by the API sub do_holdings_lookup { diff --git a/Open-ILS/src/perlmods/live_t/20-lp1541559-ebook-api.t b/Open-ILS/src/perlmods/live_t/20-lp1541559-ebook-api.t index 72054a5e71..6c0aedb888 100644 --- a/Open-ILS/src/perlmods/live_t/20-lp1541559-ebook-api.t +++ b/Open-ILS/src/perlmods/live_t/20-lp1541559-ebook-api.t @@ -1,6 +1,6 @@ #!perl use strict; use warnings; -use Test::More tests => 21; # XXX +use Test::More tests => 23; # XXX use OpenILS::Utils::TestUtils; diag("Tests Ebook API"); @@ -57,6 +57,18 @@ ok($new_session_id, 'Initiated new EbookAPI session when valid session ID not pr # 3. Title availability and holdings. # ------------------------------------------------------------ +# Title details for valid title ID. +my $title_001_details_req = $ebook_api->request( + 'open-ils.ebook_api.title.details', $session_id, '001'); +my $title_001_details = $title_001_details_req->recv->content; +ok(ref($title_001_details) && $title_001_details->{title}, 'Title details check 1/2 (valid title)'); + +# Title details for invalid title ID. +my $title_004_details_req = $ebook_api->request( + 'open-ils.ebook_api.title.details', $session_id, '004'); +my $title_004_details = $title_004_details_req->recv->content; +ok(ref($title_004_details) && $title_004_details->{error}, 'Title details check 1/2 (invalid title returns error message)'); + # Title is not available. my $title_001_avail_req = $ebook_api->request( 'open-ils.ebook_api.title.availability', $session_id, '001'); -- 2.11.0