Bug 17977: Add acquisitions sidebar menu to suggestions
[koha.git] / opac / svc / overdrive_proxy
blob052a7b9d2a5be24388682feecb37173e3ab5b7e1
1 #!/usr/bin/perl
3 # Copyright 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 =head1 NAME
22 svc/overdrive_proxy: Proxy OAuth'd requests to OverDrive
24 =head1 SYNOPSIS
26 svc/overdrive_proxy/libraries/9001 -> https://api.overdrive.com/v1/libraries/9001
28 =head1 DESCRIPTION
30 This service proxies incoming requests to the OverDrive OAuth API, to keep the
31 JS side from having to deal with cross-origin/authentication issues.
33 =cut
35 use strict;
36 use warnings;
38 use CGI qw(-oldstyle_urls -utf8);
39 use JSON;
41 use C4::Context;
42 use C4::External::OverDrive;
43 use C4::Output;
45 use Koha;
47 my $query = new CGI;
49 my $token;
51 if ( !IsOverDriveEnabled() || !( $token = GetOverDriveToken() ) ) {
52 print $query->header(
53 -status => '400 Bad Request',
56 print to_json({
57 error => 'invalid_client',
58 error_description => 'OverDrive login failed'
59 });
61 exit;
63 my $fixed_query = $query->query_string;
64 $fixed_query =~ tr/;/&/;
66 my $request = HTTP::Request::Common::GET( "https://api.overdrive.com/v1" . $query->path_info . '?' . $fixed_query );
67 $request->header( Authorization => $token );
69 my $ua = LWP::UserAgent->new( "Koha " . Koha::version() );
71 my $response = $ua->request( $request ) ;
72 if ( $response->code eq '500' ) {
73 print $query->header(
74 -status => '500 Internal Server Error'
77 warn "OverDrive request failed: " . $response->message;
78 print to_json({
79 error => 'invalid_client',
80 error_description => 'OverDrive request failed'
81 });
83 exit;
86 output_with_http_headers $query, undef, $response->content, 'json', $response->status_line;