Bug 20434: Update UNIMARC framework - auth (TU)
[koha.git] / Koha / ExternalContent / RecordedBooks.pm
blobc82fc41c3d2c698b80fcb830e40a139c46caaab9
1 # Copyright 2016 Catalyst
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 package Koha::ExternalContent::RecordedBooks;
20 use Modern::Perl;
21 use Carp;
23 use base qw(Koha::ExternalContent);
24 use WebService::ILS::RecordedBooks::PartnerPatron;
25 use WebService::ILS::RecordedBooks::Partner;
26 use C4::Context;
27 use Koha::Logger;
29 use constant logger => Koha::Logger->get();
31 __PACKAGE__->mk_accessors(qw(domain is_identified));
33 =head1 NAME
35 Koha::ExternalContent::RecordedBooks
37 =head1 SYNOPSIS
39 use Koha::ExternalContent::RecordedBooks;
40 my $rb_client = Koha::ExternalContent::RecordedBooks->new();
41 my $rb_auth_url = $od_client->auth_url();
43 =head1 DESCRIPTION
45 A (very) thin wrapper around C<WebService::ILS::RecordedBooks::Patron>
47 Takes "RecordedBooks*" Koha preferences
49 =cut
51 =head2 Class Methods
53 =cut
55 =head3 new
57 my $rb_client = Koha::ExternalContent::RecordedBooks->new();
59 Create the object for interacting with RecordedBooks
61 =cut
63 sub new {
64 my $class = shift;
65 my $params = shift || {};
67 my $self = $class->SUPER::new($params);
68 unless ($params->{client}) {
69 my $client_secret = C4::Context->preference('RecordedBooksClientSecret')
70 or croak("RecordedBooksClientSecret pref not set");
71 my $library_id = C4::Context->preference('RecordedBooksLibraryID')
72 or croak("RecordedBooksLibraryID pref not set");
73 my $domain = C4::Context->preference('RecordedBooksDomain');
74 my $patron = $params->{koha_session_id} ? $self->koha_patron : undef;
75 my $email;
76 if ($patron) {
77 $email = $patron->email
78 or $self->logger->warn("User with no email, cannot identify with RecordedBooks");
80 my $client;
81 if ($email) {
82 local $@;
83 $client = eval { WebService::ILS::RecordedBooks::PartnerPatron->new(
84 client_secret => $client_secret,
85 library_id => $library_id,
86 domain => $domain,
87 user_id => $email,
88 ) };
89 $self->logger->warn("Invalid RecordedBooks user $email ($@)") if $@;
90 $self->is_identified($client);
92 $client ||= WebService::ILS::RecordedBooks::Partner->new(
93 client_secret => $client_secret,
94 library_id => $library_id,
95 domain => $domain,
97 $self->client( $client );
99 return $self;
102 =head1 METHODS
104 L<WebService::ILS::RecordedBooks::PartnerPatron> methods used without mods:
106 =over 4
108 =item C<error_message()>
110 =back
112 =cut
114 use vars qw{$AUTOLOAD};
115 sub AUTOLOAD {
116 my $self = shift;
117 (my $method = $AUTOLOAD) =~ s/.*:://;
118 return $self->client->$method(@_);
120 sub DESTROY { }