View | Details | Raw Unified | Return to bug 38745
Collapse All | Expand All

(-)a/Koha/REST/V1/RPC.pm (-1 / +64 lines)
Line 0 Link Here
0
- 
1
package Koha::REST::V1::RPC;
2
3
# Copyright PTFS Europe 2024
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
24
=head1 API
25
26
=head2 Methods
27
28
=head3 process
29
30
=cut
31
32
sub process {
33
    my $c = shift->openapi->valid_input or return;
34
35
    # Parse incoming JSON-RPC payload
36
    my $payload = $c->req->json;
37
38
    my $method = $payload->{method};
39
    my $params = $payload->{params};
40
    my $id     = $payload->{id};
41
42
    # Route to handlers
43
    if ($method =~ /^([\w\.]+):([\w]+)$/) {
44
        my ($class_path, $action) = ($1, $2);
45
46
        # Map JSON-RPC class path to Perl module
47
        my $module = "Koha::REST::V1::" . join('::', map { ucfirst } split(/\./, $class_path));
48
49
        if ( $module->can($action) ) {
50
            my $result = eval { $module->$action( $c, $params ) };
51
            if ($@) {
52
                $c->app->log->error("Error in $method: $@");
53
                return $c->render(
54
                    json => { jsonrpc => '2.0', error => { code => -32603, message => 'Internal Error' }, id => $id } );
55
            }
56
            return $c->render( json => { jsonrpc => '2.0', result => $result, id => $id } );
57
        }
58
    }
59
60
    return $c->render(
61
        json => { jsonrpc => '2.0', error => { code => -32601, message => 'Method not found' }, id => $id } );
62
}
63
64
1;

Return to bug 38745