Bug 22705: Change default value of cxn_pool to 'Static'
[koha.git] / C4 / External / OverDrive.pm
blob897e2b3c48c8f7353c3b1adf0695124f5d91be44
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 GetOverDriveToken
39 sub _request {
40 my ( $request ) = @_;
41 my $ua = LWP::UserAgent->new( agent => "Koha " . $Koha::VERSION );
43 my $response;
44 eval {
45 $response = $ua->request( $request ) ;
47 if ( $@ ) {
48 warn "OverDrive request failed: $@";
49 return;
52 return $response;
55 =head1 NAME
57 C4::External::OverDrive - Retrieve OverDrive content availability information
59 =head2 FUNCTIONS
61 This module provides content search for OverDrive,
63 =over
65 =item GetOverDriveToken
67 Fetches an OAuth2 auth token for the OverDrive API, reusing an existing token in
68 Memcache if possible.
70 Returns the token ( as "bearer ..." ) or undef on failure.
72 =back
74 =cut
76 sub GetOverDriveToken {
77 my $key = C4::Context->preference( 'OverDriveClientKey' );
78 my $secret = C4::Context->preference( 'OverDriveClientSecret' );
80 return unless ( $key && $secret ) ;
82 my $cache;
84 eval { $cache = Koha::Caches->get_instance() };
86 my $token;
87 $cache and $token = $cache->get_from_cache( "overdrive_token" ) and return $token;
89 my $request = HTTP::Request::Common::POST( 'https://oauth.overdrive.com/token', [
90 grant_type => 'client_credentials'
91 ] ) ;
92 $request->header( Authorization => LWP::Authen::Basic->auth_header( $key, $secret ) );
94 my $response = _request( $request ) or return;
95 if ( $response->header('Content-Type') !~ m!application/json! ) {
96 warn "Could not connect to OverDrive: " . $response->message;
97 return;
99 my $contents = from_json( $response->decoded_content );
101 if ( !$response->is_success ) {
102 warn "Could not log into OverDrive: " . ( $contents ? $contents->{'error_description'} : $response->decoded_content );
103 return;
106 $token = $contents->{'token_type'} . ' ' . $contents->{'access_token'};
108 # Fudge factor to prevent spurious failures
109 $cache
110 and $cache->set_in_cache( 'overdrive_token', $token,
111 { expiry => $contents->{'expires_in'} - 5 } );
113 return $token;
117 __END__
119 =head1 NOTES
121 =cut
123 =head1 AUTHOR
125 Jesse Weaver <pianohacker@gmail.com>
127 =cut