From 33f94eac62477d49425bedb0f0b052781c8bbf53 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sun, 17 Mar 2019 08:57:40 +0000 Subject: [PATCH] example of api call being handled by ajax call --- lib/App/Netdisco/Web.pm | 8 +++++ lib/App/Netdisco/Web/AuthN.pm | 4 ++- lib/App/Netdisco/Web/Plugin/Device/Details.pm | 30 +++++++++++++++---- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/App/Netdisco/Web.pm b/lib/App/Netdisco/Web.pm index abff36dd..8ab97e2e 100644 --- a/lib/App/Netdisco/Web.pm +++ b/lib/App/Netdisco/Web.pm @@ -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'; diff --git a/lib/App/Netdisco/Web/AuthN.pm b/lib/App/Netdisco/Web/AuthN.pm index 7f2d0918..660ee54e 100644 --- a/lib/App/Netdisco/Web/AuthN.pm +++ b/lib/App/Netdisco/Web/AuthN.pm @@ -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; diff --git a/lib/App/Netdisco/Web/Plugin/Device/Details.pm b/lib/App/Netdisco/Web/Plugin/Device/Details.pm index 46edc359..a5fb5e07 100644 --- a/lib/App/Netdisco/Web/Plugin/Device/Details.pm +++ b/lib/App/Netdisco/Web/Plugin/Device/Details.pm @@ -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;