JRPC

 view release on metacpan or  search on metacpan

JRPC/Client.pm  view on Meta::CPAN

=head1 DESCRIPTION

JRPC::Client is a Perl LWP based JSON-RPC 2.0 Client hoping to minimize tedious boilerplate code for JSON-RPC
interaction, yet enabling advanced use cases by the power of LWP / HTTP::Request.

JRPC::Client complies to conventions of JSON-RPC 2.0, but it can be coerced to be used for other versions as well.

=head2 $client = JRPC::Client->new()

Instantiate a new JSON-RPC (2.0) Client.
HTTP keep-alive is turned on, cookie store is established and
default user-agent name is set here.
Any of the LWP::UserAgent methods are callable on the returned object as JRPC::Client IS-A LWP::UserAgent.

The lifetime of the JRPC::Client can be kept long (e.g. throughout app) and it can usually be kept as single instance
in app runtime (singleton, however JRPC::Client does not control singularity of instantiation).
The factory method method new_request() takes care of instatiating requests for various URL:s, various methods.

=cut
sub new {
  my ($class, %c) = @_;
  my $ua = LWP::UserAgent->new('keep_alive' => 1, 'cookie_jar' => {});
  $ua->agent("JSON-RPC Client/0.9");
  if ($c{'jsonrpc'}) {$ua->{'_jsonrpc'} = $c{'jsonrpc'};}
  # Re-bless ...
  return bless($ua, $class);
}

=head2 $req = $client->new_request($url, %opts)

Factory method to instantiate and prepare a new JSON-RPC request to a URL. Options in %opts:

examples/LongSearch.pm  view on Meta::CPAN

        push(@files, $rp);
      }
   };
   # We are running a server - do NOT allow chdir()
   File::Find::find({'wanted' => $cb, 'no_chdir' => 1, }, $path);
   # Call Back
   my $rp;
   $rp = {'cpid' => $$, 'rid' => $p->{'rid'}, };
   $rp->{'path'} = $path;
   $rp->{'files'} = \@files;
   $rp->{'STATUS'} = 'alive';
   if (my $url = $p->{'cburl'}) {
     #my $req = $client->new_request($url);
     #DEBUG:print($fh "Client ack to '$url': ".Dumper($rp));
     #my $resp = $req->call('onsearchcomplete', $rp, 'notify' => 1);
     #DEBUG:print($fh "Resp from '$url': ".$resp->content()."\n");
     JRPC::respond_async($client, $url, 'onsearchcomplete', $rp);
   }
   # TODO: If email ?
   if (my $email = $p->{'email'}) {
      # Load lazy to not form a strict dependency.

t/20test_mp.t  view on Meta::CPAN

my $host = $ENV{'QMP_HOST'}; # || 'localhost';

if (!$ENV{'QMP_HOST'}) {plan('skip_all', "Need QMP_HOST for host to be tested");}
eval("use WWW::Mechanize;");
if ($@) {plan('skip_all', "No WWW::Mechanize found in system");}
my $url = "http://$host/Math";
plan('tests', 8);
my $debug = $ENV{'QMP_DEBUG'} || 0;
note("Set QMP_DEBUG in environment to debug HTTP traffic (Currently: QMP_DEBUG=$debug)");
# For starters, use Plain WWW::Mechanize to test the JSON-RPC server side. TODO: Use JRPC::Client directly
my $mech = WWW::Mechanize->new('cookie_jar' => {}, 'keep_alive' => 1, );
{
  my $jsoncont = encode_json($msg);
  ok($jsoncont, "Sending(sum): $jsoncont");
  my $resp = $mech->post($url, 'Content' => $jsoncont);
  #my $hdrs = $resp->...;
  #ok($resp, "$resp");
  isa_ok($resp, "HTTP::Response");
  if ($debug) {$mech->dump_headers();}
  my $cont = $mech->content(); #    $mech->response->decoded_content();
  ok($cont, "Got Response: $cont\n");



( run in 2.450 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )