blib/lib/Dancer2/Plugin/RPC/RESTISH.pm

filter: Statementstmt 98.8% 170 / 172 Branchbran 92.0% 46 / 50 Conditioncond 79.3% 46 / 58 MC/DCmcdc 64.7% 22 / 34 Subroutinesub 100% 15 / 15 Podpod 100% 1 / 1 totaltotal 90.9% 300 / 330 CC 50 SCAR 35.4
CC
50
Cov
94%
__ANON__
47.1
restish
5.0
BEGIN
1.0
CRAP 34.4
↑ Summary
1 package Dancer2::Plugin::RPC::RESTISH;
2 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Dancer2::Plugin ;#qw( plugin_keywords );
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 with 'Dancer2::RPCPlugin';
6  
7 3 our $VERSION = '2.02';
8 3 ▶use constant PLUGIN_NAME => 'restish';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 has allow_origin => (
11 is => 'rw',
12 3 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 3 );
14  
15 3 ▶use Dancer2::RPCPlugin::CallbackResultFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Dancer2::RPCPlugin::ErrorResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Dancer2::RPCPlugin::FlattenData;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Dancer2::RPCPlugin::PluginNames;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 Dancer2::RPCPlugin::PluginNames->new->add_names(PLUGIN_NAME);
21 Dancer2::RPCPlugin::ErrorResponse->register_error_responses(
22 PLUGIN_NAME ,=> {
23 -32500 => 500,
24 -32601 => 403,
25 default => 400,
26 },
27 8 ▶ sprintf("as_%s_error", PLUGIN_NAME) => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
28 8 my $self = shift;
29  
30 return {
31 8 error_code => $self->error_code,
32 error_message => $self->error_message,
33 error_data => $self->error_data,
34 };
35 }
36 3 );
37  
38 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use Time::HiRes 'time';
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 # A char between the HTTP-Method and the REST-route
43 3 our $_HM_POSTFIX = '@';
44  
45 3 plugin_keywords PLUGIN_NAME;
46  
47 16 ▶sub restish {
Subroutine restish: called CC=5 SCAR=16.1
Pod documented
48 16 my ($plugin, $endpoint, $arguments) = @_;
49 16 ▶ my $restish_args = $arguments->{plugin_args} || {};
Condition100%2/2
$arguments->{'plugin_args'} || {}
l!l
214
MC/DC100%1/1
$arguments->{'plugin_args'} || {}
$arguments->{'plugin_args'}
Truth table100%2/2
A
A= $arguments->{'plugin_args'}
Aresult
11
00
50  
51 16 ▶ $plugin->allow_origin->{$endpoint} = $restish_args->{cors_allow_origin} || '';
Condition100%2/2
$restish_args->{'cors_allow_origin'} || ''
l!l
214
MC/DC100%1/1
$restish_args->{'cors_allow_origin'} || ''
$restish_args->{'cors_allow_origin'}
Truth table100%2/2
A
A= $restish_args->{'cors_allow_origin'}
Aresult
11
00
52  
53 my $dispatcher = $plugin->dispatch_builder(
54 $endpoint,
55 $arguments->{publish},
56 $arguments->{arguments},
57 16 plugin_setting(),
58 )->();
59  
60 my $lister = $plugin->partial_method_lister(
61 protocol => __PACKAGE__->rpcplugin_tag,
62 endpoint => $endpoint,
63 16 methods => [ sort keys %{ $dispatcher } ],
64 );
65  
66 16 my $code_wrapper = $plugin->code_wrapper($arguments);
67 16 my $callback = $arguments->{callback};
68  
69 16 $plugin->app->log(debug => "Starting restish-handler build: ", $lister);
70 32 ▶ my $handle_call = sub {
Subroutine __ANON__: called CC=46 SCAR=38.5
71 32 my ($dsl) = @_;
72 32 my ($pi) = grep { ref($_) eq __PACKAGE__ } @{ $dsl->plugins };
73  
74 32 my $allow_origin = $pi->allow_origin->{$endpoint};
75 32 my @allowed_origins = split(" ", $allow_origin);
76  
77 # we'll only handle requests that have either a JSON body or no body
78 32 my $http_request = $dsl->app->request;
79 32 ▶ my ($ct) = split(/;\s*/, $http_request->content_type // "", 2);
Condition100%2/2
$http_request->content_type // ""
l!l
626
MC/DC100%1/1
$http_request->content_type // ""
$http_request->content_type
Truth table100%2/2
A
A= $http_request->content_type
Aresult
11
00
80 32 ▶ $ct //= "";
Condition100%2/2
$ct //= ""
l!l
626
MC/DC100%1/1
$ct //= ""
$ct
Truth table100%2/2
A
A= $ct
Aresult
11
00
81  
82 32 ▶ if ($http_request->body && ($ct ne 'application/json')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http_request->body and $ct ne "application/json") 1 31 32
Condition100%3/3
$http_request->body and $ct ne "application/json"
!ll&&!rl&&r
2561
MC/DC100%2/2
$http_request->body and $ct ne "application/json"
$http_request->body $ct ne "application/json"
Truth table100%3/3
A and B
A= $http_request->bodyB= $ct ne "application/json"
ABresult
0X0
100
111
83 1 $dsl->pass();
84 }
85  
86 31 my $http_method = uc($http_request->method);
87 31 my $request_path = $http_request->path;
88  
89 ##### Cross Origin Resource Sharing(CORS)
90 my $has_origin_header = grep {
91 31 lc($_) eq 'origin'
92 } $http_request->headers->header_field_names;
93 31 ▶ my $has_origin = $http_request->header('Origin') || '';
Condition100%2/2
$http_request->header('Origin') || ''
l!l
1120
MC/DC100%1/1
$http_request->header('Origin') || ''
$http_request->header('Origin')
Truth table100%2/2
A
A= $http_request->header('Origin')
Aresult
11
00
94 my $allowed_origin = ($allow_origin eq '*')
95 31 ▶ || grep { $_ eq $has_origin } @allowed_origins;
Condition66%2/3
$allow_origin eq '*' || grep({$_ eq $has_origin;} @allowed_origins)
l!l&&r!l&&!r
10021
MC/DC50%1/2
$allow_origin eq '*' || grep({$_ eq $has_origin;} @allowed_origins)
$allow_origin eq '*' grep({$_ eq $has_origin;} @allowed_origins)
Truth table66%2/3
A || B
A= $allow_origin eq '*'B= grep({$_ eq $has_origin;} @allowed_origins)
ABresult
1X1
011
000
96  
97 31 ▶ my $is_preflight = $has_origin_header && ($http_method eq 'OPTIONS');
Condition100%3/3
$has_origin_header && $http_method eq 'OPTIONS'
!ll&&!rl&&r
2056
MC/DC100%2/2
$has_origin_header && $http_method eq 'OPTIONS'
$has_origin_header $http_method eq 'OPTIONS'
Truth table100%3/3
A && B
A= $has_origin_headerB= $http_method eq 'OPTIONS'
ABresult
0X0
100
111
98 31 ▶ $dsl->app->log(debug => "[RESTISH-CORS] Preflight from $has_origin")
Branch: 1 branch
BranchTrueFalseTotal
if $is_preflight 6 25 31
99 if $is_preflight;
100  
101 # with CORS, we do not allow mismatches on Origin
102 31 ▶ if ($allow_origin && $has_origin && !$allowed_origin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_origin and $has_origin and not $allowed_origin) 1 30 31
Condition83%5/6
$allow_origin and $has_origin and not $allowed_origin
!ll&&!rl&&r
20101
$allow_origin and $has_origin
!ll&&!rl&&r
20011
MC/DC66%2/3
$allow_origin and $has_origin and not $allowed_origin
$allow_origin $has_origin not $allowed_origin
Truth table75%3/4
A and B and not(C)
A= $allow_originB= $has_originC= not $allowed_origin
ABCresult
0XX0
10X0
1100
1111
103 1 $dsl->app->log(
104 debug => "[RESTISH-CORS] '$has_origin' not allowed ($allow_origin)"
105 );
106 1 $dsl->response->status(403);
107 1 $dsl->response->content_type('text/plain');
108 1 return "[CORS] $has_origin not allowed";
109 }
110  
111 # method_name should exist...
112 # we need to turn 'GET@some_resource/:id' into a regex that we can use
113 # to match this request so we know what thing to call...
114 30 (my $method_name = $request_path) =~ s{^$endpoint/}{};
115 30 my ($found_match, $found_method);
116 my @sorted_dispatch_keys = sort {
117 # reverse length of the regex we use to match
118 30 my ($am, $ar) = split(/\b$_HM_POSTFIX/, $a);
119 63 $ar =~ s{/:\w+}{/[^/]+};
120 63 my ($bm, $br) = split(/\b$_HM_POSTFIX/, $b);
121 63 $br =~ s{/:\w+}{/[^/]+};
122 63 length($br) <=> length($ar)
123 } keys %$dispatcher;
124  
125 30 ▶ my $preflight_method = $is_preflight
Branch: 1 branch
BranchTrueFalseTotal
$is_preflight ? : 5 25 30
Condition100%2/2
$http_request->header("Access-Control-Request-Method") // "GET"
l!l
32
MC/DC100%1/1
$http_request->header("Access-Control-Request-Method") // "GET"
$http_request->header("Access-Control-Request-Method")
Truth table100%2/2
A
A= $http_request->header("Access-Control-Request-Method")
Aresult
11
00
126 ? $http_request->header('Access-Control-Request-Method') // 'GET'
127 : undef;
128  
129 30 my $check_for_method;
130 30 for my $plugin_route (@sorted_dispatch_keys) {
131 59 my ($hm, $route) = split(/\b$_HM_POSTFIX/, $plugin_route, 2);
132 59 $hm = uc($hm);
133  
134 59 ▶ if ($allow_origin && $is_preflight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_origin and $is_preflight) { } 14 45 59
Condition100%3/3
$allow_origin and $is_preflight
!ll&&!rl&&r
311414
MC/DC100%2/2
$allow_origin and $is_preflight
$allow_origin $is_preflight
Truth table100%3/3
A and B
A= $allow_originB= $is_preflight
ABresult
0X0
100
111
135 14 $check_for_method = $preflight_method;
136 }
137 else {
138 45 $check_for_method = $http_method;
139 }
140 59 ▶ next if $hm ne $check_for_method;
Branch: 1 branch
BranchTrueFalseTotal
if $hm ne $check_for_method 24 35 59
141  
142 35 (my $route_match = $route) =~ s{:\w+}{[^/]+}g;
143 35 $dsl->app->log(
144 debug => "[restish_find_route($check_for_method)]"
145 . " $method_name, $route ($route_match)"
146 );
147 35 ▶ if ($method_name =~ m{^$route_match$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method_name =~ /^$route_match$/) 29 6 35
148 29 $found_match = $plugin_route;
149 29 $found_method = $hm;
150 29 last;
151 }
152 }
153  
154 30 ▶ if (! $found_match) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_match) 1 29 30
155 1 ▶ if ($allow_origin && $is_preflight) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_origin and $is_preflight) 1 0 1
Condition33%1/3
$allow_origin and $is_preflight
!ll&&!rl&&r
001
MC/DC0%0/2
$allow_origin and $is_preflight
$allow_origin $is_preflight
Truth table33%1/3
A and B
A= $allow_originB= $is_preflight
ABresult
0X0
100
111
156 1 my $msg = "[CORS-preflight] failed for $preflight_method => $request_path";
157 1 $dsl->app->log(debug => $msg);
158 1 $dsl->response->status(200); # maybe 403?
159 1 $dsl->response->content_type('text/plain');
160 1 return $msg;
161 }
162 $dsl->app->log(
163 0 warning => "$http_method => $request_path ($method_name) not found, pass()"
164 );
165 0 $dsl->pass();
166 }
167 $dsl->app->log(
168 29 debug => "[restish_found_route($http_method)]"
169 . " $request_path ($method_name) ($found_match)"
170 );
171  
172 # Send the CORS 'Access-Control-Allow-Origin' header
173 29 ▶ if ($allow_origin && $has_origin_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_origin and $has_origin_header) 9 20 29
Condition66%2/3
$allow_origin and $has_origin_header
!ll&&!rl&&r
2009
MC/DC50%1/2
$allow_origin and $has_origin_header
$allow_origin $has_origin_header
Truth table66%2/3
A and B
A= $allow_originB= $has_origin_header
ABresult
0X0
100
111
174 9 ▶ my $allow_now = $allow_origin eq '*' ? '*' : $has_origin;
Branch: 1 branch
BranchTrueFalseTotal
$allow_origin eq '*' ? : 9 0 9
175 9 $dsl->response->header('Access-Control-Allow-Origin' => $allow_now);
176 }
177  
178 29 ▶ if ($is_preflight) { # Send more CORS headers and return.
Branch: 1 branch
BranchTrueFalseTotal
if ($is_preflight) 4 25 29
179 4 $dsl->app->log(
180 debug => "[CORS] preflight-request: $request_path ($method_name)"
181 );
182 4 $dsl->response->status(204);
183 4 ▶ $dsl->response->header(
Branch: 1 branch
BranchTrueFalseTotal
if $http_request->header("Access-Control-Request-Headers") 0 4 4
184 'Access-Control-Allow-Headers',
185 $http_request->header('Access-Control-Request-Headers')
186 ) if $http_request->header('Access-Control-Request-Headers');
187  
188 4 $dsl->response->header('Access-Control-Allow-Methods' => $found_method);
189 4 return "";
190 }
191  
192 25 $dsl->response->content_type ('application/json');
193 25 ▶ my $method_args = $http_request->body
Branch: 1 branch
BranchTrueFalseTotal
$http_request->body ? : 6 19 25
194 ? from_json($http_request->body)
195 : { };
196 25 ▶ my $route_args = $http_request->params('route') // { };
Condition50%1/2
$http_request->params("route") // {}
l!l
250
MC/DC0%0/1
$http_request->params("route") // {}
$http_request->params("route")
Truth table50%1/2
A
A= $http_request->params("route")
Aresult
11
00
197 25 ▶ my $query_args = $http_request->params('query') // { };
Condition50%1/2
$http_request->params("query") // {}
l!l
025
MC/DC0%0/1
$http_request->params("query") // {}
$http_request->params("query")
Truth table50%1/2
A
A= $http_request->params("query")
Aresult
11
00
198  
199 # We'll merge method_args and route_args, where route_args win:
200 25 $method_args = {
201 %$method_args,
202 %$route_args,
203 %$query_args,
204 };
205 25 $dsl->app->log(
206 debug => "[handle_restish_request('$request_path' via '$found_match')] "
207 , $method_args
208 );
209  
210 25 my $start_request = time();
211 25 my $continue = eval {
212 25 (my $match_re = $found_match) =~ s{:\w+}{[^/]+}g;
213 25 local $Dancer2::RPCPlugin::ROUTE_INFO = {
214 plugin => PLUGIN_NAME,
215 route_matched => $found_match,
216 matched_re => $match_re,
217 endpoint => $endpoint,
218 rpc_method => $method_name,
219 full_path => $http_request->path,
220 http_method => $http_method,
221 };
222 25 ▶ $callback
Branch: 1 branch
BranchTrueFalseTotal
$callback ? : 8 17 25
223 ? $callback->($http_request, $method_name, $method_args)
224 : callback_success();
225 };
226 25 my $error = $@;
227 25 my $response;
228 25 ▶ if ($error) {
Branch: 3 branches
BranchTrueFalseTotal
if ($error) { } 1 24 25
elsif (not builtin::blessed($continue) && $continue->can('does') && $continue->does('Dancer2::RPCPlugin::CallbackResult')) { } 1 23 24
elsif (builtin::blessed($continue) and not $continue->success) { } 2 21 23
229 1 my $error_response = error_response(
230 error_code => -32500,
231 error_message => $error,
232 error_data => $method_args,
233 );
234 1 $dsl->response->status($error_response->return_status(PLUGIN_NAME));
235 1 $response = $error_response->as_restish_error;
236 }
237 ▶ elsif ( !blessed($continue)
Condition66%4/6
builtin::blessed($continue) && $continue->can('does') && $continue->does('Dancer2::RPCPlugin::CallbackResult')
!ll&&!rl&&r
1023
builtin::blessed($continue) and $continue->can("does")
!ll&&!rl&&r
0123
MC/DC33%1/3
builtin::blessed($continue) && $continue->can('does') && $continue->does('Dancer2::RPCPlugin::CallbackResult')
builtin::blessed($continue) $continue->can("does") $continue->does('Dancer2::RPCPlugin::CallbackResult')
Truth table50%2/4
A and B && C
A= builtin::blessed($continue)B= $continue->can("does")C= $continue->does('Dancer2::RPCPlugin::CallbackResult')
ABCresult
0XX0
10X0
1100
1111
238 || !$continue->can('does')
239 || !$continue->does('Dancer2::RPCPlugin::CallbackResult'))
240 {
241 1 my $error_response = error_response(
242 error_code => -32603,
243 error_message => "Internal error: 'callback_result' wrong class "
244 . blessed($continue),
245 error_data => $method_args,
246 );
247 1 $dsl->response->status($error_response->return_status(PLUGIN_NAME));
248 1 $response = $error_response->as_restish_error;
249 }
250 ▶ elsif (blessed($continue) && !$continue->success) {
Condition66%2/3
builtin::blessed($continue) and not $continue->success
!ll&&!rl&&r
0212
MC/DC50%1/2
builtin::blessed($continue) and not $continue->success
builtin::blessed($continue) not $continue->success
Truth table66%2/3
A and not(B)
A= builtin::blessed($continue)B= not $continue->success
ABresult
0X0
100
111
251 2 my $error_response = error_response(
252 error_code => $continue->error_code,
253 error_message => $continue->error_message,
254 error_data => $method_args,
255 );
256 2 $dsl->response->status($error_response->return_status(PLUGIN_NAME));
257 2 $response = $error_response->as_restish_error;
258 }
259 else {
260 21 my $di = $dispatcher->{$found_match};
261 21 my $handler = $di->code;
262 21 my $package = $di->package;
263  
264 21 $response = eval {
265 21 $code_wrapper->($handler, $package, $method_name, $method_args);
266 };
267 21 my $error = $@;
268  
269 21 $dsl->app->log(debug => "[handled_restish_response($method_name)] ", $response);
270 21 $dsl->app->log(
271 info => sprintf(
272 "[RPC::RESTISH] request for '%s' took %.4fs",
273 $request_path, time() - $start_request
274 )
275 );
276  
277 21 ▶ if (my $error = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 3 18 21
278 3 ▶ my $error_response = blessed($error) && $error->can('as_restish_error')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($error) && $error->can('as_restish_error') ? : 1 2 3
Condition66%2/3
builtin::blessed($error) and $error->can("as_restish_error")
!ll&&!rl&&r
201
MC/DC50%1/2
builtin::blessed($error) and $error->can("as_restish_error")
builtin::blessed($error) $error->can("as_restish_error")
Truth table66%2/3
A and B
A= builtin::blessed($error)B= $error->can("as_restish_error")
ABresult
0X0
100
111
279 ? $error
280 : error_response(
281 error_code => 500,
282 error_message => $error,
283 error_data => $method_args,
284 );
285 3 $dsl->response->status($error_response->return_status(PLUGIN_NAME));
286 3 $response = $error_response->as_restish_error;
287 }
288 21 ▶ if (blessed($response) && $response->can('as_restish_error')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($response) and $response->can('as_restish_error')) { } 1 20 21
elsif (builtin::blessed($response)) { } 1 19 20
Condition100%3/3
builtin::blessed($response) and $response->can("as_restish_error")
!ll&&!rl&&r
1911
MC/DC100%2/2
builtin::blessed($response) and $response->can("as_restish_error")
builtin::blessed($response) $response->can("as_restish_error")
Truth table100%3/3
A and B
A= builtin::blessed($response)B= $response->can("as_restish_error")
ABresult
0X0
100
111
289 1 $dsl->response->status($response->return_status(PLUGIN_NAME));
290 1 $response = $response->as_restish_error;
291 }
292 elsif (blessed($response)) {
293 1 $response = flatten_data($response);
294 }
295 21 $dsl->app->log(debug => "[handled_restish_response($request_path)] ", $response);
296 }
297 25 my $jsonise_options = {canonical => 1};
298 25 ▶ if ($dsl->config->{encoding} && $dsl->config->{encoding} =~ m{^utf-?8$}i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsl->config->{"encoding"} and $dsl->config->{"encoding"} =~ /^utf-?8$/i) 13 12 25
Condition66%2/3
$dsl->config->{"encoding"} and $dsl->config->{"encoding"} =~ /^utf-?8$/i
!ll&&!rl&&r
12013
MC/DC50%1/2
$dsl->config->{"encoding"} and $dsl->config->{"encoding"} =~ /^utf-?8$/i
$dsl->config->{"encoding"} $dsl->config->{"encoding"} =~ /^utf-?8$/i
Truth table66%2/3
A and B
A= $dsl->config->{"encoding"}B= $dsl->config->{"encoding"} =~ /^utf-?8$/i
ABresult
0X0
100
111
299 13 $jsonise_options->{utf8} = 1;
300 }
301  
302 # non-refs will be send as-is
303 25 ▶ return ref($response)
Branch: 1 branch
BranchTrueFalseTotal
ref $response ? : 24 1 25
304 ? to_json($response, $jsonise_options)
305 : $response;
306 16 };
307  
308 16 $plugin->app->log(debug => "Setting routes (restish): $endpoint ", $lister);
309 # split the keys in $dispatcher so we can register methods for all
310 16 for my $dispatch_route (keys %$dispatcher) {
311 25 my ($hm, $route) = split(/$_HM_POSTFIX/, $dispatch_route, 2);
312 25 my $dancer_route = "$endpoint/$route";
313 25 $plugin->app->log(debug => "[restish] registering `$hm $dancer_route`");
314 25 $plugin->app->add_route(
315 method => lc($hm),
316 regexp => $dancer_route,
317 code => $handle_call,
318 );
319 25 ▶ $plugin->app->add_route(
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->allow_origin 25 0 25
320 method => 'options',
321 regexp => $dancer_route,
322 code => $handle_call
323 ) if $plugin->allow_origin;
324 }
325  
326 };
327  
328 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
329 3 1;
330  
331 =head1 NAME
332  
333 Dancer::Plugin::RPC::RESTISH - Simple plugin to implement a restish interface.
334  
335 =head1 SYNOPSIS
336  
337 In the Controler-bit:
338  
339 use Dancer::Plugin::RPC::RESTISH;
340 restish '/endpoint' => {
341 publish => 'pod',
342 arguments => ['MyProject::Admin'],
343 plugin_args => {
344 cors_allow_origin => '*',
345 },
346 };
347  
348 and in the Model-bit (B<MyProject::Admin>):
349  
350 package MyProject::Admin;
351
352 =for restish GET@ability/:id rpc_get_ability_details
353
354 =cut
355
356 sub rpc_get_ability_details {
357 my %args = @_; # contains: {"id": 42}
358 return {
359 # datastructure
360 };
361 }
362 1;
363  
364 =head1 DESCRIPTION
365  
366 RESTISH is an implementation of REST that lets you bind routes to code in the
367 style the rest of L<Dancer::Plugin::RPC> modules do. One must realise that this
368 basically binds REST-paths to RPC-methods (that's not ideal, but saves a lot of
369 code).
370  
371 B<This version only supports JSON as data serialisation>.
372  
373 =head2 restish '/base_path' => \%publisher_arguments
374  
375 See L<Dancer::Plugin::RPC>, L<Dancer::Plugin::RPC::JSONRPC>,
376 L<Dancer::Plugin::RPC::RESTRPC>, L<Dancer::Plugin::RPC::XMLRPC> for more
377 information about the C<%publisher_arguments>.
378  
379 =head2 Implement the routes for RESTISH
380  
381 The plugin registers Dancer-C<any> route-handlers for the C<base_path> +
382 C<method_path> and the route-handler looks for a data-handler that matches the path
383 and HTTP-method.
384  
385 Method-paths can contain colon-prefixed parameters native to Dancer. These
386 parameters will be merged with the content-parameters and the query-parameters
387 into a single hash which will be passed to the code as the parameters.
388  
389 Method-paths are prefixed by a HTTP-method followed by B<@>:
390  
391 =over
392  
393 =item publisher => 'config'
394  
395 plugins:
396 'RPC::RESTISH':
397 '/rest':
398 'MyProject::Admin':
399 'GET@resources': 'get_all_resourses'
400 'POST@resource': 'create_resource'
401 'GET@resource/:id': 'get_resource'
402 'PATCH@resource/:id': 'update_resource'
403 'DELETE@resource/:id': 'delete_resource'
404  
405 =item publisher => 'pod'
406  
407 =for restish GET@resources get_all_resources /rest
408 =for restish POST@resource create_resource /rest
409 =for restish GET@resource/:id get_resource /rest
410 =for restish PATCH@resource/:id update_resource /rest
411 =for restish DELETE@resource/:id delete_resource /rest
412  
413 The third argument (the base_path) is optional.
414  
415 =back
416  
417 The plugin for RESTISH also adds 2 fields to C<$Dancer2::RPCPlugin::ROUTE_INFO>:
418  
419 local $Dancer2::RPCPlugin::ROUTE_INFO = {
420 plugin => PLUGIN_NAME,
421 endpoint => $endpoint,
422 rpc_method => $method_name,
423 full_path => request->path,
424 http_method => $http_method,
425 # These two are added
426 route_matched => $found_match, # PATCH@resource/:id
427 matched_re => $match_re, # PATCH@resource/[^/]+
428 };
429  
430 =head2 CORS (Cross-Origin Resource Sharing)
431  
432 If one wants the service to be directly called from javascript in a browser, one
433 has to consider CORS as browsers enforce that. This means that the actual
434 request is preceded by what's called a I<preflight request> that uses the
435 HTTP-method B<OPTIONS> with a number of header-fields.
436  
437 =over
438  
439 =item Origin
440  
441 =item Access-Control-Request-Method
442  
443 =back
444  
445 The plugin supports considering these CORS requests, by special casing these
446 B<OPTIONS> requests and always sending the C<Access-Control-Allow-Origin> header
447 as set in the config options.
448  
449 =head3 cors_allow_origin => $list_of_urls | '*'
450  
451 If left out, no attempt to honour a CORS B<OPTIONS> request will be done and the
452 request will be passed.
453  
454 When set to a value, the B<OPTIONS> request will be executed, for any http-method in
455 the C<Access-Control-Request-Method> header. The response to the B<OPTIONS>
456 request will also contain every C<Access-Control-Allow-*> header that was
457 requested as C<Access-Control-Request-*> header.
458  
459 When set, all responses will contain the C<Access-Control-Allow-Origin>-header
460 with either C<*> if that was set, or the value of the actual C<Origin>-header
461 that was passed and equals one the preset values.
462  
463 =head1 INTERNAL
464  
465 =head2 Attributes
466  
467 =over
468  
469 =item B<allow_origin>
470  
471 Where do we allow Origin to be from.
472  
473 =back
474  
475 =head2 build_dispatcher_from_config
476  
477 Creates a (partial) dispatch table from data passed from the (YAML)-config file.
478  
479 =head2 build_dispatcher_from_pod
480  
481 Creates a (partial) dispatch table from data provided in POD.
482  
483 =begin pod-coverage
484  
485 =head2 PLUGIN_NAME
486  
487 The name for this plugin.
488  
489 =end pod-coverage
490  
491 =head1 LICENSE
492  
493 This library is free software; you can redistribute it and/or modify
494 it under the same terms as Perl itself.
495  
496 See:
497  
498 =over 4
499  
500 =item * L<http://www.perl.com/perl/misc/Artistic.html>
501  
502 =item * L<http://www.gnu.org/copyleft/gpl.html>
503  
504 =back
505  
506 This program is distributed in the hope that it will be useful,
507 but WITHOUT ANY WARRANTY; without even the implied warranty of
508 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
509  
510 =head1 COPYRIGHT
511  
512 E<copy> MMXX - Abe Timmerman <abeltje@cpan.org>
513  
514 =cut
↑ Summary