]> git.koha-community.org Git - koha.git/blob - Koha/REST/V1/Illbackends.pm
Bug 30719: DB and API
[koha.git] / Koha / REST / V1 / Illbackends.pm
1 package Koha::REST::V1::Illbackends;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Illrequest::Config;
23 use Koha::Illrequests;
24 use Koha::Illbackend;
25
26 =head1 NAME
27
28 Koha::REST::V1::Illbackends
29
30 =head2 Operations
31
32 =head3 list
33
34 Return a list of available ILL backends and its capabilities
35
36 =cut
37
38 sub list {
39     my $c = shift->openapi->valid_input;
40
41     my $config   = Koha::Illrequest::Config->new;
42     my $backends = $config->available_backends;
43
44     my @data;
45     foreach my $b (@$backends) {
46         my $backend = Koha::Illrequest->new->load_backend($b);
47         push @data,
48           {
49             ill_backend_id => $b,
50             capabilities   => $backend->capabilities,
51           };
52     }
53     return $c->render( status => 200, openapi => \@data );
54 }
55
56 =head3 get
57
58 Get one backend
59
60 =cut
61
62 sub get {
63     my $c = shift->openapi->valid_input;
64
65     my $backend_id = $c->param('ill_backend_id');
66
67     return try {
68
69         #FIXME: Should we move load_backend into Koha::Illbackend...
70         #       or maybe make Koha::Ill::Backend a base class for all
71         #       backends?
72         my $backend = Koha::Illrequest->new->load_backend($backend_id);
73
74         my $backend_module = Koha::Illbackend->new;
75
76         my $embed =
77           $backend_module->embed( $backend_id,
78             $c->req->headers->header('x-koha-embed') );
79
80         #TODO: We need a to_api method in Koha::Illbackend
81         my $return = {
82             ill_backend_id => $backend_id,
83             capabilities   => $backend->capabilities,
84         };
85
86         return $c->render(
87             status  => 200,
88             openapi => $embed ? { %$return, %$embed } : $return,
89         );
90     }
91     catch {
92         return $c->render(
93             status  => 404,
94             openapi => { error => "ILL backend does not exist" }
95         );
96     };
97 }
98
99 1;