API implementation (#712)

* initial v0 creator

* working json api for generic reports

* add require login

* move report swagger into plugin, and set new default layout of noop

* require proper role and also use new util func

* start to tidy authn

* some work on cleaning up web authn

* clean up the authN checks

* fix bug

* fix the auth for api

* fixes to json handling

* set swagger sort order

* enable most reports for api endpoints

* fix doc

* add paramters to reports

* add missed report

* allow api_parameters in reports config

* reorganise api

* add vlan search

* add port search

* make sure to enable layout processing

* add device search

* add v1 to api paths

* add Node Search

* support api_responses

* add device object search; fix spurious ports field in device result class

* handle some plugins just returning undef if search fails

* errors from api seamlessley

* fix error in date range default

* more sensible default for prefix

* change order of endpoints in swagger-ui

* all db row classes can now TO_JSON

* add device_port api endpoint

* add device ports endpoint

* do not expand docs

* add swagger ui json tree formatter

* add all relations from Device table

* add port relations

* add nodes retrieve on device or vlan

* rename to GetAPIKey

* update config for previous commit
This commit is contained in:
Oliver Gorwits
2020-04-15 21:15:52 +01:00
committed by GitHub
parent a8a77a2df1
commit dff26abc5c
78 changed files with 815 additions and 257 deletions

View File

@@ -3,12 +3,20 @@ package App::Netdisco::Util::Web;
use strict;
use warnings;
use base 'Exporter';
use Dancer ':syntax';
use Time::Piece;
use Time::Seconds;
use base 'Exporter';
our @EXPORT = ();
our @EXPORT_OK = qw/
sort_port sort_modules interval_to_daterange sql_match
sort_port sort_modules
interval_to_daterange
sql_match
request_is_api
request_is_api_report
request_is_api_search
/;
our %EXPORT_TAGS = (all => \@EXPORT_OK);
@@ -25,6 +33,51 @@ subroutines.
=head1 EXPORT_OK
=head2 request_is_api
Client has requested JSON format data and an endpoint under C</api>.
=cut
sub request_is_api {
return ((request->accept =~ m/(?:json|javascript)/) and (
index(request->path, uri_for('/api/')->path) == 0
or
(param('return_url')
and index(param('return_url'), uri_for('/api/')->path) == 0)
));
}
=head2 request_is_api_report
Same as C<request_is_api> but also requires path to start "C</api/v1/report/...>".
=cut
sub request_is_api_report {
return (request_is_api and (
index(request->path, uri_for('/api/v1/report/')->path) == 0
or
(param('return_url')
and index(param('return_url'), uri_for('/api/v1/report/')->path) == 0)
));
}
=head2 request_is_api_search
Same as C<request_is_api> but also requires path to start "C</api/v1/search/...>".
=cut
sub request_is_api_search {
return (request_is_api and (
index(request->path, uri_for('/api/v1/search/')->path) == 0
or
(param('return_url')
and index(param('return_url'), uri_for('/api/v1/search/')->path) == 0)
));
}
=head2 sql_match( $value, $exact? )
Convert wildcard characters "C<*>" and "C<?>" to "C<%>" and "C<_>"