File Coverage

lib/MojoX/JSON/RPC/Client.pm
Criterion Covered Total %
statement 85 97 87.6
branch 34 52 65.3
condition 20 34 58.8
subroutine 18 19 94.7
pod 2 2 100.0
total 159 204 77.9


line stmt bran cond sub pod time code
1             package MojoX::JSON::RPC::Client;
2              
3 3     3   2050 use Mojo::Base -base;
  3         5  
  3         15  
4 3     3   366 use Mojo::JSON qw(encode_json decode_json);
  3         3  
  3         104  
5 3     3   13 use Mojo::UserAgent;
  3         4  
  3         26  
6              
7             has id => undef;
8             has ua => sub { Mojo::UserAgent->new };
9             has version => '2.0';
10             has content_type => 'application/json';
11             has tx => undef; # latest transaction
12              
13             sub call {
14 37     37 1 4056 my ( $self, $uri, $body, $callback ) = @_;
15              
16             # body might be json already, only encode if necessary
17 37 100 100     171 if ( ref $body eq 'HASH' || ref $body eq 'ARRAY' ) {
18 30 100       82 foreach my $o ( ref $body eq 'HASH' ? $body : @{$body} ) {
  5         11  
19 36   33     166 $o->{version} ||= $self->version;
20             }
21 30         204 $body = encode_json($body);
22             }
23             else {
24 7   100     19 $body ||= q{};
25             }
26              
27             # Always POST if $body is not empty!
28 37 100       3280 if ( ref $callback ne 'CODE' ) {
29 35 100       60 if ( $body ne q{} ) {
    50          
30 33         80 return $self->_process_result(
31             $self->ua->post(
32             $uri, { Content_Type => $self->content_type }, $body
33             )
34             );
35             }
36             elsif ( $uri =~ /\?/xms ) {
37 2         7 return $self->_process_result( $self->ua->get($uri) );
38             }
39             }
40             else { # non-blocking
41 2 50       6 if ( $body ne q{} ) {
    0          
42             $self->ua->post(
43             $uri,
44             { Content_Type => $self->content_type },
45             $body,
46             sub { # callback
47 2     2   3078 $callback->( $self->_process_result(pop) );
48             },
49 2         6 );
50 2         2105 return;
51             }
52             elsif ( $uri =~ /\?/xms ) {
53             $self->ua->get(
54             $uri => sub { # callback
55 0     0   0 $callback->( $self->_process_result(pop) );
56             }
57 0         0 );
58 0         0 return;
59             }
60             }
61 0         0 return Carp::croak 'Cannot process call!';
62             }
63              
64             # Prepare a Proxy object
65             sub prepare {
66 1     1 1 303 my $self = shift;
67              
68 1         2 my %m = ();
69             URI:
70 1         3 while ( my $uri = shift ) {
71 2         4 my $methods = shift;
72              
73             # methods can be a name, a reference to a name or
74             # a reference to an array of names
75 2 50 66     10 if ( ref $methods eq 'SCALAR' ) {
    100          
76 0         0 $methods = [$$methods];
77             }
78             elsif ( defined $methods && ref $methods eq q{} ) {
79 1         3 $methods = [$methods];
80             }
81 2 50       4 if ( ref $methods ne 'ARRAY' ) {
82 0         0 last URI;
83             }
84             METHOD:
85 2         3 foreach my $method ( @{$methods} ) {
  2         3  
86 3 50 33     7 if ( exists $m{$method} && $m{$method} ne $uri ) {
87 0         0 Carp::croak qq{Cannot register method $method twice!};
88             }
89 3         8 $m{$method} = $uri;
90             }
91             }
92 1         10 return bless {
93             client => $self,
94             methods => \%m
95             },
96             'MojoX::JSON::RPC::Client::Proxy';
97             }
98              
99             sub _process_result {
100 37     37   72626 my ( $self, $tx ) = @_;
101              
102 37         135 $self->tx($tx); # save latest transaction
103              
104 37         556 my $tx_res = $tx->res;
105 37 50       166 my $log = $self->ua->server->app->log if $self->ua->server->app;
106 37 50 33     975 if ( $log && $log->is_level('debug') ) {
107 0         0 $log->debug( 'TX BODY: [' . $tx_res->body . ']' );
108             }
109              
110             # Check if RPC call is succesfull
111 37 50 66     265 if ( !( $tx_res->is_success || $tx_res->is_client_error ) )
112             {
113 0         0 return;
114             }
115              
116 37         578 my $decode_error;
117             my $rpc_res;
118            
119 37 50 100     47 eval{ $rpc_res = decode_json( $tx_res->body || '{}' ); 1; } or $decode_error = $@;
  37         78  
  37         17178  
120 37 50 33     104 if ( $decode_error && $log ) { # Server result cannot be parsed!
121 0         0 $log->error( 'Cannot parse rpc result: ' . $decode_error );
122 0         0 return;
123             }
124              
125             # Return one or more ReturnObject's
126             return ref $rpc_res eq 'ARRAY'
127             ? [
128             map {
129 8         34 MojoX::JSON::RPC::Client::ReturnObject->new( rpc_response => $_ )
130 37 100       185 } ( @{$rpc_res} )
  2         5  
131             ]
132             : MojoX::JSON::RPC::Client::ReturnObject->new(
133             rpc_response => $rpc_res );
134             }
135              
136             package MojoX::JSON::RPC::Client::Proxy;
137              
138 3     3   2250 use Carp;
  3         6  
  3         160  
139 3     3   14 use warnings;
  3         4  
  3         88  
140 3     3   24 use strict;
  3         10  
  3         534  
141              
142             # no constructor defined. Object creation
143             # done by MojoX::JSON::RPC::Client.
144              
145             our $AUTOLOAD;
146              
147             # Dispatch calls
148             sub AUTOLOAD {
149 4     4   21 my $self = shift;
150              
151 4         4 my $method = $AUTOLOAD;
152 4         21 $method =~ s/.*:://;
153              
154             # We do not want to overload DESTROY
155 4 100       12 if ( $method eq 'DESTROY' ) {
156 1         16 return;
157             }
158              
159 3 50       11 if ( !exists $self->{methods}->{$method} ) {
160 0         0 Carp::croak "Unsupported method $method";
161             }
162              
163             my $res = $self->{client}->call(
164             $self->{methods}->{$method},
165 3         19 { id => $self->{id}++,
166             method => $method,
167             params => \@_
168             }
169             );
170 3 50       38 return defined $res ? $res->result : ();
171             }
172              
173             package MojoX::JSON::RPC::Client::ReturnObject;
174              
175 3     3   16 use Mojo::Base -base;
  3         4  
  3         9  
176              
177             has rpc_response => undef; # rpc response
178              
179             sub result {
180 40     40   162 my ($self) = @_;
181 40         68 my $rpc_response = $self->rpc_response;
182             return
183             ref $rpc_response eq 'HASH' && exists $rpc_response->{result}
184             ? $rpc_response->{result}
185 40 100 66     253 : undef;
186             }
187              
188             sub id {
189 26     26   690 my ($self) = @_;
190 26         41 my $rpc_response = $self->rpc_response;
191             return
192             ref $rpc_response eq 'HASH' && exists $rpc_response->{id}
193             ? $rpc_response->{id}
194 26 50 33     155 : undef;
195             }
196              
197             sub is_error {
198 94     94   575 my ($self) = @_;
199 94         158 my $rpc_response = $self->rpc_response;
200             return ref $rpc_response eq 'HASH' && exists $rpc_response->{error}
201 94 100 66     439 ? 1
202             : 0;
203             }
204              
205             sub error_code {
206 14     14   56 my ($self) = @_;
207 14 50       20 return $self->is_error ? $self->rpc_response->{error}->{code} : undef;
208             }
209              
210             sub error_message {
211 14     14   72 my ($self) = @_;
212 14 50       18 return $self->is_error ? $self->rpc_response->{error}->{message} : undef;
213             }
214              
215             sub error_data {
216 14     14   75 my ($self) = @_;
217 14 50       21 return $self->is_error ? $self->rpc_response->{error}->{data} : undef;
218             }
219              
220             1;
221              
222             __END__