File Coverage

blib/lib/Dancer/Plugin/RPC/RESTRPC.pm
Criterion Covered Total %
statement 92 94 97.8
branch 23 26 88.4
condition 9 11 81.8
subroutine 17 17 100.0
pod 2 2 100.0
total 143 150 95.3


line stmt bran cond sub pod time code
1             package Dancer::Plugin::RPC::RESTRPC;
2 5     5   528923 use v5.10;
  5         33  
3 5     5   25 use Dancer ':syntax';
  5         11  
  5         24  
4 5     5   1898 use Dancer::Plugin;
  5         2274  
  5         275  
5 5     5   27 use Scalar::Util 'blessed';
  5         8  
  5         220  
6              
7 5     5   838 no if $] >= 5.018, warnings => 'experimental::smartmatch';
  5         24  
  5         30  
8              
9 5     5   756 use Dancer::RPCPlugin::CallbackResult;
  5         14  
  5         273  
10 5     5   1509 use Dancer::RPCPlugin::ErrorResponse;
  5         13  
  5         232  
11 5     5   486 use Dancer::RPCPlugin::DispatchFromConfig;
  5         11  
  5         187  
12 5     5   502 use Dancer::RPCPlugin::DispatchFromPod;
  5         16  
  5         281  
13 5     5   42 use Dancer::RPCPlugin::DispatchItem;
  5         12  
  5         189  
14 5     5   1086 use Dancer::RPCPlugin::DispatchMethodList;
  5         38  
  5         200  
15 5     5   678 use Dancer::RPCPlugin::FlattenData;
  5         9  
  5         4169  
16              
17             my %dispatch_builder_map = (
18             pod => \&build_dispatcher_from_pod,
19             config => \&build_dispatcher_from_config,
20             );
21              
22             register restrpc => sub {
23 13     13   33981 my($self, $base_url, $arguments) = plugin_args(@_);
24              
25 13         71 my $publisher;
26 13   100     58 given ($arguments->{publish} // 'config') {
27 13         41 when (exists $dispatch_builder_map{$_}) {
28 4         10 $publisher = $dispatch_builder_map{$_};
29              
30 4 100       21 $arguments->{arguments} = plugin_setting() if $_ eq 'config';
31             }
32 9         17 default {
33 9         27 $publisher = $_;
34             }
35             }
36 13         75 my $dispatcher = $publisher->($arguments->{arguments}, $base_url);
37              
38 13         127 my $lister = Dancer::RPCPlugin::DispatchMethodList->new();
39             $lister->set_partial(
40             protocol => 'restrpc',
41             endpoint => $base_url,
42 13         36 methods => [ sort keys %{ $dispatcher } ],
  13         88  
43             );
44              
45             my $code_wrapper = $arguments->{code_wrapper}
46             ? $arguments->{code_wrapper}
47             : sub {
48 5     5   10 my $code = shift;
49 5         10 my $pkg = shift;
50 5         18 $code->(@_);
51 13 100       91 };
52 13         33 my $callback = $arguments->{callback};
53              
54 13         60 debug("Starting restrpc-handler build: ", $lister);
55             my $handle_call = sub {
56 11 100   11   78375 if (request->content_type ne 'application/json') {
57 1         16 pass();
58             }
59 10         141 debug("[handle_restrpc_request] Processing: ", request->body);
60              
61             # method_name should exist
62 10         1613 my ($method_name) = request->path =~ m{$base_url/(\w+)};
63 10 50       276 if (! exists $dispatcher->{$method_name}) {
64 0         0 warning("$base_url/#$method_name not found, pass()");
65 0         0 pass();
66             }
67              
68 10         42 content_type 'application/json';
69 10         833 my $response;
70 10 50       29 my $method_args = request->body
71             ? from_json(request->body)
72             : undef;
73 10         782 my Dancer::RPCPlugin::CallbackResult $continue = eval {
74 10 100       44 $callback
75             ? $callback->(request(), $method_name, $method_args)
76             : callback_success();
77             };
78              
79 10 100 66     211 if (my $error = $@) {
    100 66        
    100          
80 1         9 $response = Dancer::RPCPlugin::ErrorResponse->new(
81             error_code => 500,
82             error_message => $error,
83             )->as_restrpc_error;
84             }
85             elsif (!blessed($continue) || !$continue->isa('Dancer::RPCPlugin::CallbackResult')) {
86 1         12 $response = Dancer::RPCPlugin::ErrorResponse->new(
87             error_code => 500,
88             error_message => "Internal error: 'callback_result' wrong class " . blessed($continue),
89             )->as_restrpc_error;
90             }
91             elsif (blessed($continue) && !$continue->success) {
92 1         8 $response = Dancer::RPCPlugin::ErrorResponse->new(
93             error_code => $continue->error_code,
94             error_message => $continue->error_message,
95             )->as_restrpc_error;
96             }
97             else {
98 7         20 my Dancer::RPCPlugin::DispatchItem $di = $dispatcher->{$method_name};
99 7         31 my $handler = $di->code;
100 7         28 my $package = $di->package;
101              
102 7         15 $response = eval {
103 7         28 $code_wrapper->($handler, $package, $method_name, $method_args);
104             };
105              
106 7         73 debug("[handling_jsonrpc_response($method_name)] ", $response);
107 7 100       800 if (my $error = $@) {
108 2         18 $response = Dancer::RPCPlugin::ErrorResponse->new(
109             error_code => 500,
110             error_message => $error,
111             )->as_restrpc_error;
112             }
113 7 100 100     66 if (blessed($response) && $response->can('as_restrpc_error')) {
    100          
114 1         5 $response = $response->as_restrpc_error;
115             }
116             elsif (blessed($response)) {
117 1         5 $response = flatten_data($response);
118             }
119             }
120 10 50       41 $response = { result => $response } if !ref($response);
121 10         44 return to_json($response);
122 13         1878 };
123              
124 13         66 debug("setting routes (restrpc): $base_url ", $lister);
125 13         1253 for my $call (keys %{ $dispatcher }) {
  13         46  
126 18         1542 my $endpoint = "$base_url/$call";
127 18         59 post $endpoint, $handle_call;
128             }
129             };
130              
131             sub build_dispatcher_from_pod {
132 3     3 1 12 my ($pkgs, $endpoint) = @_;
133 3         13 debug("[build_dispatcher_from_pod]");
134 3         34 return dispatch_table_from_pod(
135             plugin => 'restrpc',
136             packages => $pkgs,
137             endpoint => $endpoint,
138             );
139             }
140              
141             sub build_dispatcher_from_config {
142 1     1 1 3 my ($config, $endpoint) = @_;
143 1         5 debug("[build_dispatcher_from_config] ");
144              
145 1         90 return dispatch_table_from_config(
146             plugin => 'restrpc',
147             config => $config,
148             endpoint => $endpoint,
149             );
150             }
151              
152             register_plugin();
153             true;
154              
155             =head1 NAME
156              
157             Dancer::Plugin::RPC::RESTRPC - RESTRPC Plugin for Dancer
158              
159             =head2 SYNOPSIS
160              
161             In the Controler-bit:
162              
163             use Dancer::Plugin::RPC::RESTRPC;
164             restrpc '/base_url' => {
165             publish => 'pod',
166             arguments => ['MyProject::Admin']
167             };
168              
169             and in the Model-bit (B<MyProject::Admin>):
170              
171             package MyProject::Admin;
172            
173             =for restrpc rpc_abilities rpc_show_abilities
174            
175             =cut
176            
177             sub rpc_show_abilities {
178             return {
179             # datastructure
180             };
181             }
182             1;
183              
184             =head1 DESCRIPTION
185              
186             RESTRPC is a simple protocol that uses HTTP-POST to post a JSON-string (with
187             C<Content-Type: application/json> to an endpoint. This endpoint is the
188             C<base_url> concatenated with the rpc-method name.
189              
190             This plugin lets one bind a base_url to a set of modules with the new B<restrpc> keyword.
191              
192             =head2 restrpc '/base_url' => \%publisher_arguments;
193              
194             =head3 C<\%publisher_arguments>
195              
196             =over
197              
198             =item callback => $coderef [optional]
199              
200             The callback will be called just before the actual rpc-code is called from the
201             dispatch table. The arguments are positional: (full_request, method_name).
202              
203             my Dancer::RPCPlugin::CallbackResult $continue = $callback
204             ? $callback->(request(), $method_name, @method_args)
205             : callback_success();
206              
207             The callback should return a L<Dancer::RPCPlugin::CallbackResult> instance:
208              
209             =over 8
210              
211             =item * on_success
212              
213             callback_success()
214              
215             =item * on_failure
216              
217             callback_fail(
218             error_code => <numeric_code>,
219             error_message => <error message>
220             )
221              
222             =back
223              
224             =item code_wrapper => $coderef [optional]
225              
226             The codewrapper will be called with these positional arguments:
227              
228             =over 8
229              
230             =item 1. $call_coderef
231              
232             =item 2. $package (where $call_coderef is)
233              
234             =item 3. $method_name
235              
236             =item 4. @arguments
237              
238             =back
239              
240             The default code_wrapper-sub is:
241              
242             sub {
243             my $code = shift;
244             my $pkg = shift;
245             my $method = shift;
246             $code->(@_);
247             };
248              
249             =item publisher => <config | pod | \&code_ref>
250              
251             The publiser key determines the way one connects the rpc-method name with the actual code.
252              
253             =over
254              
255             =item publisher => 'config'
256              
257             This way of publishing requires you to create a dispatch-table in the app's config YAML:
258              
259             plugins:
260             "RPC::RESTRPC":
261             '/base_url':
262             'MyProject::Admin':
263             admin.someFunction: rpc_admin_some_function_name
264             'MyProject::User':
265             user.otherFunction: rpc_user_other_function_name
266              
267             The Config-publisher doesn't use the C<arguments> value of the C<%publisher_arguments> hash.
268              
269             =item publisher => 'pod'
270              
271             This way of publishing enables one to use a special POD directive C<=for restrpc>
272             to connect the rpc-method name to the actual code. The directive must be in the
273             same file as where the code resides.
274              
275             =for restrpc admin_someFunction rpc_admin_some_function_name
276              
277             The POD-publisher needs the C<arguments> value to be an arrayref with package names in it.
278              
279             =item publisher => \&code_ref
280              
281             This way of publishing requires you to write your own way of building the dispatch-table.
282             The code_ref you supply, gets the C<arguments> value of the C<%publisher_arguments> hash.
283              
284             A dispatch-table looks like:
285              
286             return {
287             'admin_someFuncion' => dispatch_item(
288             package => 'MyProject::Admin',
289             code => MyProject::Admin->can('rpc_admin_some_function_name'),
290             ),
291             'user_otherFunction' => dispatch_item(
292             package => 'MyProject::User',
293             code => MyProject::User->can('rpc_user_other_function_name'),
294             ),
295             }
296              
297             =back
298              
299             =item arguments => <anything>
300              
301             The value of this key depends on the publisher-method chosen.
302              
303             =back
304              
305             =head2 =for restrpc restrpc-method-name sub-name
306              
307             This special POD-construct is used for coupling the restrpc-methodname to the
308             actual sub-name in the current package.
309              
310             =head1 INTERNAL
311              
312             =head2 build_dispatcher_from_config
313              
314             Creates a (partial) dispatch table from data passed from the (YAML)-config file.
315              
316             =head2 build_dispatcher_from_pod
317              
318             Creates a (partial) dispatch table from data provided in POD.
319              
320             =head1 COPYRIGHT
321              
322             (c) MMXVII - Abe Timmerman <abeltje@cpan.org>
323              
324             =cut