Catalyst-Action-REST

 view release on metacpan or  search on metacpan

lib/Catalyst/Action/Deserialize/Callback.pm  view on Meta::CPAN

package Catalyst::Action::Deserialize::Callback;
$Catalyst::Action::Deserialize::Callback::VERSION = '1.21';
use Moose;
use namespace::autoclean;
use Scalar::Util qw(openhandle);

extends 'Catalyst::Action';

sub execute {
    my $self = shift;
    my ( $controller, $c, $callbacks ) = @_;

    my $rbody;

    # could be a string or a FH
    if ( my $body = $c->request->body ) {
        if(openhandle $body) {
            seek($body, 0, 0); # in case something has already read from it
            while ( defined( my $line = <$body> ) ) {
                $rbody .= $line;
            }
        } else {
            $rbody = $body;
        }
    }

    if ( $rbody ) {
        my $rdata = eval { $callbacks->{deserialize}->( $rbody, $controller, $c ) };
        if ($@) {
            return $@;
        }
        $c->request->data($rdata);
    } else {
        $c->log->debug(
            'I would have deserialized, but there was nothing in the body!')
            if $c->debug;
    }
    return 1;

lib/Catalyst/Action/Serialize/Callback.pm  view on Meta::CPAN

package Catalyst::Action::Serialize::Callback;
$Catalyst::Action::Serialize::Callback::VERSION = '1.21';
use Moose;
use namespace::autoclean;

extends 'Catalyst::Action';

sub execute {
    my $self = shift;
    my ( $controller, $c, $callbacks ) = @_;

    my $stash_key = (
            $controller->{'serialize'} ?
                $controller->{'serialize'}->{'stash_key'} :
                $controller->{'stash_key'}
        ) || 'rest';
    my $output = $callbacks->{serialize}->( $c->stash->{$stash_key}, $controller, $c );
    $c->response->output( $output );
    return 1;
}

__PACKAGE__->meta->make_immutable;

1;

lib/Catalyst/Controller/REST.pm  view on Meta::CPAN

      map => {
          'text/xml'  => [ 'Callback', { deserialize => \&parse_xml, serialize => \&render_xml } ],
      }
  );

The C<deserialize> callback is passed a string that is the body of the
request and is expected to return a scalar value that results from
the deserialization.  The C<serialize> callback is passed the data
structure that needs to be serialized and must return a string suitable
for returning in the HTTP response.  In addition to receiving the scalar
to act on, both callbacks are passed the controller object and the context
(i.e. C<$c>) as the second and third arguments.

=back

By default, L<Catalyst::Controller::REST> will return a
C<415 Unsupported Media Type> response if an attempt to use an unsupported
content-type is made.  You can ensure that something is always returned by
setting the C<default> config option:

  __PACKAGE__->config(default => 'text/x-yaml');



( run in 0.454 second using v1.01-cache-2.11-cpan-9b1e4054eb1 )