example of api call being handled by ajax call
This commit is contained in:
@@ -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 {
|
||||
var('notfound' => true);
|
||||
status 'not_found';
|
||||
|
||||
@@ -52,7 +52,9 @@ hook 'before' => sub {
|
||||
session(logged_in_user => 'guest');
|
||||
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 $user = $provider->validate_api_token($token)
|
||||
or return;
|
||||
|
||||
@@ -4,13 +4,25 @@ use Dancer ':syntax';
|
||||
use Dancer::Plugin::Ajax;
|
||||
use Dancer::Plugin::DBIC;
|
||||
use Dancer::Plugin::Auth::Extensible;
|
||||
use Dancer::Plugin::Swagger;
|
||||
|
||||
use App::Netdisco::Web::Plugin;
|
||||
|
||||
register_device_tab({ tag => 'details', label => 'Details' });
|
||||
|
||||
# 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 $device = schema('netdisco')->resultset('Device')
|
||||
->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')
|
||||
->search( { 'me.ip' => $device->ip } )->with_poestats->hri->all;
|
||||
|
||||
content_type('text/html');
|
||||
template 'ajax/device/details.tt', {
|
||||
d => $results[0], p => \@power
|
||||
}, { layout => undef };
|
||||
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');
|
||||
template 'ajax/device/details.tt', {
|
||||
d => $results[0], p => \@power
|
||||
}, { layout => undef };
|
||||
}
|
||||
};
|
||||
|
||||
1;
|
||||
|
||||
Reference in New Issue
Block a user