File Coverage

blib/lib/XAS/Lib/RPC/JSON/Client.pm
Criterion Covered Total %
statement 6 28 21.4
branch 0 6 0.0
condition 0 6 0.0
subroutine 2 4 50.0
pod 1 1 100.0
total 9 45 20.0


line stmt bran cond sub pod time code
1             package XAS::Lib::RPC::JSON::Client;
2              
3             our $VERSION = '0.02';
4              
5             use XAS::Class
6 1         8 debug => 0,
7             version => $VERSION,
8             base => 'XAS::Lib::Net::Client',
9             utils => ':validation dotid',
10             codec => 'JSON',
11             constants => ':jsonrpc HASHREF',
12 1     1   756 ;
  1         2  
13              
14 1     1   959 use Data::Dumper;
  1         2  
  1         297  
15              
16             # ----------------------------------------------------------------------
17             # Public Methods
18             # ----------------------------------------------------------------------
19              
20             sub call {
21 0     0 1   my $self = shift;
22 0           my $p = validate_params(\@_, {
23             -method => 1,
24             -id => 1,
25             -params => { type => HASHREF }
26             });
27              
28 0           my $params;
29             my $response;
30              
31 0           while (my ($key, $value) = each(%{$p->{'params'}})) {
  0            
32              
33 0           $key =~ s/^-//;
34 0           $params->{$key} = $value;
35              
36             }
37              
38             my $packet = {
39             jsonrpc => RPC_JSON,
40             id => $p->{'id'},
41 0           method => $p->{'method'},
42             params => $params
43             };
44              
45 0           $self->log->debug(Dumper($packet));
46              
47 0           $self->puts(encode($packet));
48              
49 0 0         if ($response = $self->gets()) {
50              
51 0           $response = decode($response);
52 0           $self->log->debug(Dumper($response));
53              
54 0 0         if ($response->{'id'} eq $p->{'id'}) {
55              
56 0           $self->_check_for_errors($response);
57 0           return $response->{'result'};
58              
59             } else {
60              
61 0           $self->throw_msg(
62             dotid($self->class) . '.call.invalid_id',
63             'json_rpc_invalid_id',
64             );
65              
66             }
67            
68             } else {
69            
70             $self->throw_msg(
71             dotid($self->class) . '.call.invalid_response',
72             'json_rpc_invalid_response',
73 0           $p->{'method'}
74             );
75              
76             }
77              
78             }
79              
80             # ----------------------------------------------------------------------
81             # Private Methods
82             # ----------------------------------------------------------------------
83              
84             sub _check_for_errors {
85 0     0     my $self = shift;
86 0           my $response = shift;
87              
88 0 0         if ($response->{'error'}) {
89              
90             $self->throw_msg(
91             dotid($self->class) . '.call.rpc_error',
92             'json_rpc_error',
93             $response->{'error'}->{'code'} || '',
94             $response->{'error'}->{'message'} || '',
95 0   0       $response->{'error'}->{'data'} || '',
      0        
      0        
96             );
97              
98             }
99              
100             }
101              
102             1;
103              
104             __END__