| 1 | package REST::Consumer::Dispatch; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 9 | ▶ | use REST::Consumer::HandlerInvocation; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | # While the code functions as a dispatcher, | ||||||||||||||||||||
| 9 | # I'd like the object to represent an individual dispatch, | ||||||||||||||||||||
| 10 | # and have notions about its retry count which can safely change. | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 52 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 13 | 52 | my ($class, %args) = @_; | |||||||||||||||||||
| 14 | 52 | my $self = bless {}, $class; | |||||||||||||||||||
| 15 | 52 | $self->initialize(%args); | |||||||||||||||||||
| 16 | 52 | return $self; | |||||||||||||||||||
| 17 | } | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 52 | ▶ | sub initialize { | ||||||||||||||||||
|
Subroutine
initialize: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||
| 20 | 52 | my ($self, %args) = @_; | |||||||||||||||||||
| 21 | 52 | $self->{$_} = $args{$_} for qw(handlers default_is_raw debugger); | |||||||||||||||||||
| 22 | 52 | ▶ | $self->{debugger} ||= sub {}; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Condition33%1/3
$self->{"debugger"} ||= sub {
}
MC/DC0%0/2
$self->{"debugger"} ||= sub {
}
$self->{"debugger"}
sub {
}
Truth table33%1/3
A ||= B
A= $self->{"debugger"}B= sub {
}
| |||||||||||||||||||||
| 23 | } | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 52 | ▶ | sub handlers_for_code { | ||||||||||||||||||
|
Subroutine
handlers_for_code: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 26 | 52 | my ($self, $code) = @_; | |||||||||||||||||||
| 27 | 52 | my $metacode = substr($code, 0, 1) . 'xx'; # e.g. 4xx | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 52 | return grep { defined } ( | |||||||||||||||||||
| 30 | $self->user_handler($code), | ||||||||||||||||||||
| 31 | $self->user_handler($metacode), | ||||||||||||||||||||
| 32 | $self->fallback_handler($code), | ||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 104 | ▶ | sub user_handler { | ||||||||||||||||||
|
Subroutine
user_handler: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 37 | 104 | my ($self, $code) = @_; | |||||||||||||||||||
| 38 | 104 | return $self->{handlers}{$code}; | |||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 52 | ▶ | sub handle_response { | ||||||||||||||||||
|
Subroutine
handle_response: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||
| 42 | 52 | my ($self, %args) = @_; | |||||||||||||||||||
| 43 | 52 | my $response = $args{response}; | |||||||||||||||||||
| 44 | 52 | my $request = $args{request}; | |||||||||||||||||||
| 45 | 52 | my $attempt_no = $args{attempt}; | |||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | my @args = (REST::Consumer::HandlerInvocation->new( | ||||||||||||||||||||
| 48 | request => $request, | ||||||||||||||||||||
| 49 | response => $response, | ||||||||||||||||||||
| 50 | attempt => $attempt_no, | ||||||||||||||||||||
| 51 | debugger => $self->{debugger}, | ||||||||||||||||||||
| 52 | 52 | )); | |||||||||||||||||||
| 53 | 52 | my $code = $response->code; | |||||||||||||||||||
| 54 | 52 | my @handlers = $self->handlers_for_code($code); | |||||||||||||||||||
| 55 | 52 | ▶ | while (my $attempt = shift @handlers) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 53 | my @results = $attempt->(@args); | |||||||||||||||||||
| 57 | 52 | ▶ | if ($self->is_magic_operation($results[0], 'default')) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 58 | 1 | next; | |||||||||||||||||||
| 59 | } elsif ($self->is_magic_operation($results[0], 'retry')) { | ||||||||||||||||||||
| 60 | 39 | return ('retry', $results[0]); | |||||||||||||||||||
| 61 | } elsif ($self->is_magic_operation($results[0], 'fail')) { | ||||||||||||||||||||
| 62 | 0 | return ('fail', $results[0]); | |||||||||||||||||||
| 63 | } else { | ||||||||||||||||||||
| 64 | 12 | return ('succeed', \@results); | |||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | 0 | die "the fallback handler didn't catch us. who broke it?"; | |||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | 115 | ▶ | sub is_magic_operation { | ||||||||||||||||||
|
Subroutine
is_magic_operation: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 71 | # Herein we solve the semi-predicate problem | ||||||||||||||||||||
| 72 | # by coming up with a predicate that is implausible | ||||||||||||||||||||
| 73 | 115 | my ($self, $candidate_magic, $operation) = @_; | |||||||||||||||||||
| 74 | 115 | ▶ | return ref $candidate_magic && UNIVERSAL::can($candidate_magic, "rest_consumer_should_${operation}"); | ||||||||||||||||||
|
Condition100%3/3
ref $candidate_magic && UNIVERSAL::can($candidate_magic, "rest_consumer_should_$operation")
MC/DC100%2/2
ref $candidate_magic && UNIVERSAL::can($candidate_magic, "rest_consumer_should_$operation")
ref $candidate_magic
UNIVERSAL::can($candidate_magic, "rest_consumer_should_$operation")
Truth table100%3/3
A && B
A= ref $candidate_magicB= UNIVERSAL::can($candidate_magic, "rest_consumer_should_$operation")
| |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 52 | ▶ | sub fallback_handler { | ||||||||||||||||||
|
Subroutine
fallback_handler: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 78 | 52 | my ($self, $code) = @_; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 37 | ▶ | return sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||
| 81 | 37 | my ($h) = @_; | |||||||||||||||||||
| 82 | 37 | ▶ | if ($h->response->is_success) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | 9 | ▶ | if ($self->{default_is_raw}) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 84 | 1 | return $h->response; | |||||||||||||||||||
| 85 | } elsif ($h->response_parseable) { | ||||||||||||||||||||
| 86 | 1 | return $h->parsed_response; | |||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||
| 88 | 7 | return $h->response_body; | |||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | } | ||||||||||||||||||||
| 91 | 28 | my $ought_retry = !(scalar grep {$h->response->code() == $_} qw(403 404 405 413)); | |||||||||||||||||||
| 92 | 28 | ▶ | if ($ought_retry) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 28 | return $h->retry; | |||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||
| 95 | 0 | return $h->fail; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | } | ||||||||||||||||||||
| 98 | 52 | } | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | ▶ | sub debug { | ||||||||||||||||||
|
Subroutine
debug: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 101 | 0 | shift->{debugger}->(@_); | |||||||||||||||||||
| 102 | } | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 9 | 1; | |||||||||||||||||||