Actually fixing the version number (hopefully)
[catagits/Catalyst-Action-REST.git] / README
1 NAME
2     Catalyst::Controller::REST - A RESTful controller
3
4 SYNOPSIS
5         package Foo::Controller::Bar;
6
7         use base 'Catalyst::Controller::REST';
8
9         sub thing : Local : ActionClass('REST') { }
10
11         # Answer GET requests to "thing"
12         sub thing_GET {
13            my ( $self, $c ) = @_;
14      
15            # Return a 200 OK, with the data in entity
16            # serialized in the body 
17            $self->status_ok(
18                 $c, 
19                 entity => {
20                     some => 'data',
21                     foo  => 'is real bar-y',
22                 },
23            );
24         }
25
26         # Answer PUT requests to "thing"
27         sub thing_PUT { 
28           .. some action ..
29         }
30
31 DESCRIPTION
32     Catalyst::Controller::REST implements a mechanism for building RESTful
33     services in Catalyst. It does this by extending the normal Catalyst
34     dispatch mechanism to allow for different subroutines to be called based
35     on the HTTP Method requested, while also transparently handling all the
36     serialization/deserialization for you.
37
38     This is probably best served by an example. In the above controller, we
39     have declared a Local Catalyst action on "sub thing", and have used the
40     ActionClass('REST').
41
42     Below, we have declared "thing_GET" and "thing_PUT". Any GET requests to
43     thing will be dispatched to "thing_GET", while any PUT requests will be
44     dispatched to "thing_PUT".
45
46     Any unimplemented HTTP methods will be met with a "405 Method Not
47     Allowed" response, automatically containing the proper list of available
48     methods. You can override this behavior through implementing a custom
49     "thing_not_implemented" method.
50
51     If you do not provide an OPTIONS handler, we will respond to any OPTIONS
52     requests with a "200 OK", populating the Allowed header automatically.
53
54     Any data included in "$c->stash->{'rest'}" will be serialized for you.
55     The serialization format will be selected based on the content-type of
56     the incoming request. It is probably easier to use the "STATUS HELPERS",
57     which are described below.
58
59     The HTTP POST, PUT, and OPTIONS methods will all automatically
60     deserialize the contents of $c->request->body based on the requests
61     content-type header. A list of understood serialization formats is
62     below.
63
64     If we do not have (or cannot run) a serializer for a given content-type,
65     a 415 "Unsupported Media Type" error is generated.
66
67     To make your Controller RESTful, simply have it
68
69       use base 'Catalyst::Controller::REST'; 
70
71 SERIALIZATION
72     Catalyst::Controller::REST will automatically serialize your responses,
73     and deserialize any POST, PUT or OPTIONS requests. It evaluates which
74     serializer to use by mapping a content-type to a Serialization module.
75     We select the content-type based on:
76
77     The Content-Type Header
78       If the incoming HTTP Request had a Content-Type header set, we will
79       use it.
80
81     The content-type Query Parameter
82       If this is a GET request, you can supply a content-type query
83       parameter.
84
85     Evaluating the Accept Header
86       Finally, if the client provided an Accept header, we will evaluate it
87       and use the best-ranked choice.
88
89 AVAILABLE SERIALIZERS
90     A given serialization mechanism is only available if you have the
91     underlying modules installed. For example, you can't use XML::Simple if
92     it's not already installed.
93
94     In addition, each serializer has it's quirks in terms of what sorts of
95     data structures it will properly handle. Catalyst::Controller::REST
96     makes no attempt to svae you from yourself in this regard. :)
97
98     "text/x-yaml" => "YAML::Syck"
99       Returns YAML generated by YAML::Syck.
100
101     "text/html" => "YAML::HTML"
102       This uses YAML::Syck and URI::Find to generate YAML with all URLs
103       turned to hyperlinks. Only useable for Serialization.
104
105     "text/x-json" => "JSON::Syck"
106       Uses JSON::Syck to generate JSON output
107
108     "text/x-data-dumper" => "Data::Serializer"
109       Uses the Data::Serializer module to generate Data::Dumper output.
110
111     "text/x-data-denter" => "Data::Serializer"
112       Uses the Data::Serializer module to generate Data::Denter output.
113
114     "text/x-data-taxi" => "Data::Serializer"
115       Uses the Data::Serializer module to generate Data::Taxi output.
116
117     "application/x-storable" => "Data::Serializer"
118       Uses the Data::Serializer module to generate Storable output.
119
120     "application/x-freezethaw" => "Data::Serializer"
121       Uses the Data::Serializer module to generate FreezeThaw output.
122
123     "text/x-config-general" => "Data::Serializer"
124       Uses the Data::Serializer module to generate Config::General output.
125
126     "text/x-php-serialization" => "Data::Serializer"
127       Uses the Data::Serializer module to generate PHP::Serialization
128       output.
129
130     "text/xml" => "XML::Simple"
131       Uses XML::Simple to generate XML output. This is probably not suitable
132       for any real heavy XML work. Due to XML::Simples requirement that the
133       data you serialize be a HASHREF, we transform outgoing data to be in
134       the form of:
135
136         { data => $yourdata }
137
138     View
139       Uses a regular Catalyst view. For example, if you wanted to have your
140       "text/html" and "text/xml" views rendered by TT:
141
142               'text/html' => [ 'View', 'TT' ],
143               'text/xml'  => [ 'View', 'XML' ],
144         
145       Will do the trick nicely.
146
147     By default, Catalyst::Controller::REST will return a "415 Unsupported
148     Media Type" response if an attempt to use an unsupported content-type is
149     made. You can ensure that something is always returned by setting the
150     "default" config option:
151
152        __PACKAGE__->config->{'serialize'}->{'default'} = 'text/x-yaml';
153
154     Would make it always fall back to the serializer plugin defined for
155     text/x-yaml.
156
157     Implementing new Serialization formats is easy! Contributions are most
158     welcome! See Catalyst::Action::Serialize and
159     Catalyst::Action::Deserialize for more information.
160
161 CUSTOM SERIALIZERS
162     If you would like to implement a custom serializer, you should create
163     two new modules in the Catalyst::Action::Serialize and
164     Catalyst::Action::Deserialize namespace. Then assign your new class to
165     the content-type's you want, and you're done.
166
167 STATUS HELPERS
168     Since so much of REST is in using HTTP, we provide these Status Helpers.
169     Using them will ensure that you are responding with the proper codes,
170     headers, and entities.
171
172     These helpers try and conform to the HTTP 1.1 Specification. You can
173     refer to it at: <http://www.w3.org/Protocols/rfc2616/rfc2616.txt>. These
174     routines are all implemented as regular subroutines, and as such require
175     you pass the current context ($c) as the first argument.
176
177     status_ok
178         Returns a "200 OK" response. Takes an "entity" to serialize.
179
180         Example:
181
182           $self->status_ok(
183             $c, 
184             entity => {
185                 radiohead => "Is a good band!",
186             }
187           );
188
189     status_created
190         Returns a "201 CREATED" response. Takes an "entity" to serialize,
191         and a "location" where the created object can be found.
192
193         Example:
194
195           $self->status_created(
196             $c, 
197             location => $c->req->uri->as_string,
198             entity => {
199                 radiohead => "Is a good band!",
200             }
201           );
202
203         In the above example, we use the requested URI as our location. This
204         is probably what you want for most PUT requests.
205
206     status_accepted
207         Returns a "202 ACCEPTED" response. Takes an "entity" to serialize.
208
209         Example:
210
211           $self->status_accepted(
212             $c, 
213             entity => {
214                 status => "queued",
215             }
216           );
217
218     status_bad_request
219         Returns a "400 BAD REQUEST" response. Takes a "message" argument as
220         a scalar, which will become the value of "error" in the serialized
221         response.
222
223         Example:
224
225           $self->status_bad_request(
226             $c, 
227             message => "Cannot do what you have asked!",
228           );
229
230     status_not_found
231         Returns a "404 NOT FOUND" response. Takes a "message" argument as a
232         scalar, which will become the value of "error" in the serialized
233         response.
234
235         Example:
236
237           $self->status_not_found(
238             $c, 
239             message => "Cannot find what you were looking for!",
240           );
241
242 MANUAL RESPONSES
243     If you want to construct your responses yourself, all you need to do is
244     put the object you want serialized in $c->stash->{'rest'}.
245
246 IMPLEMENTATION DETAILS
247     This Controller ties together Catalyst::Action::REST,
248     Catalyst::Action::Serialize and Catalyst::Action::Deserialize. It should
249     be suitable for most applications. You should be aware that it:
250
251     Configures the Serialization Actions
252         This class provides a default configuration for Serialization. It is
253         currently:
254
255           __PACKAGE__->config(
256               serialize => {
257                  'stash_key' => 'rest',
258                  'map'       => {
259                     'text/html'          => 'YAML::HTML',
260                     'text/xml'           => 'XML::Simple',
261                     'text/x-yaml'        => 'YAML',
262                     'text/x-json'        => 'JSON',
263                     'text/x-data-dumper' => [ 'Data::Serializer', 'Data::Dumper' ],
264                     'text/x-data-denter' => [ 'Data::Serializer', 'Data::Denter' ],
265                     'text/x-data-taxi'   => [ 'Data::Serializer', 'Data::Taxi'   ],
266                     'application/x-storable'    => [ 'Data::Serializer', 'Storable'     
267         ],
268                     'application/x-freezethaw'  => [ 'Data::Serializer', 'FreezeThaw'   
269         ],
270                     'text/x-config-general' => [ 'Data::Serializer', 'Config::General' ]
271         ,
272                     'text/x-php-serialization' => [ 'Data::Serializer', 'PHP::Serialization' ],
273                   },
274               }
275           );
276
277         You can read the full set of options for this configuration block in
278         Catalyst::Action::Serialize.
279
280     Sets a "begin" and "end" method for you
281         The "begin" method uses Catalyst::Action::Deserialize. The "end"
282         method uses Catalyst::Action::Serialize. If you want to override
283         either behavior, simply implement your own "begin" and "end" actions
284         and use NEXT:
285
286           my Foo::Controller::Monkey;
287           use base qw(Catalyst::Controller::REST);
288
289           sub begin :Private {
290             my ($self, $c) = @_;
291             ... do things before Deserializing ...
292             $self->NEXT::begin($c); 
293             ... do things after Deserializing ...
294           } 
295
296           sub end :Private {
297             my ($self, $c) = @_;
298             ... do things before Serializing ...
299             $self->NEXT::end($c); 
300             ... do things after Serializing ...
301           }
302
303 A MILD WARNING
304         I have code in production using Catalyst::Controller::REST. That
305         said, it is still under development, and it's possible that things
306         may change between releases. I promise to not break things
307         unneccesarily. :)
308
309 SEE ALSO
310         Catalyst::Action::REST, Catalyst::Action::Serialize,
311         Catalyst::Action::Deserialize
312
313         For help with REST in general:
314
315         The HTTP 1.1 Spec is required reading.
316         http://www.w3.org/Protocols/rfc2616/rfc2616.txt
317
318         Wikipedia!
319         http://en.wikipedia.org/wiki/Representational_State_Transfer
320
321         The REST Wiki: http://rest.blueoxen.net/cgi-bin/wiki.pl?FrontPage
322
323 AUTHOR
324         Adam Jacob <adam@stalecoffee.org>, with lots of help from mst and
325         jrockway
326
327         Marchex, Inc. paid me while I developed this module.
328         (http://www.marchex.com)
329
330 LICENSE
331         You may distribute this code under the same terms as Perl itself.
332