Bug 32030: Fix perlcritic errors
[koha.git] / Koha / REST / V1 / ERM / EHoldings / Titles / EBSCO.pm
1 package Koha::REST::V1::ERM::EHoldings::Titles::EBSCO;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use JSON qw( decode_json );
23 use Koha::ERM::Providers::EBSCO;
24
25 use Scalar::Util qw( blessed );
26 use Try::Tiny qw( catch try );
27
28 =head1 API
29
30 =head2 Methods
31
32 =head3 list
33
34 =cut
35
36 sub list {
37     my $c = shift->openapi->valid_input or return;
38
39     return try {
40
41         my $args = $c->validation->output;
42
43         my $ebsco = Koha::ERM::Providers::EBSCO->new;
44
45         # We cannot get base_total as a search kw is required by the API
46
47         my ( $per_page, $page ) = $ebsco->build_query_pagination($args);
48
49         my $additional_params = $ebsco->build_additional_params( $c->req->params->to_hash );
50
51         unless ( defined $additional_params->{publication_title} ) {
52
53             # TODO We can add  search on publisher, isxn, [subject or zdbid]
54             return $c->render(
55                 status  => 400,
56                 openapi => {
57                     errors => [
58                         {
59                             message =>
60 "A search keyword on publication_title is required"
61                         }
62                     ]
63                 }
64             );
65         }
66
67         my $searchfield = 'titlename';
68         my $params =
69           sprintf '?orderby=relevance&offset=%s&count=%s&searchfield=%s',
70           $page, $per_page, $searchfield;
71         my $result =
72           $ebsco->request( GET => '/titles' . $params, $additional_params );
73
74         my $embed_header = $c->req->headers->header('x-koha-embed');
75         my @titles;
76         for my $t ( @{ $result->{titles} } ) {
77             my $title = $ebsco->build_title($t);
78             $title = $ebsco->embed( $title, $t, $embed_header );
79             push @titles, $title;
80         }
81         my $total = $result->{totalResults};
82         $total = 10000 if $total > 10000;
83
84         $c->add_pagination_headers(
85             {
86                 #base_total => $base_total,
87                 total  => $total,
88                 params => $args,
89             }
90         );
91         return $c->render( status => 200, openapi => \@titles );
92     }
93     catch {
94         if ( blessed $_ ) {
95             if ( $_->isa('Koha::Exceptions::Authorization::Unauthorized') ) {
96                 return $c->render(
97                     status  => 401,
98                     openapi => {
99                         errors => [
100                             {
101                                 message => "Check your ERMProviderEbscoApiKey/ERMProviderEbscoCustomerID system preferences."
102                             }
103                         ]
104                     }
105                 );
106             }
107         }
108         $c->unhandled_exception($_);
109     };
110 }
111
112 =head3 get
113
114 =cut
115
116 sub get {
117     my $c = shift->openapi->valid_input or return;
118
119     return try {
120         my $title_id = $c->validation->param('title_id');
121         my $ebsco    = Koha::ERM::Providers::EBSCO->new;
122         my $t        = $ebsco->request( GET => '/titles/' . $title_id );
123         unless ($t) {
124             return $c->render(
125                 status  => 404,
126                 openapi => { error => "Title not found" }
127             );
128         }
129
130         my $title = $ebsco->build_title($t);
131         $title =
132           $ebsco->embed( $title, $t, $c->req->headers->header('x-koha-embed') );
133
134         return $c->render(
135             status  => 200,
136             openapi => $title,
137         );
138     }
139     catch {
140         $c->unhandled_exception($_);
141     };
142 }
143
144 1;