Bug 35581: Koha::Illrequest::Config -> Koha::ILL::Request::Config
[koha.git] / Koha / REST / V1 / ILL / Backends.pm
1 package Koha::REST::V1::ILL::Backends;
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 Koha::ILL::Request::Config;
23 use Koha::Illrequests;
24 use Koha::ILL::Backend;
25
26 =head1 NAME
27
28 Koha::REST::V1::ILL::Backends
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::ILL::Request::Config->new;
42     my $backends       = $config->available_backends;
43     my $backend_module = Koha::ILL::Backend->new;
44
45     my @data;
46     foreach my $b (@$backends) {
47         my $backend = Koha::Illrequest->new->load_backend($b);
48
49         my $embed = $backend_module->embed(
50             $b,
51             $c->req->headers->header('x-koha-embed')
52         );
53
54         my $return = {
55             ill_backend_id => $b,
56             capabilities   => $backend->capabilities,
57         };
58         push @data, $embed ? { %$return, %$embed } : $return;
59     }
60     return $c->render( status => 200, openapi => \@data );
61 }
62
63 =head3 get
64
65 Get one backend
66
67 =cut
68
69 sub get {
70     my $c = shift->openapi->valid_input;
71
72     my $backend_id = $c->param('ill_backend_id');
73
74     return try {
75
76         #FIXME: Should we move load_backend into Koha::ILL::Backend...
77         #       or maybe make Koha::Ill::Backend a base class for all
78         #       backends?
79         my $backend = Koha::Illrequest->new->load_backend($backend_id);
80
81         my $backend_module = Koha::ILL::Backend->new;
82
83         my $embed = $backend_module->embed(
84             $backend_id,
85             $c->req->headers->header('x-koha-embed')
86         );
87
88         #TODO: We need a to_api method in Koha::ILL::Backend
89         my $return = {
90             ill_backend_id => $backend_id,
91             capabilities   => $backend->capabilities,
92         };
93
94         return $c->render(
95             status  => 200,
96             openapi => $embed ? { %$return, %$embed } : $return,
97         );
98     } catch {
99         return $c->render(
100             status  => 404,
101             openapi => { error => "ILL backend does not exist" }
102         );
103     };
104 }
105
106 1;