Add deserialize_http_methods attribute to Catalyst::Action::Deserialize.
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Action / Deserialize.pm
CommitLineData
7ad87df9 1package Catalyst::Action::Deserialize;
2
930013e6 3use Moose;
4use namespace::autoclean;
7ad87df9 5
930013e6 6extends 'Catalyst::Action::SerializeBase';
7ad87df9 7use Module::Pluggable::Object;
def65dcc 8use MRO::Compat;
7ad87df9 9
48ff1e96 10our $VERSION = '0.90';
f465980c 11$VERSION = eval $VERSION;
12
05009b91 13has plugins => ( is => 'rw' );
7ad87df9 14
588cbecc 15has deserialize_http_methods => (
16 traits => ['Array'],
17 isa => 'ArrayRef[Str]',
18 builder => '_build_deserialize_http_methods',
19 handles => {
20 deserialize_http_methods => 'elements',
21 },
22);
23
24sub _build_deserialize_http_methods { [qw(POST PUT OPTIONS DELETE)] }
25
7ad87df9 26sub execute {
27 my $self = shift;
e601adda 28 my ( $controller, $c ) = @_;
7ad87df9 29
eccb2137 30 my $method = $c->request->method;
588cbecc 31 if ( grep /^$method$/, $self->deserialize_http_methods ) {
9a76221e 32 my ( $sclass, $sarg, $content_type ) =
33 $self->_load_content_plugins( 'Catalyst::Action::Deserialize',
34 $controller, $c );
35 return 1 unless defined($sclass);
e601adda 36 my $rc;
eccb2137 37 if ( defined($sarg) ) {
e601adda 38 $rc = $sclass->execute( $controller, $c, $sarg );
7ad87df9 39 } else {
e601adda 40 $rc = $sclass->execute( $controller, $c );
41 }
9a76221e 42 if ( $rc eq "0" ) {
43 return $self->_unsupported_media_type( $c, $content_type );
44 } elsif ( $rc ne "1" ) {
45 return $self->_serialize_bad_request( $c, $content_type, $rc );
7ad87df9 46 }
9a76221e 47 }
e601adda 48
def65dcc 49 $self->maybe::next::method(@_);
e601adda 50
51 return 1;
eccb2137 52}
7ad87df9 53
05009b91 54__PACKAGE__->meta->make_immutable;
55
398c5a1b 56=head1 NAME
57
58Catalyst::Action::Deserialize - Deserialize Data in a Request
59
60=head1 SYNOPSIS
61
62 package Foo::Controller::Bar;
63
64 __PACKAGE__->config(
faf5c20b 65 'default' => 'text/x-yaml',
66 'stash_key' => 'rest',
67 'map' => {
68 'text/x-yaml' => 'YAML',
69 'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
70 },
398c5a1b 71 );
72
e601adda 73 sub begin :ActionClass('Deserialize') {}
398c5a1b 74
75=head1 DESCRIPTION
76
588cbecc 77This action will deserialize HTTP POST, PUT, OPTIONS and DELETE requests.
398c5a1b 78It assumes that the body of the HTTP Request is a serialized object.
79The serializer is selected by introspecting the requests content-type
80header.
81
588cbecc 82If you want deserialize any other HTTP method besides POST, PUT,
83OPTIONS and DELETE you can do this by setting the
84C<< deserialize_http_methods >> list via C<< action_args >>.
85Just modify the config in your controller and define a list of HTTP
86methods the deserialization should happen for:
87
88 __PACKAGE__->config(
89 action_args => {
90 '*' => {
91 deserialize_http_methods => [qw(POST PUT OPTIONS DELETE GET)]
92 }
93 }
94 );
95
96See also L<Catalyst::Controller/action_args>.
97
398c5a1b 98The specifics of deserializing each content-type is implemented as
99a plugin to L<Catalyst::Action::Deserialize>. You can see a list
100of currently implemented plugins in L<Catalyst::Controller::REST>.
101
102The results of your Deserializing will wind up in $c->req->data.
103This is done through the magic of L<Catalyst::Request::REST>.
104
e601adda 105While it is common for this Action to be called globally as a
106C<begin> method, there is nothing stopping you from using it on a
107single routine:
398c5a1b 108
e601adda 109 sub foo :Local :Action('Deserialize') {}
398c5a1b 110
e601adda 111Will work just fine.
398c5a1b 112
9a76221e 113When you use this module, the request class will be changed to
114L<Catalyst::Request::REST>.
115
398c5a1b 116=head1 SEE ALSO
117
118You likely want to look at L<Catalyst::Controller::REST>, which implements
119a sensible set of defaults for a controller doing REST.
120
121L<Catalyst::Action::Serialize>, L<Catalyst::Action::REST>
122
5cb5f6bb 123=head1 AUTHORS
398c5a1b 124
5cb5f6bb 125See L<Catalyst::Action::REST> for authors.
398c5a1b 126
127=head1 LICENSE
128
129You may distribute this code under the same terms as Perl itself.
130
131=cut