JSON-RPC
view release on metacpan or search on metacpan
my ($self, $context) = @_;
my $dispatch = ...; # grab it from somewhere
$dispatch->handle_psgi( $context->env );
}
# ERRORS
When your handler dies, it is automatically included in the response hash, unless no response was requested (see ["NOTIFICATIONS"](#notifications)).
For example, something like below
sub rpc {
...
if ($bad_thing_happend) {
die "Argh! I failed!";
}
}
Would result in a response like
{
error => {
code => -32603,
message => "Argh! I failed! at ...",
}
}
However, you can include custom data by die()'ing with a hash:
sub rpc {
...
if ($bad_thing_happend) {
die { message => "Argh! I failed!", data => time() };
}
}
This would result in:
{
error => {
code => -32603,
message => "Argh! I failed! at ...",
data => 1339817722,
}
}
# NOTIFICATIONS
Notifications are defined as procedures without an id.
Notification handling does not produce a response. When all procedures are notifications no content is returned (if the request is valid).
To maintain some basic compatibility with relaxed client implementations, JSON::RPC::Dispatch includes responses when procedures do not have a "jsonrpc" field set to "2.0".
Note that no error is returned in response to a notification when the handler dies or when the requested method is not available.
For example, a request structure like this:
[
{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
{"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
{"jsonrpc": "2.0", "method": "keep_alive"},
{"jsonrpc": "2.0", "method": "get_data", "id": "9"}
]
Would result in a response like
[
{"jsonrpc": "2.0", "result": 7, "id": "1"},
{"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
]
# BACKWARDS COMPATIBILITY
Eh, not compatible at all. JSON RPC 0.xx was fine, but it predates PSGI, and things are just... different before and after PSGI.
Code at version 0.96 has been moved to JSON::RPC::Legacy namespace, so change your application to use JSON::RPC::Legacy if you were using the old version.
# AUTHORS
Daisuke Maki
Shinichiro Aska
Yoshimitsu Torii
# AUTHOR EMERITUS
Makamaka Hannyaharamitu, <makamaka\[at\]cpan.org> - JSON::RPC modules up to 0.96
# COPYRIGHT AND LICENSE
The JSON::RPC module is
Copyright (C) 2011 by Daisuke Maki
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.0 or,
at your option, any later version of Perl 5 you may have available.
See JSON::RPC::Legacy for copyrights and license for previous versions.
( run in 0.855 second using v1.01-cache-2.11-cpan-d7a12ab2c7f )