AnyEvent-JSONRPC

 view release on metacpan or  search on metacpan

lib/AnyEvent/JSONRPC/HTTP/Client.pm  view on Meta::CPAN

package AnyEvent::JSONRPC::HTTP::Client;
use Any::Moose;
use Any::Moose '::Util::TypeConstraints';

extends 'AnyEvent::JSONRPC::Client';

use Carp;
use Scalar::Util 'weaken';

use AnyEvent;
use AnyEvent::HTTP;

use JSON::RPC::Common::Procedure::Call;
use JSON::RPC::Common::Procedure::Return;

use MIME::Base64;
use JSON::XS;

has url => (
    is       => 'ro',
    isa      => 'Str',
    required => 1,
);

has version => (
    is      => 'rw',
    isa     => enum( [qw( 1.0 1.1 2.0 )] ),
    default => "2.0",
);

has username => (
    is => "rw",
    isa => 'Str',
    predicate => "has_username"
);

has password => (
    is => "rw",
    isa => "Str"
);

has _request_pool => (
    is      => 'ro',
    isa     => 'HashRef',
    lazy    => 1,
    default => sub { {} },
);

has _next_id => (
    is      => 'ro',
    isa     => 'CodeRef',
    lazy    => 1,
    default => sub {
        my $id = 0;
        sub { ++$id };
    },
);

no Any::Moose;

sub call {
    my ($self, $method, @params) = @_;

    my $request = JSON::RPC::Common::Procedure::Call->inflate ( 
        version => $self->version,
        id      => $self->_next_id->(),
        method  => $method,
        params  => $self->_params( @params ),
    );

    my $guard = http_post $self->url, 
                          encode_json( $request->deflate ) . "   ",
                          headers => { 
                              "Content-Type" => "application/json",
                              $self->_authorization_header,
                          },
                          sub { $self->_handle_response( @_ ) };



( run in 1.959 second using v1.01-cache-2.11-cpan-39bf76dae61 )