broken refactor but need to leave now...
This commit is contained in:
@@ -8,7 +8,6 @@ use Time::Seconds;
|
||||
use base 'Exporter';
|
||||
our @EXPORT = ();
|
||||
our @EXPORT_OK = qw/
|
||||
request_is_api
|
||||
sort_port sort_modules
|
||||
interval_to_daterange
|
||||
sql_match
|
||||
@@ -28,18 +27,6 @@ subroutines.
|
||||
|
||||
=head1 EXPORT_OK
|
||||
|
||||
=head2 request_is_api
|
||||
|
||||
Whether the request should be interpreted as an API call.
|
||||
|
||||
=cut
|
||||
|
||||
sub request_is_api {
|
||||
return (setting('api_token_lifetime')
|
||||
and request->accept =~ m/(?:json|javascript)/
|
||||
and index(var('orig_path'), uri_for('/api')->path) == 0);
|
||||
}
|
||||
|
||||
=head2 sql_match( $value, $exact? )
|
||||
|
||||
Convert wildcard characters "C<*>" and "C<?>" to "C<%>" and "C<_>"
|
||||
|
||||
@@ -12,8 +12,7 @@ use HTML::Entities (); # to ensure dependency is met
|
||||
use URI::QueryParam (); # part of URI, to add helper methods
|
||||
use Path::Class 'dir';
|
||||
use Module::Load ();
|
||||
use App::Netdisco::Util::Web
|
||||
qw/request_is_api interval_to_daterange/;
|
||||
use App::Netdisco::Util::Web 'interval_to_daterange';
|
||||
|
||||
use App::Netdisco::Web::AuthN;
|
||||
use App::Netdisco::Web::OpenAPI;
|
||||
@@ -202,7 +201,7 @@ hook 'after' => sub {
|
||||
};
|
||||
|
||||
any qr{.*} => sub {
|
||||
if (request_is_api()) {
|
||||
if (request->is_api) {
|
||||
status(404);
|
||||
return to_json { error => 'not found' };
|
||||
}
|
||||
@@ -222,6 +221,16 @@ any qr{.*} => sub {
|
||||
$response->status($status || 302);
|
||||
$response->headers('Location' => $destination);
|
||||
};
|
||||
|
||||
# helper for handlers of more than one method type
|
||||
*Dancer::Request::is_api = sub {
|
||||
my $self = shift;
|
||||
vars->{'orig_path'} = request->path unless request->is_forward;
|
||||
my $path = ($self->is_forward ? vars->{'orig_path'} : $self->path);
|
||||
return (setting('api_token_lifetime')
|
||||
and $self->accept =~ m/(?:json|javascript)/
|
||||
and index($path, uri_for('/api')->path) == 0);
|
||||
};
|
||||
}
|
||||
|
||||
true;
|
||||
|
||||
@@ -5,7 +5,6 @@ use Dancer::Plugin::DBIC;
|
||||
use Dancer::Plugin::Auth::Extensible;
|
||||
use Dancer::Plugin::Swagger;
|
||||
|
||||
use App::Netdisco::Util::Web 'request_is_api';
|
||||
use MIME::Base64;
|
||||
|
||||
hook 'before' => sub {
|
||||
@@ -49,7 +48,7 @@ hook 'before' => sub {
|
||||
session(logged_in_user => 'guest');
|
||||
session(logged_in_user_realm => 'users');
|
||||
}
|
||||
elsif (request_is_api()
|
||||
elsif (request->is_api
|
||||
and request->header('Authorization')) {
|
||||
|
||||
my $token = request->header('Authorization');
|
||||
@@ -68,7 +67,7 @@ hook 'before' => sub {
|
||||
|
||||
# user redirected here (POST -> GET) when login fails
|
||||
get qr{^/(?:login(?:/denied)?)?} => sub {
|
||||
if (request_is_api()) {
|
||||
if (request->is_api) {
|
||||
status('unauthorized');
|
||||
return to_json {
|
||||
error => 'not authorized',
|
||||
@@ -96,7 +95,7 @@ swagger_path {
|
||||
},
|
||||
},
|
||||
post qr{^/(?:api/)?login$} => sub {
|
||||
my $mode = (request_is_api() ? 'API' : 'WebUI');
|
||||
my $mode = (request->is_api ? 'API' : 'WebUI');
|
||||
|
||||
my $x = params; use DDP; p $x;
|
||||
|
||||
@@ -173,7 +172,7 @@ swagger_path {
|
||||
responses => { default => { examples => { 'application/json' => {} } } },
|
||||
},
|
||||
get qr{^/(?:api/)?logout$} => sub {
|
||||
my $mode = (request_is_api() ? 'API' : 'WebUI');
|
||||
my $mode = (request->is_api ? 'API' : 'WebUI');
|
||||
|
||||
# clear out API token
|
||||
my $user = schema('netdisco')->resultset('User')
|
||||
|
||||
@@ -5,9 +5,6 @@ use Dancer::Plugin::Ajax;
|
||||
|
||||
use Dancer::Plugin::Swagger;
|
||||
|
||||
use App::Netdisco::Util::Web
|
||||
qw/request_is_api interval_to_daterange/;
|
||||
|
||||
# setup for swagger API
|
||||
my $swagger = Dancer::Plugin::Swagger->instance->doc;
|
||||
$swagger->{schemes} = ['http','https'];
|
||||
@@ -30,11 +27,6 @@ $swagger->{securityDefinitions} = {
|
||||
};
|
||||
$swagger->{security} = [ { APIKeyHeader => [] } ];
|
||||
|
||||
# support for checking if this is an api request even after forward
|
||||
hook 'before' => sub {
|
||||
vars->{'orig_path'} = request->path unless request->is_forward;
|
||||
};
|
||||
|
||||
# workaround for Swagger plugin weird response body
|
||||
hook 'after' => sub {
|
||||
my $r = shift; # a Dancer::Response
|
||||
@@ -50,7 +42,6 @@ any '/api/:type/:identifier/:method' => require_login sub {
|
||||
pass unless setting('api_enabled')
|
||||
->{ params->{'type'} }->{ params->{'method'} };
|
||||
|
||||
vars->{'is_api'} = 1;
|
||||
my $target =
|
||||
sprintf '/ajax/content/%s/%s', params->{'type'}, params->{'method'};
|
||||
forward $target, { tab => params->{'method'}, q => params->{'identifier'} };
|
||||
|
||||
@@ -3,7 +3,6 @@ package App::Netdisco::Web::Plugin;
|
||||
use Dancer ':syntax';
|
||||
use Dancer::Plugin;
|
||||
|
||||
use App::Netdisco::Util::Web 'request_is_api';
|
||||
use Path::Class 'dir';
|
||||
|
||||
set(
|
||||
@@ -25,7 +24,7 @@ set(
|
||||
config->{engines}->{netdisco_template_toolkit}->{INCLUDE_PATH} ||= [ setting('views') ];
|
||||
|
||||
register 'bang' => sub {
|
||||
if (request_is_api()) {
|
||||
if (request->is_api) {
|
||||
content_type('application/json');
|
||||
set serializer => 'JSON';
|
||||
status $_[1];
|
||||
|
||||
@@ -22,7 +22,6 @@ swagger_path {
|
||||
} } },
|
||||
},
|
||||
get '/api/device/:identifier' => require_login sub {
|
||||
vars->{'is_api'} = 1;
|
||||
forward '/ajax/content/device/details',
|
||||
{ tab => 'details', q => params->{'identifier'} };
|
||||
};
|
||||
@@ -45,7 +44,7 @@ get '/ajax/content/device/details' => require_login sub {
|
||||
delete $results[0]->{'snmp_comm'};
|
||||
$results[0]->{'power'} = \@power;
|
||||
|
||||
if (vars->{'is_api'}) {
|
||||
if (request->is_api) {
|
||||
content_type('application/json');
|
||||
to_json { device => $results[0] };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user