-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathREST.pm
More file actions
287 lines (218 loc) · 6.92 KB
/
REST.pm
File metadata and controls
287 lines (218 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package Dancer::Plugin::REST;
use strict;
use warnings;
use Carp 'croak';
use Dancer ':syntax';
use Dancer::Plugin;
our $AUTHORITY = 'SUKRIA';
our $VERSION = '0.07';
my $content_types = {
json => 'application/json',
yml => 'text/x-yaml',
xml => 'application/xml',
};
register prepare_serializer_for_format => sub {
my $conf = plugin_setting;
my $serializers = (
($conf && exists $conf->{serializers})
? $conf->{serializers}
: { 'json' => 'JSON',
'yml' => 'YAML',
'xml' => 'XML',
'dump' => 'Dumper',
}
);
hook 'before' => sub {
my $format = params->{'format'};
return unless defined $format;
my $serializer = $serializers->{$format};
unless (defined $serializer) {
return halt(
Dancer::Error->new(
code => 404,
message => "unsupported format requested: " . $format
)
);
}
set serializer => $serializer;
my $ct = $content_types->{$format} || setting('content_type');
content_type $ct;
};
};
register resource => sub {
my (undef, $resource, %triggers) = plugin_args(@_);
croak "resource should be given with triggers"
unless defined $resource
and defined $triggers{get}
and defined $triggers{update}
and defined $triggers{delete}
and defined $triggers{create};
get "/${resource}/:id.:format" => $triggers{get};
get "/${resource}/:id" => $triggers{get};
put "/${resource}/:id.:format" => $triggers{update};
put "/${resource}/:id" => $triggers{update};
post "/${resource}.:format" => $triggers{create};
post "/${resource}" => $triggers{create};
del "/${resource}/:id.:format" => $triggers{delete};
del "/${resource}/:id" => $triggers{delete};
};
register send_entity => sub {
my (undef, $entity, $http_code) = plugin_args(@_);
$http_code ||= 200;
status($http_code);
$entity;
};
my %http_codes = (
# 1xx
100 => 'Continue',
101 => 'Switching Protocols',
102 => 'Processing',
# 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
207 => 'Multi-Status',
210 => 'Content Different',
# 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found',
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
307 => 'Temporary Redirect',
310 => 'Too many Redirect',
# 4xx
400 => 'Bad Request',
401 => 'Unauthorized',
402 => 'Payment Required',
403 => 'Forbidden',
404 => 'Not Found',
405 => 'Method Not Allowed',
406 => 'Not Acceptable',
407 => 'Proxy Authentication Required',
408 => 'Request Time-out',
409 => 'Conflict',
410 => 'Gone',
411 => 'Length Required',
412 => 'Precondition Failed',
413 => 'Request Entity Too Large',
414 => 'Request-URI Too Long',
415 => 'Unsupported Media Type',
416 => 'Requested range unsatisfiable',
417 => 'Expectation failed',
418 => 'Teapot',
422 => 'Unprocessable entity',
423 => 'Locked',
424 => 'Method failure',
425 => 'Unordered Collection',
426 => 'Upgrade Required',
449 => 'Retry With',
450 => 'Parental Controls',
# 5xx
500 => 'Internal Server Error',
501 => 'Not Implemented',
502 => 'Bad Gateway',
503 => 'Service Unavailable',
504 => 'Gateway Time-out',
505 => 'HTTP Version not supported',
507 => 'Insufficient storage',
509 => 'Bandwidth Limit Exceeded',
);
for my $code (keys %http_codes) {
my $helper_name = lc($http_codes{$code});
$helper_name =~ s/[^\w]+/_/gms;
$helper_name = "status_${helper_name}";
register $helper_name => sub {
my ($self, $msg) = plugin_args(@_);
my @args;
if ($code >= 400) {
@args = ({error => $msg}, $code);
}
else {
@args = ($msg, $code);
}
if ( $self ) {
$self->send_entity(@args);
}
else {
send_entity(@args);
}
};
}
register_plugin for_versions => [ 1, 2 ];
1;
__END__
=pod
=head1 NAME
Dancer::Plugin::REST - A plugin for writing RESTful apps with Dancer
=head1 SYNOPSYS
package MyWebService;
use Dancer;
use Dancer::Plugin::REST;
prepare_serializer_for_format;
get '/user/:id.:format' => sub {
User->find(params->{id});
};
# curl http://mywebservice/user/42.json
{ "id": 42, "name": "John Foo", email: "john.foo@example.com"}
# curl http://mywebservice/user/42.yml
--
id: 42
name: "John Foo"
email: "john.foo@example.com"
=head1 DESCRIPTION
This plugin helps you write a RESTful webservice with Dancer.
=head1 KEYWORDS
=head2 prepare_serializer_for_format
When this pragma is used, a before filter is set by the plugin to automatically
change the serializer when a format is detected in the URI.
That means that each route you define with a B<:format> token will trigger a
serializer definition, if the format is known.
This lets you define all the REST actions you like as regular Dancer route
handlers, without explicitly handling the outgoing data format.
=head2 resource
This keyword lets you declare a resource your application will handle.
resource user =>
get => sub { # return user where id = params->{id} },
create => sub { # create a new user with params->{user} },
delete => sub { # delete user where id = params->{id} },
update => sub { # update user with params->{user} };
# this defines the following routes:
# GET /user/:id
# GET /user/:id.:format
# POST /user
# POST /user.:format
# DELETE /user/:id
# DELETE /user/:id.:format
# PUT /user/:id
# PUT /user/:id.:format
=head2 helpers
Some helpers are available. This helper will set an appropriate HTTP status for you.
=head3 status_ok
status_ok({users => {...}});
Set the HTTP status to 200
=head3 status_created
status_created({users => {...}});
Set the HTTP status to 201
=head3 status_accepted
status_accepted({users => {...}});
Set the HTTP status to 202
=head3 status_bad_request
status_bad_request("user foo can't be found");
Set the HTTP status to 400. This function as for argument a scalar that will be used under the key B<error>.
=head3 status_not_found
status_not_found("users doesn't exists");
Set the HTTP status to 404. This function as for argument a scalar that will be used under the key B<error>.
=head1 LICENCE
This module is released under the same terms as Perl itself.
=head1 AUTHORS
This module has been written by Alexis Sukrieh C<< <sukria@sukria.net> >> and Franck
Cuny.
=head1 SEE ALSO
L<Dancer> L<http://en.wikipedia.org/wiki/Representational_State_Transfer>
=cut