1 package Koha::REST::Plugin::PluginRoutes;
3 # This file is part of Koha.
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Plugin';
22 use Koha::Exceptions::Plugin;
26 use Clone qw( clone );
27 use Try::Tiny qw( catch try );
31 Koha::REST::Plugin::PluginRoutes
42 my ( $self, $app, $config ) = @_;
44 my $spec = $config->{spec};
45 my $validator = $config->{validator};
49 if ( C4::Context->config("enable_plugins") )
51 $self->{'swagger-v2-schema'} = $app->home->rel_file("api/swagger-v2-schema.json");
53 # plugin needs to define a namespace
54 @plugins = Koha::Plugins->new()->GetPlugins(
56 method => 'api_namespace',
60 foreach my $plugin ( @plugins ) {
61 $spec = $self->inject_routes( $spec, $plugin, $validator );
74 my ( $self, $spec, $plugin, $validator ) = @_;
76 return merge_spec( $spec, $plugin ) unless $validator;
80 my $backup_spec = merge_spec( clone($spec), $plugin );
81 if ( $self->spec_ok( $backup_spec, $validator ) ) {
82 $spec = merge_spec( $spec, $plugin );
85 Koha::Exceptions::Plugin->throw(
86 "The resulting spec is invalid. Skipping " . $plugin->get_metadata->{name}
94 my $class = ref $plugin;
95 my $logger = Koha::Logger->get({ interface => 'api' });
96 $logger->error("Plugin $class route injection failed: $error");
106 my ( $spec, $plugin ) = @_;
108 if($plugin->can('api_routes')) {
109 my $plugin_spec = $plugin->api_routes;
111 foreach my $route ( keys %{ $plugin_spec } ) {
112 my $THE_route = '/contrib/' . $plugin->api_namespace . $route;
113 if ( exists $spec->{ $THE_route } ) {
114 # Route exists, overwriting is forbidden
115 Koha::Exceptions::Plugin::ForbiddenAction->throw(
116 "Attempted to overwrite $THE_route"
120 $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
124 if($plugin->can('static_routes')) {
125 my $plugin_spec = $plugin->static_routes;
127 foreach my $route ( keys %{ $plugin_spec } ) {
129 my $THE_route = '/contrib/' . $plugin->api_namespace . '/static'.$route;
130 if ( exists $spec->{ $THE_route } ) {
131 # Route exists, overwriting is forbidden
132 Koha::Exceptions::Plugin::ForbiddenAction->throw(
133 "Attempted to overwrite $THE_route"
137 $spec->{'paths'}->{ $THE_route } = $plugin_spec->{ $route };
148 my ( $self, $spec, $validator ) = @_;
150 my $schema = $self->{'swagger-v2-schema'};
153 $validator->load_and_validate_schema(
156 allow_invalid_ref => 1,
157 schema => ( $schema ) ? $schema : undef,