Bug 32030: ERM - Agreement documents (FIXED)
[koha-ffzg.git] / Koha / REST / V1.pm
1 package Koha::REST::V1;
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';
21
22 use C4::Context;
23 use Koha::Logger;
24
25 use JSON::Validator::Schema::OpenAPIv2;
26
27 use Try::Tiny qw( catch try );
28
29 =head1 NAME
30
31 Koha::REST::V1 - Main v.1 REST api class
32
33 =head1 API
34
35 =head2 Class Methods
36
37 =head3 startup
38
39 Overloaded Mojolicious->startup method. It is called at application startup.
40
41 =cut
42
43 sub startup {
44     my $self = shift;
45
46     my $logger = Koha::Logger->get({ interface => 'api' });
47     $self->log($logger);
48
49     $self->hook(
50         before_dispatch => sub {
51             my $c = shift;
52
53             # Remove /api/v1/app.pl/ from the path
54             $c->req->url->base->path('/');
55
56             # Handle CORS
57             $c->res->headers->header( 'Access-Control-Allow-Origin' =>
58                   C4::Context->preference('AccessControlAllowOrigin') )
59               if C4::Context->preference('AccessControlAllowOrigin');
60         }
61     );
62
63     # Force charset=utf8 in Content-Type header for JSON responses
64     $self->types->type( json    => 'application/json; charset=utf8' );
65     # MARC-related types
66     $self->types->type( marcxml => 'application/marcxml+xml' );
67     $self->types->type( mij     => 'application/marc-in-json' );
68     $self->types->type( marc    => 'application/marc' );
69
70     my $secret_passphrase = C4::Context->config('api_secret_passphrase');
71     if ($secret_passphrase) {
72         $self->secrets([$secret_passphrase]);
73     }
74
75     my $spec_file = $self->home->rel_file("api/v1/swagger/swagger.yaml");
76
77     push @{$self->routes->namespaces}, 'Koha::Plugin';
78
79     # Try to load and merge all schemas first and validate the result just once.
80     try {
81
82         my $schema = JSON::Validator::Schema::OpenAPIv2->new;
83
84         $schema->resolve( $spec_file );
85
86         my $spec = $schema->bundle->data;
87
88         $self->plugin(
89             'Koha::REST::Plugin::PluginRoutes' => {
90                 spec     => $spec,
91                 validate => 0,
92             }
93         ) unless C4::Context->needs_install; # load only if Koha is installed
94
95         $self->plugin(
96             OpenAPI => {
97                 spec  => $spec,
98                 route => $self->routes->under('/api/v1')->to('Auth#under'),
99             }
100         );
101
102         $self->plugin('RenderFile');
103     }
104     catch {
105         # Validation of the complete spec failed. Resort to validation one-by-one
106         # to catch bad ones.
107
108         # JSON::Validator uses confess, so trim call stack from the message.
109         my $logger = Koha::Logger->get({ interface => 'api' });
110         $logger->error("Warning: Could not load REST API spec bundle: " . $_);
111
112         try {
113
114             my $schema = JSON::Validator::Schema::OpenAPIv2->new;
115             $schema->resolve( $spec_file );
116
117             my $spec = $schema->bundle->data;
118
119             $self->plugin(
120                 'Koha::REST::Plugin::PluginRoutes' => {
121                     spec     => $spec,
122                     validate => 1
123                 }
124             )  unless C4::Context->needs_install; # load only if Koha is installed
125
126             $self->plugin(
127                 OpenAPI => {
128                     spec  => $spec,
129                     route => $self->routes->under('/api/v1')->to('Auth#under'),
130                 }
131             );
132         }
133         catch {
134             # JSON::Validator uses confess, so trim call stack from the message.
135             $logger->error("Warning: Could not load REST API spec bundle: " . $_);
136         };
137     };
138
139     $self->plugin( 'Koha::REST::Plugin::Pagination' );
140     $self->plugin( 'Koha::REST::Plugin::Query' );
141     $self->plugin( 'Koha::REST::Plugin::Objects' );
142     $self->plugin( 'Koha::REST::Plugin::Exceptions' );
143 }
144
145 1;