example of api call being handled by ajax call

This commit is contained in:
Oliver Gorwits
2019-03-17 08:57:40 +00:00
parent fc02f2c2dd
commit 33f94eac62
3 changed files with 36 additions and 6 deletions

View File

@@ -232,6 +232,14 @@ hook 'after' => sub {
} }
}; };
# forward API calls to AJAX route handlers
any '/api/:type/:identifier/:method' => require_login sub {
vars->{'is_api'} = 1;
my $target =
sprintf '/ajax/content/%s/%s', params->{'type'}, params->{'method'};
forward $target, { tab => params->{'method'}, q => params->{'identifier'} };
};
any qr{.*} => sub { any qr{.*} => sub {
var('notfound' => true); var('notfound' => true);
status 'not_found'; status 'not_found';

View File

@@ -52,7 +52,9 @@ hook 'before' => sub {
session(logged_in_user => 'guest'); session(logged_in_user => 'guest');
session(logged_in_user_realm => 'users'); session(logged_in_user_realm => 'users');
} }
elsif (request_is_api()) { elsif (request_is_api()
and index(request->path, uri_for('/api')->path) == 0) {
my $token = request->header('Authorization'); my $token = request->header('Authorization');
my $user = $provider->validate_api_token($token) my $user = $provider->validate_api_token($token)
or return; or return;

View File

@@ -4,13 +4,25 @@ use Dancer ':syntax';
use Dancer::Plugin::Ajax; use Dancer::Plugin::Ajax;
use Dancer::Plugin::DBIC; use Dancer::Plugin::DBIC;
use Dancer::Plugin::Auth::Extensible; use Dancer::Plugin::Auth::Extensible;
use Dancer::Plugin::Swagger;
use App::Netdisco::Web::Plugin; use App::Netdisco::Web::Plugin;
register_device_tab({ tag => 'details', label => 'Details' }); register_device_tab({ tag => 'details', label => 'Details' });
# device details table # device details table
ajax '/ajax/content/device/details' => require_login sub { swagger_path {
description => 'Get properties and power details for a device.',
path => '/api/device/{identifier}/details',
tags => ['Devices'],
parameters => [
{ name => 'identifier', in => 'path', required => 1, type => 'string' },
],
responses => { default => { examples => {
'application/json' => { device => {}, power => {} },
} } },
},
get '/ajax/content/device/details' => require_login sub {
my $q = param('q'); my $q = param('q');
my $device = schema('netdisco')->resultset('Device') my $device = schema('netdisco')->resultset('Device')
->search_for_device($q) or send_error('Bad device', 400); ->search_for_device($q) or send_error('Bad device', 400);
@@ -24,10 +36,18 @@ ajax '/ajax/content/device/details' => require_login sub {
= schema('netdisco')->resultset('DevicePower') = schema('netdisco')->resultset('DevicePower')
->search( { 'me.ip' => $device->ip } )->with_poestats->hri->all; ->search( { 'me.ip' => $device->ip } )->with_poestats->hri->all;
if (vars->{'is_api'}) {
content_type('application/json');
# TODO merge power into device details
# TODO remove sensitive data (community)
to_json { device => $results[0], power => \@power };
}
else {
content_type('text/html'); content_type('text/html');
template 'ajax/device/details.tt', { template 'ajax/device/details.tt', {
d => $results[0], p => \@power d => $results[0], p => \@power
}, { layout => undef }; }, { layout => undef };
}
}; };
1; 1;