Bug 10320: (follow-up) Fix copyright and style errors
[koha.git] / C4 / External / OverDrive.pm
1 package C4::External::OverDrive;
2
3 # Copyright (c) 2013 ByWater
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or ( at your option )  any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22
23 use JSON;
24 use Koha::Cache;
25 use HTTP::Request;
26 use HTTP::Request::Common;
27 use LWP::Authen::Basic;
28 use LWP::UserAgent;
29
30 BEGIN {
31     require Exporter;
32     our $VERSION = 3.07.00.049;
33     our @ISA = qw( Exporter ) ;
34     our @EXPORT = qw(
35         IsOverDriveEnabled
36         GetOverDriveToken
37     );
38 }
39
40 sub _request {
41     my ( $request ) = @_;
42     my $ua = LWP::UserAgent->new( "Koha " . C4::Context->KOHAVERSION );
43
44     my $response;
45     eval {
46         $response = $ua->request( $request ) ;
47     };
48     if ( $@ )  {
49         warn "OverDrive request failed: $@";
50         return;
51     }
52
53     return $response;
54 }
55
56 =head1 NAME
57
58 C4::External::OverDrive - Retrieve OverDrive content availability information
59
60 =head2 FUNCTIONS
61
62 This module provides content search for OverDrive,
63
64 =over
65
66 =item IsOverDriveEnabled
67
68 Returns 1 if all of the necessary system preferences for OverDrive are set.
69
70 =back
71
72 =cut
73
74 sub IsOverDriveEnabled {
75     return (
76         C4::Context->preference( 'OverDriveClientKey' ) &&
77         C4::Context->preference( 'OverDriveClientSecret' )
78     );
79 }
80
81 =over
82
83 =item GetOverDriveToken
84
85 Fetches an OAuth2 auth token for the OverDrive API, reusing an existing token in
86 Memcache if possible.
87
88 Returns the token ( as "bearer ..." )  or undef on failure.
89
90 =back
91
92 =cut
93
94 sub GetOverDriveToken {
95     my $key = C4::Context->preference( 'OverDriveClientKey' );
96     my $secret = C4::Context->preference( 'OverDriveClientSecret' );
97
98     return unless ( $key && $secret ) ;
99
100     my $cache;
101
102     eval { $cache = Koha::Cache->new() };
103
104     my $token;
105     $cache and $token = $cache->get_from_cache( "overdrive_token" ) and return $token;
106
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 ) );
111
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;
116     }
117     my $contents = from_json( $response->decoded_content );
118
119     if ( !$response->is_success ) {
120         warn "Could not log into OverDrive: " . ( $contents ? $contents->{'error_description'} : $response->decoded_content );
121         return;
122     }
123
124     $token = $contents->{'token_type'} . ' ' . $contents->{'access_token'};
125
126     # Fudge factor to prevent spurious failures
127     $cache and $cache->set_in_cache( 'overdrive_token', $token, $contents->{'expires_in'} - 5 );
128
129     return $token;
130 }
131
132 1;
133 __END__
134
135 =head1 NOTES
136
137 =cut
138
139 =head1 AUTHOR
140
141 Jesse Weaver <pianohacker@gmail.com>
142
143 =cut