NAME

Mojolicious::Plugin::JSONRPC2 - JSON RPC 2.0 over HTTP

SYNOPSIS

  use JSON::RPC2::Server;

# in Mojolicious app
sub startup {
  my $app = shift;
  $app->plugin('JSONRPC2');

  my $server = JSON::RPC2::Server->new();

  $r->jsonrpc2('/rpc', $server);
  $r->jsonrpc2_get('/rpc', $server)->over(headers => { $app->jsonrpc2_headers });

DESCRIPTION

Mojolicious::Plugin::JSONRPC2 is a plugin that allow you to handle some routes in Mojolicious app using JSON RPC 2.0 over HTTP protocol.

Implements this spec: http://www.simple-is-better.org/json-rpc/transport_http.html. The "pipelined Requests/Responses" is not supported yet.

INTERFACE

$r->jsonrpc2($path, $server)

Add handler for JSON RPC 2.0 over HTTP protocol on $path (with format=>0) using POST method.

RPC functions registered with $server will be called only with their own parameters (provided with RPC request) - if they will need access to Mojolicious app you'll have to provide it manually (using global vars or closures).

$r->jsonrpc2_get($path, $server_safe_idempotent)

WARNING! In most cases you don't need it. In other cases usually you'll have to use different $server objects for POST and GET because using GET you can provide only safe and idempotent RPC functions (because of GET semantic, caching/proxies, etc.).

Add handler for JSON RPC 2.0 over HTTP protocol on $path (with format=>0) using GET method.

RPC functions registered with $server_safe_idempotent will be called only with their own parameters (provided with RPC request) - if they will need access to Mojolicious app you'll have to provide it manually (using global vars or closures).

$r->over(headers => { $app->jsonrpc2_headers })

You can use this condition to distinguish between JSON RPC 2.0 and other request types on same $path - for example if you want to serve web page and RPC on same url you can do this:

my $r = $app->routes;
$r->jsonrpc2_get('/', $server)->over(headers=>{$app->jsonrpc2_headers});
$r->get('/')->to('controller#action');

If you don't use this condition and plugin's handler will get request with wrong headers it will reply with 415 Unsupported Media Type.

OPTIONS

Mojolicious::Plugin::JSONRPC2 has no options.

METHODS

Mojolicious::Plugin::JSONRPC2 inherits all methods from Mojolicious::Plugin and implements the following new ones.

register

$plugin->register(Mojolicious->new);

Register hooks in Mojolicious application.

SEE ALSO

JSON::RPC2::Server, Mojolicious, MojoX::JSONRPC2::HTTP.

BUGS AND LIMITATIONS

No bugs have been reported.

SUPPORT

Please report any bugs or feature requests through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Mojolicious-Plugin-JSONRPC2. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

You can also look for information at:

AUTHOR

Alex Efros <powerman@cpan.org>

LICENSE AND COPYRIGHT

Copyright 2014 Alex Efros <powerman@cpan.org>.

This program is distributed under the MIT (X11) License: http://www.opensource.org/licenses/mit-license.php

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.