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