Dancer2::Plugin::RPC::JSON - Dancer Plugin to register jsonrpc2 methods.
In the Controler-bit:
use Dancer2::Plugin::RPC::JSON; jsonrpc '/endpoint' => { publish => 'pod', arguments => ['MyProject::Admin'] };
and in the Model-bit (MyProject::Admin):
package MyProject::Admin; =for jsonrpc rpc.abilities rpc_show_abilities =cut sub rpc_show_abilities { return { # datastructure }; } 1;
This plugin lets one bind an endpoint to a set of modules with the new jsonrpc keyword.
\%publisher_arguments
The callback will be called just before the actual rpc-code is called from the dispatch table. The arguments are positional: (full_request, method_name).
my Dancer2::RPCPlugin::CallbackResult $continue = $callback ? $callback->(request(), $method_name, @method_args) : callback_success();
The callback should return a Dancer2::RPCPlugin::CallbackResult instance:
on_success
callback_success()
on_failure
callback_fail( error_code => <numeric_code>, error_message => <error message> )
The codewrapper will be called with these positional arguments:
The default code_wrapper-sub is:
sub { my $code = shift; my $pkg = shift; $code->(@_); };
The publiser key determines the way one connects the rpc-method name with the actual code.
This way of publishing requires you to create a dispatch-table in the app's config YAML:
plugins: "RPC::JSON": '/endpoint': 'MyProject::Admin': admin.someFunction: rpc_admin_some_function_name 'MyProject::User': user.otherFunction: rpc_user_other_function_name
The Config-publisher doesn't use the arguments value of the %publisher_arguments hash.
arguments
%publisher_arguments
This way of publishing enables one to use a special POD directive =for jsonrpc to connect the rpc-method name to the actual code. The directive must be in the same file as where the code resides.
=for jsonrpc
=for jsonrpc admin.someFunction rpc_admin_some_function_name
The POD-publisher needs the arguments value to be an arrayref with package names in it.
This way of publishing requires you to write your own way of building the dispatch-table. The code_ref you supply, gets the arguments value of the %publisher_arguments hash.
A dispatch-table looks like:
return { 'admin.someFuncion' => dispatch_item( package => 'MyProject::Admin', code => MyProject::Admin->can('rpc_admin_some_function_name'), ), 'user.otherFunction' => dispatch_item( package => 'MyProject::User', code => MyProject::User->can('rpc_user_other_function_name'), ), }
The value of this key depends on the publisher-method chosen.
This special POD-construct is used for coupling the jsonrpc-methodname to the actual sub-name in the current package.
Deserializes the string as Perl-datastructure.
Returns a jsonrpc response as a hashref.
Returns a jsonrpc error response as a hashref.
Creates a (partial) dispatch table from data passed from the (YAML)-config file.
Creates a (partial) dispatch table from data provided in POD.
(c) MMXVI - Abe Timmerman <abeltje@cpan.org>.
To install Dancer2::Plugin::RPC, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer2::Plugin::RPC
CPAN shell
perl -MCPAN -e shell install Dancer2::Plugin::RPC
For more information on module installation, please visit the detailed CPAN module installation guide.