App-Dochazka-REST
view release on metacpan or search on metacpan
lib/App/Dochazka/REST/Dispatch.pm view on Meta::CPAN
=head2 Genreport handlers
=head3 handler_genreport
Handler for the 'POST genreport' resource.
=cut
sub handler_genreport {
my ( $self, $pass ) = @_;
$log->debug( "Entering " . __PACKAGE__ . "::handler_genreport" );
# first pass
return 1 if $pass == 1;
# second pass
# - check that entity is kosher
my $status = shared_entity_check( $self, 'path' );
return $status unless $status->ok;
my $context = $self->context;
my $entity = $context->{'request_entity'};
# - get path and look it up
my $path = $entity->{'path'};
my $comp = shared_first_pass_lookup( $self, 'path', $path );
return $fail unless $path;
delete $entity->{'path'};
# - if there is a 'parameters' property, check that it is a hashref
my $parameters;
if ( $entity->{'parameters'} ) {
$log->debug( "Vetting parameters: " . Dumper $entity->{'parameters'} ) ;
if ( ref( $entity->{'parameters'} ) ne 'HASH' ) {
$self->mrest_declare_status(
code => 400,
explanation => 'parameters must be given as key:value pairs'
);
return $fail;
}
# - convert $parameters hashref into $parameters arrayref for validation
my $count = 0;
foreach my $key ( keys %{ $entity->{'parameters'} } ) {
$parameters->[$count] = $key;
$count += 1;
$parameters->[$count] = $entity->{'parameters'}->{$key};
$count += 1;
}
}
# - if there is a validations property, convert it into a hashref
# and check the parameters against it
if ( $comp->{validations} ) {
my $validations = eval $comp->{validations};
$log->debug( "Validations before eval: " . Dumper $comp->{validations} );
$log->debug( "Validations after eval: " . Dumper $validations );
die "AGAAKH! validations is not a HASHREF: $validations" unless
ref( $validations ) eq 'HASH';
$parameters = {} if not defined $parameters;
$log->debug( "About to validate parameters: " . Dumper $parameters );
my $success = 1;
validate_with(
params => $parameters,
spec => $validations,
on_fail => sub {
my $errmsg = shift;
$self->mrest_declare_status( code => 400, explanation => $errmsg );
$success = 0;
},
);
return $fail unless $success;
} elsif ( $parameters ) {
$log->WARN( "Parameters were given to component, but component has no validations!" );
}
# - generate report
$parameters = [] if not defined $parameters;
return $CELL->status_ok(
'DISPATCH_GENERATED_REPORT',
payload => $comp->generate( my %paramhash = @$parameters )
);
}
=head2 History handlers
=head3 handler_history_self
Handler method for the '{priv,schedule}/history/self/?:tsrange' resource.
=cut
sub handler_history_self {
my ( $self, $pass ) = @_;
$log->debug( "Entering " . __PACKAGE__ . "::handler_history_self" );
# first pass
return 1 if $pass == 1;
# second pass
my $context = $self->context;
my %ARGS = (
'eid' => $context->{'current'}->{'eid'},
'nick' => $context->{'current'}->{'nick'},
);
if ( defined $context->{'mapping'}->{'tsrange'} ) {
$ARGS{'tsrange'} = $context->{'mapping'}->{'tsrange'};
}
if ( $context->{'components'}->[0] eq 'priv' ) {
return get_privhistory( $context, %ARGS );
} elsif ( $context->{'components'}->[0] eq 'schedule' ) {
return get_schedhistory( $context, %ARGS );
}
}
=head3 handler_history_get_single
Handler method for GET requests on the '/{priv,schedule}/history/eid/..' and
( run in 1.475 second using v1.01-cache-2.11-cpan-df04353d9ac )