NAME

MojoX::JSON::RPC::Client - JSON RPC client

SYNOPSIS

use MojoX::JSON::RPC::Client;

my $client = MojoX::JSON::RPC::Client->new;
my $url    = 'http://www.example.com/jsonrpc/API';
my $callobj = {
    id      => 1,
    method  => 'sum',
    params  => [ 17, 25 ]
};

my $res = $client->call($url, $callobj);

if($res) {
    if ($res->is_error) { # RPC ERROR
        print 'Error : ', $res->error_message;
    }
    else {
        print $res->result;
    }
}
else {
    my $tx_res = $client->tx->res; # Mojo::Message::Response object
    print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
}

Non-blocking:

$client->call($url, $callobj, sub {
    # With callback
    my $res = pop;

    # ... process result ...

    Mojo::IOLoop->stop;
});

Mojo::IOLoop->start;

Easy access:

my $proxy = $client->prepare($uri, ['sum', 'echo']);

print $proxy->sum(10, 23);

DESCRIPTION

A JSON-RPC client.

ATTRIBUTES

MojoX::JSON::RPC::Client implements the following attributes.

id

Id used for JSON-RPC requests. Used when no id is provided as request parameter.

ua

Mojo::UserAgent object.

json

Mojo::JSON object for encoding and decoding.

version

JSON-RPC version. Defaults to 2.0.

content_type

Content type. Defaults to application/json.

tx

Mojo::Transaction object of last request.

METHODS

MojoX::JSON::RPC::Client inherits all methods from Mojo::Base and implements the following new ones.

new

Creates new MojoX::JSON::RPC::Client object.

my $client = MojoX::JSON::RPC::Client->new;

call

Execute JSON-RPC call. Returns MojoX::JSON::RPC::CLient::ReturnObject if RPC call is executed correctly.

my $client = MojoX::JSON::RPC::Client->new;
my $url    = 'http://www.example.com/jsonrpc/API';
my $callobj = {
    id      => 1,
    method  => 'sum',
    params  => [ 17, 25 ]
};

my $res = $client->call($url, $callobj);
if($res) {
    if ($res->is_error) { # RPC error
        print 'Error : ', $res->error_message;
    }
    else {
        print $res->result;
    }
}
else {
    my $tx_res = $client->tx->res; # Mojo::Message::Response object
    print 'HTTP response '.$tx_res->code.' '.$tx_res->message;
}

Make non-blocking call:

$client->call($url, $callobj, sub {
    # With callback
    my $res = pop;

    # ... process result ...

    Mojo::IOLoop->stop;
});

Mojo::IOLoop->start;

prepare

Prepares a proxy object that allows RPC methods to be called more easily.

my $proxy = $client->prepare($uri, ['sum', 'echo']);

my $res = $proxy->sum(1, 2);

print $proxy->echo("Echo this!");

Register services from multiple urls at once:

my $proxy = $client->prepare($uri1, 'sum', $uri2, [ 'echo', 'ping' ]);

my $res = $proxy->sum(1, 2);

print $proxy->echo("Echo this!");

my $ping_res = $proxy->ping;

MojoX::JSON::RPC::CLient::ReturnObject

This object is returned by call.

result

RPC result.

is_error

Returns a boolean indicating whether an error code has been set.

error_code

RPC error code.

error_message

RPC error message.

error_data

RPC error data.

SEE ALSO

MojoX::JSON::RPC