package MojoX::JSON::RPC;
use strict;
our $VERSION = '0.13';
1;
__END__
=head1 NAME
MojoX::JSON::RPC - a Perl implementation of the JSON-RPC 2.0 protocol for Mojolicious
=head1 SYNOPSIS
Server
lugin::JsonRpcDispatcher>):
#!/usr/bin/env perl
use Mojolicious::Lite;
use MojoX::JSON::RPC::Service;
plugin 'json_rpc_dispatcher' => {
services => {
'/jsonrpc' =
> MojoX::JSON::RPC::Service->new->register(
'sum',
sub {
my @params = @_;
my $sum = 0;
$sum += $_ for @pa
MojoX::JSON::RPC::Dispatcher;
use Mojo::Base 'Mojolicious::Controller';
use Mojo::JSON qw(decode_json);
use MojoX::JSON::RPC::Dispatcher::Method;
use MojoX::JSON::RPC::Service;
# process JSON-RPC ca
: q{}
)
);
$res->headers->content_type('application/json-rpc');
return $self->render(json => $rpc_response);
}
sub _acquire_methods {
my ($self) = @_;
my $log
st = decode_json( $req->body ); 1; } or $decode_error = $@;
if ( $decode_error ) {
$log->debug( 'REQUEST: JSON error> ' . $decode_error );
return MojoX::JSON::RPC::Disp
package MojoX::JSON::RPC::Service;
use Mojo::Base -base;
{
# Store rpc methods registered by register_rpc_method_names
my $_rpcs = undef;
sub new {
my $class = shift;
m
1;
__END__
=head1 NAME
MojoX::JSON::RPC::Service - JSON RPC Service registration
=head1 SYNOPSIS
use MojoX::JSON::RPC::Service;
my $svc = MojoX::JSON::RPC::Service->new;
$svc->re
'json_rpc_dispatcher',
services => {
'/jsonrpc' => $svc,
}
);
This package can also be used as a base class to make it easy to create object-oriented
JSON-RPC a
package MojoX::JSON::RPC::Client;
use Mojo::Base -base;
use Mojo::JSON qw(encode_json decode_json);
use Mojo::UserAgent;
has id => undef;
has ua => sub { Mojo::UserAgent->new };
e => 'application/json';
has tx => undef; # latest transaction
sub call {
my ( $self, $uri, $body, $callback ) = @_;
# body might be json already, only enc
ody : @{$body} ) {
$o->{version} ||= $self->version;
}
$body = encode_json($body);
}
else {
$body ||= q{};
}
# Always POST if $body is not empty!
package MojoX::JSON::RPC::Dispatcher::Method;
use Mojo::Base -base;
has id => undef;
has method => undef;
has params => undef;
has result => undef;
has is_not
rror( -32700, 'Parse error.', $msg );
}
sub response {
my ($self) = @_;
return {
jsonrpc => '2.0',
$self->is_notification ? () : ( id => $self->id ),
$self->has_error
NAME
MojoX::JSON::RPC::Dispatcher::Method - The data holder between RPC requests and responses.
=head1 SYNOPSIS
use MojoX::JSON::RPC::Dispatcher::Method;
my $meth = MojoX::JSON::RPC::Dispa
=head1 NAME
MojoX::JSON::RPC - a Perl implementation of the JSON-RPC 2.0 protocol for Mojolicious
=head1 SYNOPSIS
Server as a plugin (L<Mojolicious::Plugin::JsonRpcDispatcher>):
#!/usr/bin
olicious::Lite;
use MojoX::JSON::RPC::Service;
plugin 'json_rpc_dispatcher' => {
services => {
'/jsonrpc' => MojoX::JSON::RPC::Service->new->register(
joX::JSON::RPC::Client>):
#!/usr/bin/env perl
use MojoX::JSON::RPC::Client;
my $client = MojoX::JSON::RPC::Client->new;
my $url = 'http://www.example.com/jsonrpc';
package Mojolicious::Plugin::JsonRpcDispatcher;
use Mojo::Base 'Mojolicious::Plugin';
use MojoX::JSON::RPC::Dispatcher;
sub register {
my ( $self, $app, $conf ) = @_;
my $r = $app->routes;
'Dispatcher#call',
service => $svc,
namespace => 'MojoX::JSON::RPC',
exists $conf->{exception_handler}
? ( exception_handler =>
ious::Plugin::JsonRpcDispatcher - Plugin to allow Mojolicious to act as a JSON-RPC server.
=head1 SYNOPSIS
# lib/your-application.pm
use base 'Mojolicious';
use MojoX::JSON::RPC::Servic