Bug 21975: (follow-up) get item datas from previous SQL query
[koha.git] / C4 / External / OverDrive.pm
blob9044169b59a8a4b1361346b8caf5a71df9d7c014
1 package C4::External::OverDrive;
3 # Copyright (c) 2013 ByWater
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 use warnings;
23 use Koha;
24 use JSON;
25 use Koha::Caches;
26 use HTTP::Request;
27 use HTTP::Request::Common;
28 use LWP::Authen::Basic;
29 use LWP::UserAgent;
31 BEGIN {
32 require Exporter;
33 our @ISA = qw( Exporter ) ;
34 our @EXPORT = qw(
35 IsOverDriveEnabled
36 GetOverDriveToken
40 sub _request {
41 my ( $request ) = @_;
42 my $ua = LWP::UserAgent->new( agent => "Koha " . $Koha::VERSION );
44 my $response;
45 eval {
46 $response = $ua->request( $request ) ;
48 if ( $@ ) {
49 warn "OverDrive request failed: $@";
50 return;
53 return $response;
56 =head1 NAME
58 C4::External::OverDrive - Retrieve OverDrive content availability information
60 =head2 FUNCTIONS
62 This module provides content search for OverDrive,
64 =over
66 =item IsOverDriveEnabled
68 Returns 1 if all of the necessary system preferences for OverDrive are set.
70 =back
72 =cut
74 sub IsOverDriveEnabled {
75 return (
76 C4::Context->preference( 'OverDriveClientKey' ) &&
77 C4::Context->preference( 'OverDriveClientSecret' )
81 =over
83 =item GetOverDriveToken
85 Fetches an OAuth2 auth token for the OverDrive API, reusing an existing token in
86 Memcache if possible.
88 Returns the token ( as "bearer ..." ) or undef on failure.
90 =back
92 =cut
94 sub GetOverDriveToken {
95 my $key = C4::Context->preference( 'OverDriveClientKey' );
96 my $secret = C4::Context->preference( 'OverDriveClientSecret' );
98 return unless ( $key && $secret ) ;
100 my $cache;
102 eval { $cache = Koha::Caches->get_instance() };
104 my $token;
105 $cache and $token = $cache->get_from_cache( "overdrive_token" ) and return $token;
107 my $request = HTTP::Request::Common::POST( 'https://oauth.overdrive.com/token', [
108 grant_type => 'client_credentials'
109 ] ) ;
110 $request->header( Authorization => LWP::Authen::Basic->auth_header( $key, $secret ) );
112 my $response = _request( $request ) or return;
113 if ( $response->header('Content-Type') !~ m!application/json! ) {
114 warn "Could not connect to OverDrive: " . $response->message;
115 return;
117 my $contents = from_json( $response->decoded_content );
119 if ( !$response->is_success ) {
120 warn "Could not log into OverDrive: " . ( $contents ? $contents->{'error_description'} : $response->decoded_content );
121 return;
124 $token = $contents->{'token_type'} . ' ' . $contents->{'access_token'};
126 # Fudge factor to prevent spurious failures
127 $cache
128 and $cache->set_in_cache( 'overdrive_token', $token,
129 { expiry => $contents->{'expires_in'} - 5 } );
131 return $token;
135 __END__
137 =head1 NOTES
139 =cut
141 =head1 AUTHOR
143 Jesse Weaver <pianohacker@gmail.com>
145 =cut