Add Trait containing all the real logic
[catagits/Catalyst-Action-REST.git] / lib / Catalyst / Request / REST.pm
CommitLineData
256c894f 1package Catalyst::Request::REST;
930013e6 2use Moose;
37694e6c 3
4use Catalyst::Utils;
f168fa3e 5use namespace::autoclean;
6
7extends 'Catalyst::Request';
e623bdf2 8with 'Catalyst::TraitFor::Request::REST';
f168fa3e 9
5132f5e4 10sub _insert_self_into {
37694e6c 11 my ($class, $app_class ) = @_;
27beb190 12 # the fallback to $app_class is for the (rare and deprecated) case when
13 # people are defining actions in MyApp.pm instead of in a controller.
2998eff6 14 my $app = (blessed($app_class) && $app_class->can('_application'))
15 ? $app_class->_application : Catalyst::Utils::class2appclass( $app_class ) || $app_class;
37694e6c 16
5132f5e4 17 my $req_class = $app->request_class;
18 return if $req_class->isa($class);
19 if ($req_class eq 'Catalyst::Request') {
20 $app->request_class($class);
21 } else {
22 die "$app has a custom request class $req_class, "
23 . "which is not a $class; see Catalyst::Request::REST";
24 }
25}
256c894f 26
f168fa3e 27__PACKAGE__->meta->make_immutable;
28__END__
29
9a76221e 30=head1 NAME
31
32Catalyst::Request::REST - A REST-y subclass of Catalyst::Request
33
34=head1 SYNOPSIS
35
d6fb033c 36 if ( $c->request->accepts('application/json') ) {
9a76221e 37 ...
38 }
39
40 my $types = $c->request->accepted_content_types();
41
42=head1 DESCRIPTION
43
44This is a subclass of C<Catalyst::Request> that adds a few methods to
45the request object to faciliate writing REST-y code. Currently, these
46methods are all related to the content types accepted by the client.
47
5132f5e4 48Note that if you have a custom request class in your application, and it does
49not inherit from C<Catalyst::Request::REST>, your application will fail with an
50error indicating a conflict the first time it tries to use
51C<Catalyst::Request::REST>'s functionality. To fix this error, make sure your
52custom request class inherits from C<Catalyst::Request::REST>.
9a76221e 53
54=head1 METHODS
55
bd8fc54e 56=over
9a76221e 57
bd8fc54e 58=item data
9a76221e 59
bd8fc54e 60If the request went through the Deserializer action, this method will
61return the deserialized data structure.
fec6d454 62
9a76221e 63=item accepted_content_types
64
65Returns an array reference of content types accepted by the
66client.
67
68The list of types is created by looking at the following sources:
69
70=over 8
71
72=item * Content-type header
73
74If this exists, this will always be the first type in the list.
75
76=item * content-type parameter
77
78If the request is a GET request and there is a "content-type"
79parameter in the query string, this will come before any types in the
80Accept header.
81
82=item * Accept header
83
84This will be parsed and the types found will be ordered by the
85relative quality specified for each type.
86
87=back
88
89If a type appears in more than one of these places, it is ordered based on
90where it is first found.
91
9a76221e 92=item preferred_content_type
93
94This returns the first content type found. It is shorthand for:
95
96 $request->accepted_content_types->[0]
97
9a76221e 98=item accepts($type)
99
100Given a content type, this returns true if the type is accepted.
101
102Note that this does not do any wildcard expansion of types.
103
fec6d454 104=back
105
6e6e0bf9 106=head1 AUTHORS
9a76221e 107
6e6e0bf9 108See L<Catalyst::Action::REST> for authors.
fec6d454 109
9a76221e 110=head1 LICENSE
111
112You may distribute this code under the same terms as Perl itself.
113
114=cut