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