TypeSafe/Client.pm

filter: Statementstmt 85.1% 132 / 155 Branchbran 53.5% 45 / 84 Conditioncond 37.8% 39 / 103 MC/DCmcdc 3.2% 2 / 61 Subroutinesub 78.2% 18 / 23 Podpod 0.0% 0 / 5 totaltotal 54.7% 236 / 431 CC 83 SCAR 42.9
CC
83
Cov
63%
_request
157.9
new
53.2
system_one
18.2
CRAP 73.2
↑ Summary TypeSafe/RetryPolicy.pm »
1 package WebService::TypeSafe::Client;
2  
3 3 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON::PP qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Time::HiRes qw(time sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use WebService::TypeSafe::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use WebService::TypeSafe::Models ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use WebService::TypeSafe::Question ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use WebService::TypeSafe::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use WebService::TypeSafe::RetryPolicy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 ▶sub new {
Subroutine new: called CC=21 SCAR=39.7
Pod undocumented
18 5 my ($class, @args) = @_;
19 0 ▶ my %args = @args == 1 && ref($args[0]) eq 'HASH' ? %{ $args[0] } : @args;
Branch: 1 branch
BranchTrueFalseTotal
@args == 1 && ref $args[0] eq 'HASH' ? : 0 5 5
Condition33%1/3
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
500
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1 ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
ABresult
0X0
100
111
20 5 ▶ my $api_key = exists $args{api_key} ? $args{api_key} : $ENV{TYPESAFE_API_KEY};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'api_key'} ? : 5 0 5
21 5 ▶ $api_key =~ s/^\s+|\s+$//g if defined $api_key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $api_key 5 0 5
22 5 ▶ _throw('TypeSafe API key is required') unless defined($api_key) && length($api_key);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $api_key and length $api_key 0 5 5
Condition33%1/3
defined $api_key and length $api_key
!ll&&!rl&&r
005
MC/DC0%0/2
defined $api_key and length $api_key
defined $api_key length $api_key
Truth table33%1/3
A and B
A= defined $api_keyB= length $api_key
ABresult
0X0
100
111
23 5 ▶ _throw('TypeSafe API key contains whitespace, control, or non-ASCII characters')
Branch: 1 branch
BranchTrueFalseTotal
if $api_key =~ /\s|[^\x21-\x7e]/u 0 5 5
24 if $api_key =~ /\s|[^\x21-\x7e]/;
25 5 ▶ my $base_url = $args{base_url} // $ENV{TYPESAFE_BASE_URL} // 'https://api.typesafe.ai';
Condition40%2/5
($args{"base_url"} // $ENV{"TYPESAFE_BASE_URL"}) // "https://api.typesafe.ai"
l!l
05
$args{"base_url"} // $ENV{"TYPESAFE_BASE_URL"}
l!l&&r!l&&!r
005
MC/DC0%0/2
($args{"base_url"} // $ENV{"TYPESAFE_BASE_URL"}) // "https://api.typesafe.ai"
$args{"base_url"} $ENV{"TYPESAFE_BASE_URL"}
Truth table33%1/3
A // B
A= $args{"base_url"}B= $ENV{"TYPESAFE_BASE_URL"}
ABresult
1X1
011
000
26 5 $base_url =~ s{/+$}{};
27 5 ▶ my $timeout = $args{timeout} // 60;
Condition50%1/2
$args{"timeout"} // 60
l!l
05
MC/DC0%0/1
$args{"timeout"} // 60
$args{"timeout"}
Truth table50%1/2
A
A= $args{"timeout"}
Aresult
11
00
28 5 ▶ _throw('timeout must be a positive number')
Branch: 1 branch
BranchTrueFalseTotal
unless $timeout =~ /^\d+(?:\.\d+)?$/u and $timeout > 0 0 5 5
Condition33%1/3
$timeout =~ /^\d+(?:\.\d+)?$/u and $timeout > 0
!ll&&!rl&&r
005
MC/DC0%0/2
$timeout =~ /^\d+(?:\.\d+)?$/u and $timeout > 0
$timeout =~ /^\d+(?:\.\d+)?$/u $timeout > 0
Truth table33%1/3
A and B
A= $timeout =~ /^\d+(?:\.\d+)?$/uB= $timeout > 0
ABresult
0X0
100
111
29 unless $timeout =~ /^\d+(?:\.\d+)?$/ && $timeout > 0;
30 5 ▶ my $retry = $args{retry} // WebService::TypeSafe::RetryPolicy->new;
Condition66%2/3
$args{"retry"} // "WebService::TypeSafe::RetryPolicy"->new
l!l&&r!l&&!r
410
MC/DC0%0/2
$args{"retry"} // "WebService::TypeSafe::RetryPolicy"->new
$args{"retry"} "WebService::TypeSafe::RetryPolicy"->new
Truth table66%2/3
A // B
A= $args{"retry"}B= "WebService::TypeSafe::RetryPolicy"->new
ABresult
1X1
011
000
31 5 ▶ $retry = WebService::TypeSafe::RetryPolicy->new($retry) if ref($retry) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $retry eq "HASH" 0 5 5
32 5 ▶ _throw('retry must be a WebService::TypeSafe::RetryPolicy')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($retry) and $retry->isa("WebService::TypeSafe::RetryPolicy") 0 5 5
Condition33%1/3
builtin::blessed($retry) and $retry->isa("WebService::TypeSafe::RetryPolicy")
!ll&&!rl&&r
005
MC/DC0%0/2
builtin::blessed($retry) and $retry->isa("WebService::TypeSafe::RetryPolicy")
builtin::blessed($retry) $retry->isa("WebService::TypeSafe::RetryPolicy")
Truth table33%1/3
A and B
A= builtin::blessed($retry)B= $retry->isa("WebService::TypeSafe::RetryPolicy")
ABresult
0X0
100
111
33 unless blessed($retry) && $retry->isa('WebService::TypeSafe::RetryPolicy');
34 my $self = bless {
35 api_key => $api_key,
36 model => $args{model} // $ENV{TYPESAFE_DEFAULT_MODEL} // 'jev-latest',
37 base_url => $base_url,
38 timeout => $timeout,
39 retry => $retry,
40 5 ▶ headers => { %{ $args{headers} || {} } },
Condition60%3/5
$args{'headers'} || {}
l!l
05
$args{'sleeper'} || sub { &WebService::TypeSafe::Client::sleep($_[0]); }
l!l&&r!l&&!r
230
MC/DC0%0/1
$args{'headers'} || {}
$args{'headers'}
MC/DC0%0/2
$args{'sleeper'} || sub { &WebService::TypeSafe::Client::sleep($_[0]); }
$args{'sleeper'} sub { &WebService::TypeSafe::Client::sleep($_[0]); }
Truth table50%1/2
A
A= $args{'headers'}
Aresult
11
00
Truth table66%2/3
A || B
A= $args{'sleeper'}B= sub { &WebService::TypeSafe::Client::sleep($_[0]); }
ABresult
1X1
011
000
41 http => $args{http},
42 0 ▶ sleeper => $args{sleeper} || sub { sleep($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
43 5 ▶ }, $class;
Condition40%2/5
($args{"model"} // $ENV{"TYPESAFE_DEFAULT_MODEL"}) // "jev-latest"
l!l
05
$args{"model"} // $ENV{"TYPESAFE_DEFAULT_MODEL"}
l!l&&r!l&&!r
005
MC/DC0%0/2
($args{"model"} // $ENV{"TYPESAFE_DEFAULT_MODEL"}) // "jev-latest"
$args{"model"} $ENV{"TYPESAFE_DEFAULT_MODEL"}
Truth table33%1/3
A // B
A= $args{"model"}B= $ENV{"TYPESAFE_DEFAULT_MODEL"}
ABresult
1X1
011
000
44 5 $self->{models} = WebService::TypeSafe::Models->new($self);
45 5 return $self;
46 }
47  
48 0 ▶sub model { $_[0]->{model} }
Subroutine model: not called CC=1 SCAR=6.9
Pod undocumented
49 0 ▶sub models { $_[0]->{models} }
Subroutine models: not called CC=1 SCAR=6.9
Pod undocumented
50  
51 5 ▶sub system_one {
Subroutine system_one: called CC=12 SCAR=29.0
Pod undocumented
52 5 my ($self, %args) = @_;
53 5 ▶ _throw('state is required') unless exists $args{state} && defined $args{state};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"state"} and defined $args{"state"} 0 5 5
Condition33%1/3
exists $args{"state"} and defined $args{"state"}
!ll&&!rl&&r
005
MC/DC0%0/2
exists $args{"state"} and defined $args{"state"}
exists $args{"state"} defined $args{"state"}
Truth table33%1/3
A and B
A= exists $args{"state"}B= defined $args{"state"}
ABresult
0X0
100
111
54 _throw('questions must be a nonempty hash reference')
55 5 ▶ unless ref($args{questions}) eq 'HASH' && keys %{ $args{questions} };
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"questions"} eq "HASH" and keys %{$args{"questions"};} 0 5 5
Condition33%1/3
ref $args{"questions"} eq "HASH" and keys %{$args{"questions"};}
!ll&&!rl&&r
005
MC/DC0%0/2
ref $args{"questions"} eq "HASH" and keys %{$args{"questions"};}
ref $args{"questions"} eq "HASH" keys %{$args{"questions"};}
Truth table33%1/3
A and B
A= ref $args{"questions"} eq "HASH"B= keys %{$args{"questions"};}
ABresult
0X0
100
111
56 5 my %questions;
57 5 for my $name (keys %{ $args{questions} }) {
58 6 my $q = $args{questions}{$name};
59 6 ▶ $questions{$name} = blessed($q) && $q->can('as_hash') ? $q->as_hash : $q;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($q) && $q->can('as_hash') ? : 6 0 6
Condition33%1/3
builtin::blessed($q) and $q->can("as_hash")
!ll&&!rl&&r
006
MC/DC0%0/2
builtin::blessed($q) and $q->can("as_hash")
builtin::blessed($q) $q->can("as_hash")
Truth table33%1/3
A and B
A= builtin::blessed($q)B= $q->can("as_hash")
ABresult
0X0
100
111
60 _throw("question '$name' must be a question object or hash reference")
61 6 ▶ unless ref($questions{$name}) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $questions{$name} eq "HASH" 0 6 6
62 }
63 my $body = {
64 state => $args{state},
65 model => $args{model} // $self->{model},
66 questions => \%questions,
67 5 ▶ %{ $args{extra_body} || {} },
Condition40%2/5
$args{"model"} // $self->{"model"}
l!l&&r!l&&!r
050
$args{'extra_body'} || {}
l!l
05
MC/DC0%0/2
$args{"model"} // $self->{"model"}
$args{"model"} $self->{"model"}
MC/DC0%0/1
$args{'extra_body'} || {}
$args{'extra_body'}
Truth table33%1/3
A // B
A= $args{"model"}B= $self->{"model"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $args{'extra_body'}
Aresult
11
00
68 };
69 my $data = $self->_request('POST', '/v1/systemone', $body,
70 5 ▶ map { exists($args{$_}) ? ($_ => $args{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} ? : 0 15 15
71 qw(retry timeout extra_headers));
72 2 _validate_response($data, $self->{base_url} . '/v1/systemone');
73 2 return WebService::TypeSafe::Response->from_hash($data);
74 }
75  
76 5 ▶sub _request {
Subroutine _request: called CC=39 SCAR=50.6
77 5 my ($self, $method, $path, $body, %opts) = @_;
78 5 ▶ my $retry = $opts{retry} // $self->{retry};
Condition33%1/3
$opts{"retry"} // $self->{"retry"}
l!l&&r!l&&!r
050
MC/DC0%0/2
$opts{"retry"} // $self->{"retry"}
$opts{"retry"} $self->{"retry"}
Truth table33%1/3
A // B
A= $opts{"retry"}B= $self->{"retry"}
ABresult
1X1
011
000
79 5 ▶ $retry = WebService::TypeSafe::RetryPolicy->new($retry) if ref($retry) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $retry eq "HASH" 0 5 5
80 5 ▶ my $timeout = $opts{timeout} // $self->{timeout};
Condition33%1/3
$opts{"timeout"} // $self->{"timeout"}
l!l&&r!l&&!r
050
MC/DC0%0/2
$opts{"timeout"} // $self->{"timeout"}
$opts{"timeout"} $self->{"timeout"}
Truth table33%1/3
A // B
A= $opts{"timeout"}B= $self->{"timeout"}
ABresult
1X1
011
000
81 5 my $url = $self->{base_url} . $path;
82 my %headers = (
83 'accept' => 'application/json',
84 'content-type' => 'application/json',
85 'authorization' => 'Bearer ' . $self->{api_key},
86 'user-agent' => 'webservice-typesafe-perl/' . $WebService::TypeSafe::VERSION,
87 5 %{ $self->{headers} },
88 5 ▶ %{ $opts{extra_headers} || {} },
Condition50%1/2
$opts{'extra_headers'} || {}
l!l
05
MC/DC0%0/1
$opts{'extra_headers'} || {}
$opts{'extra_headers'}
Truth table50%1/2
A
A= $opts{'extra_headers'}
Aresult
11
00
89 );
90 # Authentication and protocol headers cannot be accidentally overridden.
91 5 $headers{authorization} = 'Bearer ' . $self->{api_key};
92 5 $headers{accept} = 'application/json';
93 5 ▶ my $content = defined($body) ? eval { encode_json($body) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $body ? : 5 0 5
94 5 ▶ _throw("request body is not JSON encodable: $@") if defined($body) && $@;
Branch: 1 branch
BranchTrueFalseTotal
if defined $body and $@ 0 5 5
Condition33%1/3
defined $body and $@
!ll&&!rl&&r
050
MC/DC0%0/2
defined $body and $@
defined $body $@
Truth table33%1/3
A and B
A= defined $bodyB= $@
ABresult
0X0
100
111
95 5 my $started = time;
96 5 my $retry_number = 0;
97 5 my $last_error;
98 5 while (1) {
99 6 my $response;
100 6 my $ok = eval {
101 6 ▶ my $http = $self->{http} || HTTP::Tiny->new(timeout => $timeout);
Condition33%1/3
$self->{'http'} || 'HTTP::Tiny'->new('timeout', $timeout)
l!l&&r!l&&!r
600
MC/DC0%0/2
$self->{'http'} || 'HTTP::Tiny'->new('timeout', $timeout)
$self->{'http'} 'HTTP::Tiny'->new('timeout', $timeout)
Truth table33%1/3
A || B
A= $self->{'http'}B= 'HTTP::Tiny'->new('timeout', $timeout)
ABresult
1X1
011
000
102 6 my %request = (headers => \%headers);
103 6 ▶ $request{content} = $content if defined $content;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content 6 0 6
104 6 ▶ $response = ref($http) eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
ref $http eq 'CODE' ? : 6 0 6
105 ? $http->($method, $url, \%request)
106 : $http->request($method, $url, \%request);
107 6 1;
108 };
109 6 ▶ if (!$ok || !$response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok and $response) 0 6 6
Condition33%1/3
$ok and $response
!ll&&!rl&&r
006
MC/DC0%0/2
$ok and $response
$ok $response
Truth table33%1/3
A and B
A= $okB= $response
ABresult
0X0
100
111
110 0 ▶ my $error = WebService::TypeSafe::ConnectionError->new(
Condition0%0/2
$@ || 'no response'
l!l
00
MC/DC0%0/1
$@ || 'no response'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
111 message => 'connection to TypeSafe API failed: ' . ($@ || 'no response'),
112 endpoint => "$method $url",
113 );
114 0 $last_error = $error;
115 0 ▶ if ($retry->{connection_errors} && $retry_number < $retry->max_retries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry->{"connection_errors"} and $retry_number < $retry->max_retries) 0 0 0
Condition0%0/3
$retry->{"connection_errors"} and $retry_number < $retry->max_retries
!ll&&!rl&&r
000
MC/DC0%0/2
$retry->{"connection_errors"} and $retry_number < $retry->max_retries
$retry->{"connection_errors"} $retry_number < $retry->max_retries
Truth table0%0/3
A and B
A= $retry->{"connection_errors"}B= $retry_number < $retry->max_retries
ABresult
0X0
100
111
116 0 my $delay = $retry->delay(++$retry_number, {});
117 0 ▶ last if defined($retry->timeout) && time - $started + $delay >= $retry->timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $retry->timeout and &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout 0 0 0
Condition0%0/3
defined $retry->timeout and &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
!ll&&!rl&&r
000
MC/DC0%0/2
defined $retry->timeout and &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
defined $retry->timeout &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
Truth table0%0/3
A and B
A= defined $retry->timeoutB= &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
ABresult
0X0
100
111
118 0 $self->{sleeper}->($delay); next;
119 }
120 0 die $error;
121 }
122 2 my %response_headers = map { lc($_) => $response->{headers}{$_} }
123 6 ▶ keys %{ $response->{headers} || {} };
Condition50%1/2
$response->{'headers'} || {}
l!l
60
MC/DC0%0/1
$response->{'headers'} || {}
$response->{'headers'}
Truth table50%1/2
A
A= $response->{'headers'}
Aresult
11
00
124 # HTTP::Tiny represents transport failures as synthetic HTTP 599 responses.
125 6 ▶ if (($response->{status} || 0) == 599) {
Branch: 1 branch
BranchTrueFalseTotal
if (($response->{"status"} || 0) == 599) 1 5 6
Condition50%1/2
$response->{'status'} || 0
l!l
60
MC/DC0%0/1
$response->{'status'} || 0
$response->{'status'}
Truth table50%1/2
A
A= $response->{'status'}
Aresult
11
00
126 1 ▶ my $is_timeout = ($response->{content} // '') =~ /tim(?:e|ed)\s*out/i;
Condition50%1/2
$response->{"content"} // ""
l!l
10
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table50%1/2
A
A= $response->{"content"}
Aresult
11
00
127 1 ▶ my $class = $is_timeout
Branch: 1 branch
BranchTrueFalseTotal
$is_timeout ? : 1 0 1
128 ? 'WebService::TypeSafe::TimeoutError' : 'WebService::TypeSafe::ConnectionError';
129 my %fields = (
130 1 ▶ message => 'connection to TypeSafe API failed: ' . ($response->{content} // $response->{reason} // 'unknown error'),
Condition40%2/5
($response->{"content"} // $response->{"reason"}) // "unknown error"
l!l
10
$response->{"content"} // $response->{"reason"}
l!l&&r!l&&!r
100
MC/DC0%0/2
($response->{"content"} // $response->{"reason"}) // "unknown error"
$response->{"content"} $response->{"reason"}
Truth table33%1/3
A // B
A= $response->{"content"}B= $response->{"reason"}
ABresult
1X1
011
000
131 endpoint => "$method $url",
132 );
133 1 ▶ $fields{timeout} = $timeout if $is_timeout;
Branch: 1 branch
BranchTrueFalseTotal
if $is_timeout 1 0 1
134 1 $last_error = $class->new(%fields);
135 1 ▶ if ($retry->{connection_errors} && $retry_number < $retry->max_retries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry->{"connection_errors"} and $retry_number < $retry->max_retries) 0 1 1
Condition33%1/3
$retry->{"connection_errors"} and $retry_number < $retry->max_retries
!ll&&!rl&&r
010
MC/DC0%0/2
$retry->{"connection_errors"} and $retry_number < $retry->max_retries
$retry->{"connection_errors"} $retry_number < $retry->max_retries
Truth table33%1/3
A and B
A= $retry->{"connection_errors"}B= $retry_number < $retry->max_retries
ABresult
0X0
100
111
136 0 my $delay = $retry->delay(++$retry_number, \%response_headers);
137 0 ▶ last if defined($retry->timeout) && time - $started + $delay >= $retry->timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $retry->timeout and &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout 0 0 0
Condition0%0/3
defined $retry->timeout and &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
!ll&&!rl&&r
000
MC/DC0%0/2
defined $retry->timeout and &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
defined $retry->timeout &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
Truth table0%0/3
A and B
A= defined $retry->timeoutB= &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
ABresult
0X0
100
111
138 0 $self->{sleeper}->($delay); next;
139 }
140 1 die $last_error;
141 }
142 5 ▶ if ($response->{success}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->{"success"}) 2 3 5
143 2 ▶ my $data = eval { decode_json($response->{content} // '') };
Condition50%1/2
$response->{"content"} // ""
l!l
20
MC/DC0%0/1
$response->{"content"} // ""
$response->{"content"}
Truth table50%1/2
A
A= $response->{"content"}
Aresult
11
00
144 2 ▶ if ($@ || ref($data) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or ref $data ne "HASH") 0 2 2
Condition33%1/3
$@ or ref $data ne "HASH"
l!l&&r!l&&!r
002
MC/DC0%0/2
$@ or ref $data ne "HASH"
$@ ref $data ne "HASH"
Truth table33%1/3
A or B
A= $@B= ref $data ne "HASH"
ABresult
1X1
011
000
145 die WebService::TypeSafe::ResponseValidationError->new(
146 message => 'TypeSafe API returned invalid JSON', status => $response->{status},
147 0 body => $response->{content}, headers => \%response_headers,
148 endpoint => "$method $url", field_path => '',
149 );
150 }
151 2 return $data;
152 }
153 3 ▶ my $status = $response->{status} || 0;
Condition50%1/2
$response->{'status'} || 0
l!l
30
MC/DC0%0/1
$response->{'status'} || 0
$response->{'status'}
Truth table50%1/2
A
A= $response->{'status'}
Aresult
11
00
154 3 $last_error = _api_error($status, $response->{content}, \%response_headers, "$method $url");
155 3 ▶ if ($retry->should_retry_status($status) && $retry_number < $retry->max_retries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retry->should_retry_status($status) and $retry_number < $retry->max_retries) 2 1 3
Condition66%2/3
$retry->should_retry_status($status) and $retry_number < $retry->max_retries
!ll&&!rl&&r
102
MC/DC50%1/2
$retry->should_retry_status($status) and $retry_number < $retry->max_retries
$retry->should_retry_status($status) $retry_number < $retry->max_retries
Truth table66%2/3
A and B
A= $retry->should_retry_status($status)B= $retry_number < $retry->max_retries
ABresult
0X0
100
111
156 2 my $delay = $retry->delay(++$retry_number, \%response_headers);
157 2 ▶ last if defined($retry->timeout) && time - $started + $delay >= $retry->timeout;
Branch: 1 branch
BranchTrueFalseTotal
if defined $retry->timeout and &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout 1 1 2
Condition66%2/3
defined $retry->timeout and &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
!ll&&!rl&&r
101
MC/DC50%1/2
defined $retry->timeout and &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
defined $retry->timeout &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
Truth table66%2/3
A and B
A= defined $retry->timeoutB= &WebService::TypeSafe::Client::time() - $started + $delay >= $retry->timeout
ABresult
0X0
100
111
158 1 $self->{sleeper}->($delay); next;
159 }
160 1 die $last_error;
161 }
162 1 die $last_error;
163 }
164  
165 3 ▶sub _api_error {
Subroutine _api_error: called CC=10 SCAR=25.2
166 3 my ($status, $content, $headers, $endpoint) = @_;
167 3 ▶ my $body = eval { decode_json($content // '') };
Condition50%1/2
$content // ""
l!l
30
MC/DC0%0/1
$content // ""
$content
Truth table50%1/2
A
A= $content
Aresult
11
00
168 3 ▶ $body = $content if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 3 3
169 3 ▶ my $class = $status == 400 ? 'WebService::TypeSafe::BadRequestError'
Branch: 7 branches
BranchTrueFalseTotal
$status == 400 ? : 0 3 3
$status == 401 ? : 1 2 3
$status == 403 ? : 0 2 2
$status == 404 ? : 0 2 2
$status == 422 ? : 0 2 2
$status == 429 ? : 1 1 2
$status >= 500 ? : 1 0 1
170 : $status == 401 ? 'WebService::TypeSafe::AuthenticationError'
171 : $status == 403 ? 'WebService::TypeSafe::PermissionDeniedError'
172 : $status == 404 ? 'WebService::TypeSafe::NotFoundError'
173 : $status == 422 ? 'WebService::TypeSafe::UnprocessableEntityError'
174 : $status == 429 ? 'WebService::TypeSafe::RateLimitError'
175 : $status >= 500 ? 'WebService::TypeSafe::InternalServerError'
176 : 'WebService::TypeSafe::APIError';
177 3 return $class->new(
178 message => "TypeSafe API request failed with HTTP $status",
179 status => $status, body => $body, headers => $headers, endpoint => $endpoint,
180 );
181 }
182  
183 2 ▶sub _validate_response {
Subroutine _validate_response: called CC=5 SCAR=20.9
184 2 my ($data, $endpoint) = @_;
185 2 my ($path, $message);
186 0 ▶ if (!defined $data->{model}) { ($path, $message) = ('model', 'missing model') }
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $data->{'model'}) { } 0 2 2
elsif (ref $data->{'answers'} ne 'HASH') { } 0 2 2
elsif (ref $data->{'usage'} ne 'HASH') { } 0 2 2
187 0 elsif (ref($data->{answers}) ne 'HASH') { ($path, $message) = ('answers', 'missing answers object') }
188 0 elsif (ref($data->{usage}) ne 'HASH') { ($path, $message) = ('usage', 'missing usage object') }
189 2 ▶ if ($message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($message) 0 2 2
190 0 die WebService::TypeSafe::ResponseValidationError->new(
191 message => "invalid TypeSafe response: $message", status => 200,
192 body => $data, headers => {}, endpoint => "POST $endpoint", field_path => $path,
193 );
194 }
195 }
196  
197 0 ▶sub _throw { die WebService::TypeSafe::Error->new(message => $_[0]) }
Subroutine _throw: not called CC=1 SCAR=6.9
198 0 ▶sub close { return }
Subroutine close: not called CC=1 SCAR=6.9
Pod undocumented
199  
200 3 1;
↑ Summary TypeSafe/RetryPolicy.pm »