Bug 34587: Fix ->validation->output in API endpoints
[koha.git] / Koha / REST / V1 / ERM / EUsage / SushiServices.pm
1 package Koha::REST::V1::ERM::EUsage::SushiServices;
2
3 # Copyright 2023 PTFS Europe
4
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use Modern::Perl;
21
22 use Mojo::Base 'Mojolicious::Controller';
23 use HTTP::Request;
24 use LWP::UserAgent;
25 use Scalar::Util qw( blessed );
26 use JSON         qw( from_json decode_json encode_json );
27 use Try::Tiny    qw( catch try );
28
29 use Koha::Exceptions;
30
31 =head1 API
32
33 =head2 Methods
34
35 =head3 get
36
37 =cut
38
39 sub get {
40     my $c = shift->openapi->valid_input or return;
41
42     my $args = $c->param('q');
43     my $json = JSON->new;
44
45     my @query_params_array =
46         map { $_ ? $json->decode($_) : () } $args;
47
48     my $service_url = $query_params_array[0]->{url};
49
50     my $request = HTTP::Request->new( GET => $service_url );
51
52     my $ua       = LWP::UserAgent->new;
53     my $response = $ua->simple_request($request);
54
55     if ( $response->code >= 400 ) {
56         my $result = decode_json( $response->decoded_content );
57         my $message;
58         if ( ref($result) eq 'ARRAY' ) {
59             for my $r (@$result) {
60                 $message .= $r->{message};
61             }
62         } else {
63             $message = $result->{message} || $result->{Message} || q{};
64             if ( $result->{errors} ) {
65                 for my $e ( @{ $result->{errors} } ) {
66                     $message .= $e->{message};
67                 }
68             }
69         }
70         warn sprintf "ERROR - Counter registry API %s returned %s - %s\n",
71             $service_url,
72             $response->code, $message;
73         if ( $response->code == 404 ) {
74             Koha::Exceptions::ObjectNotFound->throw($message);
75         } elsif ( $response->code == 401 ) {
76             Koha::Exceptions::Authorization::Unauthorized->throw($message);
77         } else {
78             die sprintf
79                 "ERROR requesting Counter registry API\n%s\ncode %s: %s\n",
80                 $service_url,
81                 $response->code,
82                 $message;
83         }
84     } elsif ( $response->code == 204 ) {    # No content
85         return;
86     }
87
88     my $result = decode_json( $response->decoded_content );
89
90     return $c->render(
91         status  => 200,
92         openapi => $result
93     );
94 }
95
96 1;