1 package Koha::REST::V1::ERM::EUsage::SushiServices;
3 # Copyright 2023 PTFS Europe
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>.
22 use Mojo::Base 'Mojolicious::Controller';
25 use Scalar::Util qw( blessed );
26 use JSON qw( from_json decode_json encode_json );
27 use Try::Tiny qw( catch try );
40 my $c = shift->openapi->valid_input or return;
42 my $args = $c->param('q');
45 my @query_params_array =
46 map { $_ ? $json->decode($_) : () } $args;
48 my $service_url = $query_params_array[0]->{url};
50 my $request = HTTP::Request->new( GET => $service_url );
52 my $ua = LWP::UserAgent->new;
53 my $response = $ua->simple_request($request);
55 if ( $response->code >= 400 ) {
56 my $result = decode_json( $response->decoded_content );
58 if ( ref($result) eq 'ARRAY' ) {
59 for my $r (@$result) {
60 $message .= $r->{message};
63 $message = $result->{message} || $result->{Message} || q{};
64 if ( $result->{errors} ) {
65 for my $e ( @{ $result->{errors} } ) {
66 $message .= $e->{message};
70 warn sprintf "ERROR - Counter registry API %s returned %s - %s\n",
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);
79 "ERROR requesting Counter registry API\n%s\ncode %s: %s\n",
84 } elsif ( $response->code == 204 ) { # No content
88 my $result = decode_json( $response->decoded_content );