Client.pm

filter: Statementstmt 97.2% 312 / 321 Branchbran 77.1% 88 / 114 Conditioncond 71.1% 37 / 52 MC/DCmcdc 28.5% 8 / 28 Subroutinesub 92.3% 48 / 52 Podpod 63.6% 7 / 11 totaltotal 86.5% 500 / 578 CC 83 SCAR 16.1
CC
83
Cov
90%
_get_uri
11.2
new
10.1
_handle_poll_response
9.3
CRAP 5.0
↑ Summary Client/API.pm »
1 package RPC::ExtDirect::Client;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 11 ▶use RPC::ExtDirect::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use RPC::ExtDirect::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use RPC::ExtDirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #
17 # This module is not compatible with RPC::ExtDirect < 3.0
18 #
19  
20 11 ▶croak __PACKAGE__." requires RPC::ExtDirect 3.0+"
Branch: 1 branch
BranchTrueFalseTotal
if $RPC::ExtDirect::VERSION lt "3.0" 0 11 11
21 if $RPC::ExtDirect::VERSION lt '3.0';
22  
23 ### PACKAGE GLOBAL VARIABLE ###
24 #
25 # Module version
26 #
27  
28 11 our $VERSION = '1.25';
29  
30 ### PUBLIC CLASS METHOD (CONSTRUCTOR) ###
31 #
32 # Instantiate a new Client, connect to the specified server
33 # and initialize the Ext.Direct API
34 #
35  
36 14 ▶sub new {
Subroutine new: called CC=9 SCAR=23.1
Pod documented
37 14 my ($class, %params) = @_;
38
39 14 my $api = delete $params{api};
40 14 ▶ my $config = delete $params{config} || ($api && $api->config) ||
Condition66%6/9
delete $params{'config'} || $api && $api->config || 'RPC::ExtDirect::Config'->new
l!l&&r!l&&!r
4100
delete $params{"config"} or $api and $api->config
l!l&&r!l&&!r
0410
$api and $api->config
!ll&&!rl&&r
1004
MC/DC0%0/4
delete $params{'config'} || $api && $api->config || 'RPC::ExtDirect::Config'->new
delete $params{"config"} $api $api->config 'RPC::ExtDirect::Config'->new
Truth table33%2/6
A or B and C || D
A= delete $params{"config"}B= $apiC= $api->configD= 'RPC::ExtDirect::Config'->new
ABCDresult
1XXX1
011X1
00X11
01011
00X00
01000
41 RPC::ExtDirect::Config->new();
42
43 14 my $self = bless {
44 config => $config,
45 api => {},
46 tid => 0,
47 }, $class;
48
49 14 $self->_decorate_config($config);
50 14 ▶ $self->_decorate_api($api) if $api;
Branch: 1 branch
BranchTrueFalseTotal
if $api 4 10 14
51
52 14 my @config_params = qw/
53 api_path router_path poll_path remoting_var polling_var
54 /;
55
56 14 for my $key ( @config_params ) {
57 $config->$key( delete $params{ $key } )
58 70 ▶ if exists $params{ $key };
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$key} 0 70 70
59 }
60
61 14 my @our_params = qw/ host port proto cv cookies api_cb /;
62
63 14 @$self{ @our_params } = delete @params{ @our_params };
64
65 # The rest of parameters apply to the transport
66 14 $self->http_params({ %params });
67
68 # This may die()
69 14 eval { $self->_init_api($api) };
70
71 0 ▶ if ($@) { croak 'ARRAY' eq ref($@) ? $@->[0] : $@ };
Branch: 2 branches
BranchTrueFalseTotal
if ($@) 0 14 14
'ARRAY' eq ref $@ ? : 0 0 0
72
73 14 return $self;
74 }
75  
76 ### PUBLIC INSTANCE METHOD ###
77 #
78 # Call specified Action's Method
79 #
80  
81 32 ▶sub call { shift->sync_request('call', @_) }
Subroutine call: called CC=1 SCAR=0.0
Pod documented
82  
83 ### PUBLIC INSTANCE METHOD ###
84 #
85 # Submit a form to specified Action's Method
86 #
87  
88 9 ▶sub submit { shift->sync_request('form', @_) }
Subroutine submit: called CC=1 SCAR=0.0
Pod documented
89  
90 ### PUBLIC INSTANCE METHOD ###
91 #
92 # Upload a file using POST form. Same as submit()
93 #
94  
95 11 *upload = *submit;
96  
97 ### PUBLIC INSTANCE METHOD ###
98 #
99 # Poll server for Ext.Direct events
100 #
101  
102 11 ▶sub poll { shift->sync_request('poll', @_) }
Subroutine poll: called CC=1 SCAR=0.0
Pod documented
103  
104 ### PUBLIC INSTANCE METHOD ###
105 #
106 # Run a specified request type synchronously
107 #
108  
109 52 ▶sub sync_request {
Subroutine sync_request: called CC=7 SCAR=19.5
Pod undocumented
110 52 my $self = shift;
111 52 my $type = shift;
112
113 52 my $tr_class = $self->transaction_class;
114
115 52 my $resp = eval {
116 52 my $transaction = $tr_class->new(@_);
117 52 $self->_sync_request($type, $transaction);
118 };
119
120 #
121 # Internally we throw an exception string enclosed in arrayref,
122 # so that die() wouldn't munge it. Easier to do and beats stripping
123 # that \n any time. JSON or other packages could throw a plain string
124 # though, so we need to guard against that.
125 #
126 # Rethrow by croak(), and don't strip the file name and line number
127 # this time -- seeing exactly where the thing blew up in *your*
128 # code is a lot more helpful to a developer than the plain old die()
129 # exception would allow.
130 #
131 52 ▶ if ($@) { croak 'ARRAY' eq ref($@) ? $@->[0] : $@ };
Branch: 2 branches
BranchTrueFalseTotal
if ($@) 14 38 52
'ARRAY' eq ref $@ ? : 14 0 14
132
133 # We're only interested in the data, unless it's a poll. In versions
134 # < 1.0, we used to return a scalar value for polls, either a single
135 # event or an arrayref of event hashrefs; that behavior was more or
136 # less closely following the spec and typical server response.
137 # However that was kinda awkward, so we try to DWIM here and adjust
138 # to the caller's expectations.
139 38 ▶ if ( $type eq 'poll') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "poll") 11 27 38
140 11 ▶ return wantarray ? @$resp
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 6 5 11
@$resp == 1 ? : 4 1 5
141 : @$resp == 1 ? $resp->[0]
142 : $resp
143 ;
144 }
145
146 27 ▶ return ref($resp) =~ /Exception/ ? $resp : $resp->{result};
Branch: 1 branch
BranchTrueFalseTotal
ref($resp) =~ /Exception/ ? : 1 26 27
147 }
148  
149 ### PUBLIC INSTANCE METHOD ###
150 #
151 # Return next TID (transaction ID)
152 #
153  
154 28 ▶sub next_tid { $_[0]->{tid}++ }
Subroutine next_tid: called CC=1 SCAR=0.0
Pod undocumented
155  
156 ### PUBLIC INSTANCE METHOD ###
157 #
158 # Return API object by its type
159 #
160  
161 87 ▶sub get_api {
Subroutine get_api: called CC=1 SCAR=0.0
Pod documented
162 87 my ($self, $type) = @_;
163
164 87 return $self->{api}->{$type};
165 }
166  
167 ### PUBLIC INSTANCE METHOD ###
168 #
169 # Store the passed API object according to its type
170 #
171  
172 28 ▶sub set_api {
Subroutine set_api: called CC=2 SCAR=7.0
Pod undocumented
173 28 my ($self, $api, $type) = @_;
174
175 28 ▶ $type ||= $api->type;
Condition66%2/3
$type ||= $api->type
l!l&&r!l&&!r
8200
MC/DC0%0/2
$type ||= $api->type
$type $api->type
Truth table66%2/3
A ||= B
A= $typeB= $api->type
ABresult
1X1
011
000
176
177 28 $self->{api}->{$type} = $api;
178 }
179  
180 ### PUBLIC INSTANCE METHODS ###
181 #
182 # Read-only accessor delegates
183 #
184  
185 0 ▶sub remoting_var { $_[0]->config->remoting_var }
Subroutine remoting_var: not called CC=1 SCAR=6.9
Pod documented
186 0 ▶sub polling_var { $_[0]->config->polling_var }
Subroutine polling_var: not called CC=1 SCAR=6.9
Pod documented
187  
188 ### PUBLIC INSTANCE METHOD ###
189 #
190 # Return the name of the Transaction class. This was not made
191 # a Config option since the only case when somebody would want
192 # to change that is in a subclass.
193 #
194  
195 52 ▶sub transaction_class { 'RPC::ExtDirect::Client::Transaction' }
Subroutine transaction_class: called CC=1 SCAR=0.0
Pod undocumented
196  
197 ### PUBLIC INSTANCE METHODS ###
198 #
199 # Read-write accessors
200 #
201  
202 11 RPC::ExtDirect::Util::Accessor->mk_accessor(
203 simple => [qw/ config host port proto cv cookies http_params api_cb /],
204 );
205  
206 ############## PRIVATE METHODS BELOW ##############
207  
208 ### PRIVATE INSTANCE METHOD ###
209 #
210 # Create a new Exception object
211 #
212  
213 1 ▶sub _exception {
Subroutine _exception: called CC=1 SCAR=0.0
214 1 my ($self, $ex) = @_;
215
216 1 my $config = $self->config;
217 1 my $exclass = $config->exception_class;
218
219 1 eval "require $exclass";
220
221 1 return $exclass->new($ex);
222 }
223  
224 ### PRIVATE INSTANCE METHOD ###
225 #
226 # Add the Client-specific accessors to a Config instance
227 # and set defaults
228 #
229  
230 11 my %std_config = (
231 api_class_client => 'RPC::ExtDirect::Client::API',
232 transport_class => 'HTTP::Tiny',
233 );
234  
235 14 ▶sub _decorate_config {
Subroutine _decorate_config: called CC=6 SCAR=18.2
236 14 my ($self, $config) = @_;
237
238 14 $config->add_accessors(
239 overwrite => 1,
240 simple => [ keys %std_config ],
241 );
242
243 14 for my $key ( keys %std_config ) {
244 28 my $predicate = "has_${key}";
245
246 28 ▶ $config->$key( $std_config{ $key } )
Branch: 1 branch
BranchTrueFalseTotal
unless $config->$predicate 24 4 28
247 unless $config->$predicate;
248
249 # This is the best place to load the classes, too
250 # since we only want to do this once.
251 28 eval "require " . $config->$key;
252 }
253
254 14 my $std_m_class = 'RPC::ExtDirect::Client::API::Method';
255
256 14 ▶ $config->api_method_class($std_m_class)
Branch: 1 branch
BranchTrueFalseTotal
if $config->_is_default("api_method_class") 12 2 14
257 if $config->_is_default('api_method_class');
258
259 # Client uses a flavor of API::Method with disabled
260 # metadata arg checks; since the user might have set
261 # the config value we want to make sure the class
262 # has relevant overrides.
263 14 my $actual_m_class = $config->api_method_class;
264
265 14 ▶ croak __PACKAGE__ . " is configured to use API Method class ".
Branch: 1 branch
BranchTrueFalseTotal
unless $actual_m_class eq $std_m_class or $actual_m_class->isa($std_m_class) 0 14 14
Condition33%1/3
$actual_m_class eq $std_m_class or $actual_m_class->isa($std_m_class)
l!l&&r!l&&!r
1400
MC/DC0%0/2
$actual_m_class eq $std_m_class or $actual_m_class->isa($std_m_class)
$actual_m_class eq $std_m_class $actual_m_class->isa($std_m_class)
Truth table33%1/3
A or B
A= $actual_m_class eq $std_m_classB= $actual_m_class->isa($std_m_class)
ABresult
1X1
011
000
266 "$actual_m_class that is not a subclass of $std_m_class"
267 unless $actual_m_class eq $std_m_class ||
268 $actual_m_class->isa($std_m_class);
269 }
270  
271 ### PRIVATE INSTANCE METHOD ###
272 #
273 # Make sure that the API instance passed to us is a subclass
274 # of RPC::ExtDirect::Client::API
275 #
276  
277 4 ▶sub _decorate_api {
Subroutine _decorate_api: called CC=2 SCAR=6.9
278 4 my ($self, $api) = @_;
279
280 4 my $api_class = $self->config->api_class_client;
281
282 4 ▶ bless $api, $api_class unless $api->isa($api_class);
Branch: 1 branch
BranchTrueFalseTotal
unless $api->isa($api_class) 2 2 4
283 }
284  
285 ### PRIVATE INSTANCE METHOD ###
286 #
287 # Initialize API declaration.
288 #
289 # The two-step between _init_api and _import_api is to allow
290 # async API retrieval and processing in Client::Async without
291 # duplicating more code than is necessary
292 #
293  
294 14 ▶sub _init_api {
Subroutine _init_api: called CC=2 SCAR=6.9
295 14 my ($self, $api) = @_;
296
297 14 ▶ if ( $api ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($api) { } 4 10 14
298 4 $self->_assign_api($api);
299 }
300 else {
301 10 my $api_js = $self->_get_api();
302
303 10 $self->_import_api($api_js);
304 }
305 }
306  
307 ### PRIVATE INSTANCE METHOD ###
308 #
309 # Assign API object to the corresponding slots
310 #
311  
312 4 ▶sub _assign_api {
Subroutine _assign_api: called CC=2 SCAR=7.0
313 4 my ($self, $api) = @_;
314
315 4 $self->set_api($api, 'remoting');
316
317 4 ▶ if ( $api->get_poll_handlers ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($api->get_poll_handlers) 4 0 4
318 4 $self->set_api($api, 'polling');
319 }
320 }
321  
322 ### PRIVATE INSTANCE METHOD ###
323 #
324 # Receive API declaration from specified server,
325 # parse it and return Client::API object
326 #
327  
328 10 ▶sub _get_api {
Subroutine _get_api: called CC=3 SCAR=11.1
329 10 my ($self) = @_;
330  
331 10 my $uri = $self->_get_uri('api');
332 10 my $params = $self->http_params;
333
334 10 my $transport_class = $self->config->transport_class;
335  
336 10 my $resp = $transport_class->new(%$params)->get($uri);
337  
338 die ["Can't download API declaration: $resp->{status} $resp->{content}"]
339 10 ▶ unless $resp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->{"success"} 0 10 10
340  
341 10 ▶ die ["Empty API declaration"] unless length $resp->{content};
Branch: 1 branch
BranchTrueFalseTotal
unless length $resp->{"content"} 0 10 10
342  
343 10 return $resp->{content};
344 }
345  
346 ### PRIVATE INSTANCE METHOD ###
347 #
348 # Import specified API into global namespace
349 #
350  
351 10 ▶sub _import_api {
Subroutine _import_api: called CC=3 SCAR=11.0
352 10 my ($self, $api_js) = @_;
353
354 10 my $config = $self->config;
355 10 my $remoting_var = $config->remoting_var;
356 10 my $polling_var = $config->polling_var;
357 10 my $api_class = $config->api_class_client;
358
359 10 eval "require $api_class";
360
361 10 $api_js =~ s/\s*//gms;
362
363 10 my @parts = split /;\s*/, $api_js;
364
365 10 my $api_regex = qr/^\Q$remoting_var\E|\Q$polling_var\E/;
366
367 10 for my $part ( @parts ) {
368 20 ▶ next unless $part =~ $api_regex;
Branch: 1 branch
BranchTrueFalseTotal
unless $part =~ /$api_regex/ 0 20 20
369
370 20 my $api = $api_class->new_from_js(
371 config => $config,
372 js => $part,
373 );
374
375 20 $self->set_api($api);
376 }
377 }
378  
379 ### PRIVATE INSTANCE METHOD ###
380 #
381 # Return URI for specified type of call
382 #
383  
384 57 ▶sub _get_uri {
Subroutine _get_uri: called CC=11 SCAR=24.1
385 57 my ($self, $type) = @_;
386
387 57 my $config = $self->config;
388
389 57 my $api;
390
391 57 ▶ if ( $type eq 'remoting' || $type eq 'polling' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "remoting" or $type eq "polling") 44 13 57
Condition100%3/3
$type eq "remoting" or $type eq "polling"
l!l&&r!l&&!r
301413
MC/DC100%2/2
$type eq "remoting" or $type eq "polling"
$type eq "remoting" $type eq "polling"
Truth table100%3/3
A or B
A= $type eq "remoting"B= $type eq "polling"
ABresult
1X1
011
000
392 44 $api = $self->get_api($type);
393
394 44 ▶ die ["Don't have API definition for type $type"]
Branch: 1 branch
BranchTrueFalseTotal
unless $api 0 44 44
395 unless $api;
396 }
397
398 57 ▶ my $proto = $self->proto || 'http';
Condition100%2/2
$self->proto || 'http'
l!l
354
MC/DC100%1/1
$self->proto || 'http'
$self->proto
Truth table100%2/2
A
A= $self->proto
Aresult
11
00
399 57 my $host = $self->host;
400 57 my $port = $self->port;
401  
402 57 ▶ my $path = $type eq 'api' ? $config->api_path
Branch: 3 branches
BranchTrueFalseTotal
$type eq 'api' ? : 13 44 57
$type eq 'remoting' ? : 30 14 44
$type eq 'polling' ? : 14 0 14
Condition66%4/6
$api->url or $config->router_path
l!l&&r!l&&!r
2640
$api->url or $config->poll_path
l!l&&r!l&&!r
1040
MC/DC0%0/2
$api->url or $config->router_path
$api->url $config->router_path
MC/DC0%0/2
$api->url or $config->poll_path
$api->url $config->poll_path
Truth table66%2/3
A or B
A= $api->urlB= $config->router_path
ABresult
1X1
011
000
Truth table66%2/3
A or B
A= $api->urlB= $config->poll_path
ABresult
1X1
011
000
403 : $type eq 'remoting' ? $api->url || $config->router_path
404 : $type eq 'polling' ? $api->url || $config->poll_path
405 : die ["Unknown type $type"]
406 ;
407  
408 57 $path =~ s{^/}{};
409  
410 57 ▶ my $uri = $port ? "$proto://$host:$port/$path"
Branch: 1 branch
BranchTrueFalseTotal
$port ? : 54 3 57
411 : "$proto://$host/$path"
412 ;
413  
414 57 return $uri;
415 }
416  
417 ### PRIVATE INSTANCE METHOD ###
418 #
419 # Normalize passed arguments to conform to Method's spec
420 #
421  
422 30 ▶sub _normalize_arg {
Subroutine _normalize_arg: called CC=2 SCAR=6.9
423 30 my ($self, $method, $trans) = @_;
424
425 30 my $arg = $trans->arg;
426
427 # This could die with a message that has \n at the end to prevent
428 # file and line being appended. Catch and rethrow in a format
429 # more compatible with what Client does in other places.
430 30 eval { $method->check_method_arguments($arg) };
431
432 30 ▶ if ( my $xcpt = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xcpt = $@) 6 24 30
433 6 $xcpt =~ s/\n$//;
434 6 die [$xcpt];
435 }
436  
437 24 my $result = $method->prepare_method_arguments( input => $arg );
438  
439 24 return $result;
440 }
441  
442 ### PRIVATE INSTANCE METHOD ###
443 #
444 # Normalize passed metadata to conform to Method's spec
445 #
446  
447 32 ▶sub _normalize_metadata {
Subroutine _normalize_metadata: called CC=2 SCAR=6.9
448 32 my ($self, $method, $trans) = @_;
449
450 32 my $meta = $trans->metadata;
451
452 # See _normalize_arg above
453 32 eval { $method->check_method_metadata($meta) };
454
455 32 ▶ if ( my $xcpt = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xcpt = $@) 5 27 32
456 5 $xcpt =~ s/\n$//;
457 5 die [$xcpt];
458 }
459
460 27 my $result = $method->prepare_method_metadata( metadata => $meta );
461
462 27 return $result;
463 }
464  
465 ### PRIVATE INSTANCE METHOD ###
466 #
467 # Normalize passed arguments to submit as form POST
468 #
469  
470 9 ▶sub _formalize_arg {
Subroutine _formalize_arg: called CC=8 SCAR=21.2
471 9 my ($self, $method, $trans) = @_;
472
473 9 my $arg = $trans->arg;
474 9 my $upload = $trans->upload;
475
476 # formHandler method require arguments in a hashref and will die
477 # with an error if the arguments are missing. However it is often
478 # convenient to call Client->upload() with empty arg but with a
479 # list of file names to upload; it doesn't make a lot of sense to
480 # insist on providing an empty argument hashref just for the sake
481 # of being strict.
482 9 ▶ $arg = $arg || {} if $upload;
Branch: 1 branch
BranchTrueFalseTotal
if $upload 2 7 9
Condition50%1/2
$arg || {}
l!l
20
MC/DC0%0/1
$arg || {}
$arg
Truth table50%1/2
A
A= $arg
Aresult
11
00
483
484 # This could die with a message that has \n at the end to prevent
485 # file and line being appended. Catch and rethrow in a format
486 # more compatible with what Client does in other places.
487 9 eval { $method->check_method_arguments($arg) };
488
489 9 ▶ if ( my $xcpt = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xcpt = $@) 0 9 9
490 0 $xcpt =~ s/\n$//;
491 0 die [$xcpt];
492 }
493
494 9 my $fields = {
495 extAction => $method->action,
496 extMethod => $method->name,
497 extType => 'rpc',
498 extTID => $self->next_tid,
499 };
500  
501 # Go over the uploads and check if they're readable; die if not
502 9 for my $file ( @$upload ) {
503 7 ▶ die ["Upload entry '$file' is not readable"] unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 1 6 7
504 }
505
506 8 ▶ $fields->{extUpload} = 'true' if $upload;
Branch: 1 branch
BranchTrueFalseTotal
if $upload 8 0 8
507  
508 8 my $actual_arg = $method->prepare_method_arguments( input => $arg );
509
510 8 @$fields{ keys %$actual_arg } = values %$actual_arg;
511  
512 # This will die in approved format, so no outer eval
513 8 my $meta_json = $self->_formalize_metadata($method, $trans);
514
515 8 ▶ $fields->{metadata} = $meta_json if $meta_json;
Branch: 1 branch
BranchTrueFalseTotal
if $meta_json 2 6 8
516  
517 8 return $fields;
518 }
519  
520 ### PRIVATE INSTANCE METHOD ###
521 #
522 # Normalize passed metadata to conform to Method's spec
523 # and encode in JSON to be submitted in a form POST
524 #
525  
526 8 ▶sub _formalize_metadata {
Subroutine _formalize_metadata: called CC=3 SCAR=11.3
527 8 my ($self, $method, $transaction) = @_;
528
529 8 my $meta_json;
530
531 # This will die according to plan so no outer eval
532 8 my $metadata = $self->_normalize_metadata($method, $transaction);
533
534 8 ▶ if ( $metadata ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metadata) 2 6 8
535 # This won't die according to plan :(
536 2 $meta_json = eval { JSON::to_json($metadata) };
537
538 2 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
539 0 my $xcpt = RPC::ExtDirect::Util::clean_error_message($@);
540 0 die [$xcpt];
541 }
542 }
543
544 8 return $meta_json;
545 }
546  
547 ### PRIVATE INSTANCE METHOD ###
548 #
549 # Make an HTTP request in synchronous fashion. Note that we do not
550 # guard against exceptions here, they should be propagated upwards
551 # to be caught in public sync_request() that calls this one.
552 #
553  
554 52 ▶sub _sync_request {
Subroutine _sync_request: called CC=4 SCAR=14.6
555 52 my ($self, $type, $transaction) = @_;
556
557 52 my $prepare = "_prepare_${type}_request";
558 52 my $handle = "_handle_${type}_response";
559 52 ▶ my $method = $type eq 'poll' ? 'GET' : 'POST';
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'poll' ? : 11 41 52
560
561 52 my ($uri, $request_content, $http_params, $request_options)
562 = $self->$prepare($transaction);
563
564 38 $request_options->{content} = $request_content;
565
566 38 my $transport_class = $self->config->transport_class;
567
568 38 my $transport = $transport_class->new(%$http_params);
569 38 my $response = $transport->request($method, $uri, $request_options);
570
571 # By Ext.Direct spec that shouldn't even happen; however the transport
572 # may crap out or something else might cause a failed request.
573 # Status code 599 is internal for HTTP::Tiny, with the error message
574 # placed in the response content.
575 38 ▶ if (!$response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->{"success"}) 0 38 38
576 my $err = $response->{status} == 599 ? $response->{content}
577 : $response->{status}
578 0 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
$response->{'status'} == 599 ? : 0 0 0
579 0 die ["Ext.Direct request unsuccessful: $err"];
580 }
581
582 38 return $self->$handle($response, $transaction);
583 }
584  
585 ### PRIVATE INSTANCE METHOD ###
586 #
587 # Prepare the POST body, headers, request options and other
588 # data necessary to make an HTTP request for a non-form call
589 #
590  
591 32 ▶sub _prepare_call_request {
Subroutine _prepare_call_request: called CC=3 SCAR=11.0
592 32 my ($self, $transaction) = @_;
593
594 32 my $action_name = $transaction->action;
595 32 my $method_name = $transaction->method;
596
597 32 my $api = $self->get_api('remoting');
598 32 my $action = $api->get_action_by_name($action_name);
599
600 32 ▶ die ["Action $action_name is not found"] unless $action;
Branch: 1 branch
BranchTrueFalseTotal
unless $action 1 31 32
601
602 31 my $method = $action->method($method_name);
603
604 31 ▶ die ["Method $method_name is not found in Action $action_name"]
Branch: 1 branch
BranchTrueFalseTotal
unless $method 1 30 31
605 unless $method;
606
607 30 my $actual_arg = $self->_normalize_arg($method, $transaction);
608 24 my $metadata = $self->_normalize_metadata($method, $transaction);
609
610 19 my $post_body = $self->_encode_post_body(
611 action => $action_name,
612 method => $method_name,
613 data => $actual_arg,
614 metadata => $metadata,
615 );
616
617 # HTTP params is a union between transaction params and client params.
618 19 my $http_params = $self->_merge_params($transaction);
619  
620 19 my $request_options = {
621 headers => { 'Content-Type' => 'application/json', }
622 };
623  
624 19 $self->_parse_cookies($request_options, $http_params);
625  
626 19 my $uri = $self->_get_uri('remoting');
627
628 return (
629 19 $uri,
630 $post_body,
631 $http_params,
632 $request_options,
633 );
634 }
635  
636 ### PRIVATE INSTANCE METHOD ###
637 #
638 # Prepare the POST body, headers, request options and other
639 # data necessary to make an HTTP request for a form call
640 #
641  
642 9 ▶sub _prepare_form_request {
Subroutine _prepare_form_request: called CC=5 SCAR=16.1
643 9 my ($self, $transaction) = @_;
644
645 9 my $action_name = $transaction->action;
646 9 my $method_name = $transaction->method;
647
648 9 my $api = $self->get_api('remoting');
649 9 my $action = $api->get_action_by_name($action_name);
650
651 9 ▶ die ["Action $action_name is not found"] unless $action;
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 9 9
652
653 9 my $method = $action->method($method_name);
654
655 9 ▶ die ["Method $method_name is not found in Action $action_name"]
Branch: 1 branch
BranchTrueFalseTotal
unless $method 0 9 9
656 unless $method;
657
658 9 my $fields = $self->_formalize_arg($method, $transaction);
659 8 my $upload = $transaction->upload;
660
661 8 ▶ my $form_body
Branch: 1 branch
BranchTrueFalseTotal
$upload ? : 1 7 8
662 = $upload ? $self->_www_form_multipart($fields, $upload)
663 : $self->_www_form_urlencode($fields)
664 ;
665
666 8 ▶ my $ct
Branch: 1 branch
BranchTrueFalseTotal
$upload ? : 1 7 8
667 = $upload ? 'multipart/form-data; boundary='.$self->_get_boundary
668 : 'application/x-www-form-urlencoded; charset=utf-8'
669 ;
670
671 8 my $request_options = {
672 headers => { 'Content-Type' => $ct, },
673 };
674
675 8 my $http_params = $self->_merge_params($transaction);
676
677 8 $self->_parse_cookies($request_options, $http_params);
678
679 8 my $uri = $self->_get_uri('remoting');
680
681 return (
682 8 $uri,
683 $form_body,
684 $http_params,
685 $request_options,
686 );
687 }
688  
689 ### PRIVATE INSTANCE METHOD ###
690 #
691 # Prepare the POST body, headers, request options and other
692 # data necessary to make an HTTP request for an event poll
693 #
694  
695 11 ▶sub _prepare_poll_request {
Subroutine _prepare_poll_request: called CC=1 SCAR=0.0
696 11 my ($self, $transaction) = @_;
697
698 11 my $uri = $self->_get_uri('polling');
699
700 11 my $http_params = $self->_merge_params($transaction);
701
702 11 my $request_options = {
703 headers => { 'Content-Type' => 'application/json' },
704 };
705
706 11 $self->_parse_cookies($request_options, $http_params);
707
708 return (
709 11 $uri,
710 undef,
711 $http_params,
712 $request_options,
713 );
714 }
715  
716 ### PRIVATE INSTANCE METHOD ###
717 #
718 # Create POST payload body
719 #
720  
721 19 ▶sub _create_post_payload {
Subroutine _create_post_payload: called CC=2 SCAR=7.0
722 19 my ($self, %arg) = @_;
723
724 my $href = {
725 type => 'rpc',
726 tid => $self->next_tid,
727 action => $arg{action},
728 method => $arg{method},
729 data => $arg{data},
730 19 };
731
732 $href->{metadata} = $arg{metadata}
733 19 ▶ if exists $arg{metadata};
Branch: 1 branch
BranchTrueFalseTotal
if exists $arg{"metadata"} 19 0 19
734
735 19 return $href;
736 }
737  
738 ### PRIVATE INSTANCE METHOD ###
739 #
740 # Encode post payload body
741 #
742  
743 19 ▶sub _encode_post_body {
Subroutine _encode_post_body: called CC=1 SCAR=0.0
744 19 my $self = shift;
745
746 19 my $payload = $self->_create_post_payload(@_);
747  
748 19 return JSON->new->utf8(1)->encode($payload);
749 }
750  
751 ### PRIVATE INSTANCE METHOD ###
752 #
753 # Encode form fields as multipart/form-data
754 #
755  
756 1 ▶sub _www_form_multipart {
Subroutine _www_form_multipart: called CC=6 SCAR=18.0
757 1 my ($self, $arg, $uploads) = @_;
758  
759 # This code is shamelessly "adapted" from CGI::Test::Input::Multipart
760 1 my $CRLF = "\015\012";
761 1 my $boundary = '--' . $self->_get_boundary();
762 1 my $format = 'Content-Disposition: form-data; name="%s"';
763  
764 1 my $result;
765  
766 1 foreach my $field (keys %$arg) {
767 5 my $value = $arg->{$field};
768
769 5 $result .= $boundary . $CRLF;
770 5 $result .= sprintf($format, $field) . $CRLF.$CRLF;
771 5 $result .= $value . $CRLF;
772 };
773  
774 1 ▶ while ( $uploads && @$uploads ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uploads and @$uploads) 6 1 7
Condition66%2/3
$uploads and @$uploads
!ll&&!rl&&r
016
MC/DC50%1/2
$uploads and @$uploads
$uploads @$uploads
Truth table66%2/3
A and B
A= $uploadsB= @$uploads
ABresult
0X0
100
111
775 6 my $filename = shift @$uploads;
776 6 my $basename = (File::Spec->splitpath($filename))[2];
777  
778 6 $result .= $boundary . $CRLF;
779 6 $result .= sprintf $format, 'upload';
780 6 $result .= sprintf('; filename="%s"', $basename) . $CRLF;
781 6 $result .= "Content-Type: application/octet-stream" . $CRLF.$CRLF;
782  
783 6 ▶ if ( open my $fh, '<', $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $filename) 6 0 6
784 6 binmode $fh;
785 6 local $/;
786  
787 6 $result .= <$fh> . $CRLF;
788 };
789 }
790  
791 1 ▶ $result .= $boundary . '--' if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 1 0 1
792  
793 1 return $result;
794 }
795  
796 ### PRIVATE INSTANCE METHOD ###
797 #
798 # Generate multipart/form-data boundary
799 #
800  
801 11 my $boundary;
802  
803 2 ▶sub _get_boundary {
Subroutine _get_boundary: called CC=4 SCAR=13.9
804 2 ▶ return $boundary if $boundary;
Branch: 1 branch
BranchTrueFalseTotal
if $boundary 1 1 2
805
806 1 my $rand;
807  
808 1 for ( 0..19 ) {
809 20 $rand .= (0..9, 'A'..'Z')[$_] for int rand 36;
810 };
811  
812 1 return $boundary = $rand;
813 }
814  
815 ### PRIVATE INSTANCE METHOD ###
816 #
817 # Encode form fields as application/x-www-form-urlencoded
818 #
819  
820 7 ▶sub _www_form_urlencode {
Subroutine _www_form_urlencode: called CC=1 SCAR=0.0
821 7 my ($self, $arg) = @_;
822
823 7 my $transport_class = $self->config->transport_class;
824  
825 7 return $transport_class->new->www_form_urlencode($arg);
826 }
827  
828 ### PRIVATE INSTANCE METHOD ###
829 #
830 # Produce a union of transaction HTTP parameters
831 # with client HTTP parameters
832 #
833  
834 38 ▶sub _merge_params {
Subroutine _merge_params: called CC=2 SCAR=6.9
835 38 my ($self, $trans) = @_;
836
837 38 my %client_params = %{ $self->http_params };
838 38 my %trans_params = %{ $trans->http_params };
839
840 # Transaction parameters trump client's
841 38 @client_params{ keys %trans_params } = values %trans_params;
842
843 # Cookies from transaction trump client's as well,
844 # but replace them entirely instead of combining
845 38 ▶ $client_params{cookies} = $trans->cookies || $self->cookies;
Condition100%3/3
$trans->cookies || $self->cookies
l!l&&r!l&&!r
9326
MC/DC100%2/2
$trans->cookies || $self->cookies
$trans->cookies $self->cookies
Truth table100%3/3
A || B
A= $trans->cookiesB= $self->cookies
ABresult
1X1
011
000
846
847 38 return \%client_params;
848 }
849  
850 ### PRIVATE INSTANCE METHOD ###
851 #
852 # Process Ext.Direct response and return either data or exception
853 #
854  
855 27 ▶sub _handle_call_response {
Subroutine _handle_call_response: called CC=3 SCAR=11.0
856 27 my ($self, $resp) = @_;
857
858 27 my $content = $self->_decode_response_body( $resp->{content} );
859
860 return $self->_exception($content)
861 27 ▶ if 'HASH' eq ref $content and $content->{type} eq 'exception';
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" eq ref $content and $content->{"type"} eq "exception" 1 26 27
Condition66%2/3
"HASH" eq ref $content and $content->{"type"} eq "exception"
!ll&&!rl&&r
0261
MC/DC50%1/2
"HASH" eq ref $content and $content->{"type"} eq "exception"
"HASH" eq ref $content $content->{"type"} eq "exception"
Truth table66%2/3
A and B
A= "HASH" eq ref $contentB= $content->{"type"} eq "exception"
ABresult
0X0
100
111
862
863 26 return $content;
864 }
865  
866 11 *_handle_form_response = *_handle_call_response;
867  
868 ### PRIVATE INSTANCE METHOD ###
869 #
870 # Handle poll response
871 #
872  
873 11 ▶sub _handle_poll_response {
Subroutine _handle_poll_response: called CC=9 SCAR=22.3
874 11 my ($self, $resp) = @_;
875  
876 # JSON->decode can die()
877 11 my $ev = $self->_decode_response_body( $resp->{content} );
878  
879 # Poll provider has to return a null event if there are no events
880 # because returning empty response would break JavaScript client
881 # in certain (now outdated) Ext JS versions. The server has to keep
882 # the compatible behavior but we don't have to follow that
883 # broken implementation here.
884 return []
885 if ('HASH' ne ref $ev and 'ARRAY' ne ref $ev) or
886 ('HASH' eq ref $ev and
887 ($ev->{name} eq '__NONE__' or $ev->{name} eq '' or
888 11 ▶ $ev->{type} ne 'event')
Branch: 1 branch
BranchTrueFalseTotal
if "HASH" ne ref $ev and "ARRAY" ne ref $ev or "HASH" eq ref $ev and $ev->{"name"} eq "__NONE__" || $ev->{"name"} eq "" || $ev->{"type"} ne "event" 1 10 11
Condition73%11/15
"HASH" ne ref $ev and "ARRAY" ne ref $ev or "HASH" eq ref $ev and $ev->{"name"} eq "__NONE__" || $ev->{"name"} eq "" || $ev->{"type"} ne "event"
l!l&&r!l&&!r
0110
"HASH" ne ref $ev and "ARRAY" ne ref $ev
!ll&&!rl&&r
920
"HASH" eq ref $ev and $ev->{"name"} eq "__NONE__" || $ev->{"name"} eq "" || $ev->{"type"} ne "event"
!ll&&!rl&&r
281
$ev->{"name"} eq "__NONE__" or $ev->{"name"} eq "" or $ev->{"type"} ne "event"
l!l&&r!l&&!r
108
$ev->{"name"} eq "__NONE__" or $ev->{"name"} eq ""
l!l&&r!l&&!r
108
MC/DC16%1/6
"HASH" ne ref $ev and "ARRAY" ne ref $ev or "HASH" eq ref $ev and $ev->{"name"} eq "__NONE__" || $ev->{"name"} eq "" || $ev->{"type"} ne "event"
"HASH" ne ref $ev "ARRAY" ne ref $ev "HASH" eq ref $ev $ev->{"name"} eq "__NONE__" $ev->{"name"} eq "" $ev->{"type"} ne "event"
Truth table27%3/11
A and B or C and D or E or F
A= "HASH" ne ref $evB= "ARRAY" ne ref $evC= "HASH" eq ref $evD= $ev->{"name"} eq "__NONE__"E= $ev->{"name"} eq ""F= $ev->{"type"} ne "event"
ABCDEFresult
11XXXX1
0X11XX1
0X101X1
0X10011
1011XX1
10101X1
1010011
0X0XXX0
0X10000
100XXX0
1010000
889 )
890 ;
891
892 # Server side can return either a single event, or an array
893 # of events. This is how the spec goes. :/ Normalize the output
894 # here so that we could sanitize it upstream.
895 10 ▶ $ev = 'ARRAY' eq ref($ev) ? $ev : [ $ev ];
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $ev ? : 2 8 10
896  
897 10 delete $_->{type} for @$ev;
898  
899 10 return $ev;
900 }
901  
902 ### PRIVATE INSTANCE METHOD ###
903 #
904 # Decode Ext.Direct response body
905 #
906  
907 38 ▶sub _decode_response_body {
Subroutine _decode_response_body: called CC=2 SCAR=6.9
908 38 my ($self, $body) = @_;
909  
910 38 my $json_text = $body;
911  
912 # Form POSTs require this additional handling
913 38 my $re = qr{^<html><body><textarea>(.*)</textarea></body></html>$}msi;
914  
915 38 ▶ if ( $body =~ $re ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body =~ /$re/) 8 30 38
916 8 $json_text = $1;
917 8 $json_text =~ s{\\"}{"}g;
918 };
919  
920 38 return JSON->new->utf8(1)->decode($json_text);
921 }
922  
923 ### PRIVATE INSTANCE METHOD ###
924 #
925 # Parse cookies if provided, creating Cookie header
926 #
927  
928 40 ▶sub _parse_cookies {
Subroutine _parse_cookies: called CC=4 SCAR=13.9
929 40 my ($self, $to, $from) = @_;
930  
931 40 my $cookie_jar = $from->{cookies};
932  
933 40 ▶ return unless $cookie_jar;
Branch: 1 branch
BranchTrueFalseTotal
unless $cookie_jar 26 14 40
934  
935 14 my $cookies;
936  
937 14 ▶ if ( 'HTTP::Cookies' eq ref $cookie_jar ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HTTP::Cookies' eq ref $cookie_jar) { } 4 10 14
938 4 $cookies = $self->_parse_http_cookies($cookie_jar);
939 }
940 else {
941 10 $cookies = $self->_parse_raw_cookies($cookie_jar);
942 }
943  
944 14 ▶ $to->{headers}->{Cookie} = $cookies if $cookies;
Branch: 1 branch
BranchTrueFalseTotal
if $cookies 14 0 14
945 }
946  
947 ### PRIVATE INSTANCE METHOD ###
948 #
949 # Parse cookies from HTTP::Cookies object
950 #
951  
952 4 ▶sub _parse_http_cookies {
Subroutine _parse_http_cookies: called CC=1 SCAR=0.0
953 4 my ($self, $cookie_jar) = @_;
954  
955 4 my @cookies;
956  
957 8 ▶ $cookie_jar->scan(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
958 8 my ($v, $key, $value) = @_;
959  
960 8 push @cookies, "$key=$value";
961 4 });
962  
963 4 return \@cookies;
964 }
965  
966 ### PRIVATE INSTANCE METHOD ###
967 #
968 # Parse (or rather, normalize) cookies passed as a hashref
969 #
970  
971 10 ▶sub _parse_raw_cookies {
Subroutine _parse_raw_cookies: called CC=2 SCAR=7.0
972 10 my ($self, $cookie_jar) = @_;
973  
974 10 ▶ return [] unless 'HASH' eq ref $cookie_jar;
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $cookie_jar 0 10 10
975  
976 10 return [ map { join '=', $_ => $cookie_jar->{$_} } keys %$cookie_jar ];
977 }
978  
979 package
980 RPC::ExtDirect::Client::Transaction;
981  
982 11 my @fields = qw/ action method arg upload cookies metadata /;
983  
984 52 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
985 52 my ($class, %params) = @_;
986
987 52 my %self_params = map { $_ => delete $params{$_} } @fields;
988
989 52 return bless {
990 http_params => { %params },
991 %self_params,
992 }, $class;
993 }
994  
995 0 ▶sub start {}
Subroutine start: not called CC=1 SCAR=0.0
996 0 ▶sub finish {}
Subroutine finish: not called CC=1 SCAR=0.0
997  
998 11 RPC::ExtDirect::Util::Accessor->mk_accessors(
999 simple => ['http_params', @fields],
1000 );
1001  
1002 11 1;
↑ Summary Client/API.pm »