1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
##############################################################################
# JSONRPC version 1.1
# http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html
##############################################################################
use strict;
use JSON ();
use Carp ();
##############################################################################
package JSON::RPC::Legacy::Client;
$JSON::RPC::Legacy::Client::VERSION = '1.06';
use LWP::UserAgent;
BEGIN {
for my $method (qw/uri ua json content_type version id allow_call status_line/) {
eval qq|
sub $method {
\$_[0]->{$method} = \$_[1] if defined \$_[1];
\$_[0]->{$method};
}
|;
}
}
sub AUTOLOAD {
my $self = shift;
my $method = $JSON::RPC::Legacy::Client::AUTOLOAD;
$method =~ s/.*:://;
return if ($method eq 'DESTROY');
$method =~ s/^__(\w+)__$/$1/; # avoid to call built-in methods (ex. __VERSION__ => VERSION)
unless ( exists $self->allow_call->{ $method } ) {
Carp::croak("Can't call the method not allowed by prepare().");
}
my @params = @_;
my $obj = {
method => $method,
params => (ref $_[0] ? $_[0] : [@_]),
};
my $ret = $self->call($self->uri, $obj);
if ( $ret and $ret->is_success ) {
return $ret->result;
}
else {
Carp::croak ( $ret ? '(Procedure error) ' . $ret->error_message : $self->status_line );
}
}
sub create_json_coder {
JSON->new->allow_nonref->utf8;
}
sub new {
my $proto = shift;
my $self = bless {}, (ref $proto ? ref $proto : $proto);
my $ua = LWP::UserAgent->new(
agent => 'JSON::RPC::Legacy::Client/' . $JSON::RPC::Legacy::Client::VERSION . ' beta ',
timeout => 10,
);
$self->ua($ua);
$self->json( $proto->create_json_coder );
$self->version('1.1');
$self->content_type('application/json');
return $self;
}
sub prepare {
my ($self, $uri, $procedures) = @_;
$self->uri($uri);
$self->allow_call({ map { ($_ => 1) } @$procedures });
}
sub call {
my ($self, $uri, $obj) = @_;
my $result;
if ($uri =~ /\?/) {
$result = $self->_get($uri);
}
else {
Carp::croak "not hashref." unless (ref $obj eq 'HASH');
$result = $self->_post($uri, $obj);
}
my $service = $obj->{method} =~ /^system\./ if ( $obj );
$self->status_line($result->status_line);
# If the server returned an error, we have no JSON.
return if ($result->code >= 500 && $result->code < 600);
return unless($result->content); # notification?
if ($service) {
return JSON::RPC::Legacy::ServiceObject->new($result, $self->json);
}
return JSON::RPC::Legacy::ReturnObject->new($result, $self->json);
}
sub _post {
my ($self, $uri, $obj) = @_;
my $json = $self->json;
$obj->{version} ||= $self->{version} || '1.1';
if ($obj->{version} eq '1.0') {
delete $obj->{version};
if (exists $obj->{id}) {
$self->id($obj->{id}) if ($obj->{id}); # if undef, it is notification.
}
else {
$obj->{id} = $self->id || ($self->id('JSON::RPC::Legacy::Client'));
}
}
else {
$obj->{id} = $self->id if (defined $self->id);
}
my $content = $json->encode($obj);
$self->ua->post(
$uri,
Content_Type => $self->{content_type},
Content => $content,
Accept => 'application/json',
);
}
sub _get {
my ($self, $uri) = @_;
$self->ua->get(
$uri,
Accept => 'application/json',
);
}
##############################################################################
package JSON::RPC::Legacy::ReturnObject;
$JSON::RPC::Legacy::ReturnObject::VERSION = $JSON::RPC::Legacy::VERSION;
BEGIN {
for my $method (qw/is_success content jsontext version/) {
eval qq|
sub $method {
\$_[0]->{$method} = \$_[1] if defined \$_[1];
\$_[0]->{$method};
}
|;
}
}
sub new {
my ($class, $obj, $json) = @_;
my $content = ( $json || JSON->new->utf8 )->decode( $obj->content );
my $self = bless {
jsontext => $obj->content,
content => $content,
}, $class;
$content->{error} ? $self->is_success(0) : $self->is_success(1);
$content->{version} ? $self->version(1.1) : $self->version(0) ;
$self;
}
sub is_error { !$_[0]->is_success; }
sub error_message {
$_[0]->version ? $_[0]->{content}->{error}->{message} : $_[0]->{content}->{error};
}
sub result {
$_[0]->{content}->{result};
}
##############################################################################
package JSON::RPC::Legacy::ServiceObject;
use base qw(JSON::RPC::Legacy::ReturnObject);
sub sdversion {
$_[0]->{content}->{sdversion} || '';
}
sub name {
$_[0]->{content}->{name} || '';
}
sub result {
$_[0]->{content}->{summary} || '';
}
1;
__END__
=pod
=head1 NAME
JSON::RPC::Legacy::Client - Perl implementation of JSON-RPC client
=head1 SYNOPSIS
use JSON::RPC::Legacy::Client;
my $client = new JSON::RPC::Legacy::Client;
my $url = 'http://www.example.com/jsonrpc/API';
my $callobj = {
method => 'sum',
params => [ 17, 25 ], # ex.) params => { a => 20, b => 10 } for JSON-RPC v1.1
};
my $res = $client->call($uri, $callobj);
if($res) {
if ($res->is_error) {
print "Error : ", $res->error_message;
}
else {
print $res->result;
}
}
else {
print $client->status_line;
}
# Easy access
$client->prepare($uri, ['sum', 'echo']);
print $client->sum(10, 23);
=head1 DESCRIPTION
This is JSON-RPC Client.
See L<http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html>.
Gets a perl object and convert to a JSON request data.
Sends the request to a server.
Gets a response returned by the server.
Converts the JSON response data to the perl object.
=head1 JSON::RPC::Legacy::Client
=head2 METHODS
=over
=item $client = JSON::RPC::Legacy::Client->new
Creates new JSON::RPC::Legacy::Client object.
=item $response = $client->call($uri, $procedure_object)
Calls to $uri with $procedure_object.
The request method is usually C<POST>.
If $uri has query string, method is C<GET>.
About 'GET' method,
see to L<http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html#GetProcedureCall>.
Return value is L</JSON::RPC::Legacy::ReturnObject>.
=item $client->prepare($uri, $arrayref_of_procedure)
Allow to call methods in contents of $arrayref_of_procedure.
Then you can call the prepared methods with an array reference or a list.
The return value is a result part of JSON::RPC::Legacy::ReturnObject.
$client->prepare($uri, ['sum', 'echo']);
$res = $client->echo('foobar'); # $res is 'foobar'.
$res = $client->sum(10, 20); # sum up
$res = $client->sum( [10, 20] ); # same as above
If you call a method which is not prepared, it will C<croak>.
Currently, B<can't call any method names as same as built-in methods>.
=item version
Sets the JSON-RPC protocol version.
1.1 by default.
=item id
Sets a request identifier.
In JSON-RPC 1.1, it is optional.
If you set C<version> 1.0 and don't set id,
the module sets 'JSON::RPC::Legacy::Client' to it.
=item ua
Setter/getter to L<LWP::UserAgent> object.
=item json
Setter/getter to the JSON coder object.
Default is L<JSON>, likes this:
$self->json( JSON->new->allow_nonref->utf8 );
$json = $self->json;
This object serializes/deserializes JSON data.
By default, returned JSON data assumes UTF-8 encoded.
=item status_line
Returns status code;
After C<call> a remote procedure, the status code is set.
=item create_json_coder
(Class method)
Returns a JSON de/encoder in C<new>.
You can override it to use your favorite JSON de/encoder.
=back
=head1 JSON::RPC::Legacy::ReturnObject
C<call> method or the methods set by C<prepared> returns this object.
(The returned JSON data is decoded by the JSON coder object which was passed
by the client object.)
=head2 METHODS
=over
=item is_success
If the call is successful, returns a true, otherwise a false.
=item is_error
If the call is not successful, returns a true, otherwise a false.
=item error_message
If the response contains an error message, returns it.
=item result
Returns the result part of a data structure returned by the called server.
=item content
Returns the whole data structure returned by the called server.
=item jsontext
Returns the row JSON data.
=item version
Returns the version of this response data.
=back
=head1 JSON::RPC::Legacy::ServiceObject
=head1 RESERVED PROCEDURE
When a client call a procedure (method) name 'system.foobar',
JSON::RPC::Legacy::Server look up MyApp::system::foobar.
L<http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html#ProcedureCall>
L<http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html#ServiceDescription>
There is JSON::RPC::Legacy::Server::system::describe for default response of 'system.describe'.
=head1 SEE ALSO
L<http://json-rpc.org/wd/JSON-RPC-1-1-WD-20060807.html>
L<http://json-rpc.org/wiki/specification>
=head1 AUTHOR
Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright 2007-2008 by Makamaka Hannyaharamitu
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|