no need for OurPkgVersion with modern dzil
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize / Callback.pm
1 package Catalyst::Action::Deserialize::Callback;
2
3 use Moose;
4 use namespace::autoclean;
5 use Scalar::Util qw(openhandle);
6
7 extends 'Catalyst::Action';
8
9 sub execute {
10     my $self = shift;
11     my ( $controller, $c, $callbacks ) = @_;
12
13     my $rbody;
14
15     # could be a string or a FH
16     if ( my $body = $c->request->body ) {
17         if(openhandle $body) {
18             seek($body, 0, 0); # in case something has already read from it
19             while ( defined( my $line = <$body> ) ) {
20                 $rbody .= $line;
21             }
22         } else {
23             $rbody = $body;
24         }
25     }
26
27     if ( $rbody ) {
28         my $rdata = eval { $callbacks->{deserialize}->( $rbody, $controller, $c ) };
29         if ($@) {
30             return $@;
31         }
32         $c->request->data($rdata);
33     } else {
34         $c->log->debug(
35             'I would have deserialized, but there was nothing in the body!')
36             if $c->debug;
37     }
38     return 1;
39 }
40
41 __PACKAGE__->meta->make_immutable;
42
43 1;
44