blib/lib/HTTP/API/Core.pm

filter: Statementstmt 97.7% 353 / 361 Branchbran 89.9% 232 / 258 Conditioncond 84.0% 174 / 207 MC/DCmcdc 66.0% 68 / 103 Subroutinesub 92.6% 38 / 41 Podpod 66.6% 8 / 12 totaltotal 88.9% 873 / 982 CC 210 SCAR 32.0
CC
210
Cov
92%
request
57.7
_request_once
34.4
new
23.2
CRAP 24.6
« blib/lib/HTTP/API/Core/Pagination.pm ↑ Summary blib/lib/HTTP/API/Core/RateLimit.pm »
1 package HTTP::API::Core;
2  
3 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶use JSON::PP qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use Time::HiRes qw(sleep time);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 39 ▶use Time::Local qw(timegm);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 39 ▶use HTTP::API::Core::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 39 ▶use HTTP::API::Core::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 39 ▶use HTTP::API::Core::Pagination;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 39 our $VERSION = '1.15';
16  
17 193 ▶sub new {
Subroutine new: called CC=23 SCAR=31.5
Pod documented
18 193 my $class = shift;
19 193 ▶ die "constructor options must be key/value pairs\n" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 193 193
20 die "constructor option names must be scalars\n"
21 193 ▶ if grep { ref($_) ne '' } @_[ grep { $_ % 2 == 0 } 0 .. $#_ ];
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} @_[grep {$_ % 2 == 0;} 0 .. $#_] 0 193 193
22 193 my %args = @_;
23  
24 193 my $base_url = delete $args{base_url};
25 193 my $base_url_ref = ref($base_url);
26 193 ▶ die "base_url is required\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $base_url or $base_url_ref eq "" and $base_url eq "" 1 192 193
Condition66%4/6
not defined $base_url or $base_url_ref eq "" and $base_url eq ""
l!l&&r!l&&!r
10192
$base_url_ref eq "" and $base_url eq ""
!ll&&!rl&&r
21900
MC/DC33%1/3
not defined $base_url or $base_url_ref eq "" and $base_url eq ""
not defined $base_url $base_url_ref eq "" $base_url eq ""
Truth table75%3/4
not(A) or B and C
A= not defined $base_urlB= $base_url_ref eq ""C= $base_url eq ""
ABCresult
1XX1
0111
00X0
0100
27 if !defined($base_url) || ($base_url_ref eq '' && $base_url eq '');
28 192 ▶ die "base_url must be a non-empty scalar\n" if $base_url_ref ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $base_url_ref ne "" 2 190 192
29 190 $base_url =~ s{/+\z}{};
30  
31 190 ▶ my $headers = exists($args{headers}) ? delete($args{headers}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'headers'} ? : 14 176 190
32 190 ▶ die "headers must be a hash reference\n" if ref($headers) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers ne "HASH" 1 189 190
33 189 _validate_header_values($headers);
34  
35 187 ▶ my $timeout = exists $args{timeout} ? delete($args{timeout}) : 10;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'timeout'} ? : 9 178 187
36 187 my $timeout_ref = ref($timeout);
37 187 ▶ die "timeout must be a positive number\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $timeout or $timeout_ref ne "" or not $timeout =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/ or $timeout <= 0 6 181 187
Condition88%8/9
not defined $timeout or $timeout_ref ne "" or not $timeout =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/ or $timeout <= 0
l!l&&r!l&&!r
51181
not defined $timeout or $timeout_ref ne "" or not $timeout =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/
l!l&&r!l&&!r
23182
not defined $timeout or $timeout_ref ne ""
l!l&&r!l&&!r
02185
MC/DC75%3/4
not defined $timeout or $timeout_ref ne "" or not $timeout =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/ or $timeout <= 0
not defined $timeout $timeout_ref ne "" not $timeout =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/ $timeout <= 0
Truth table80%4/5
not(A) or B or not(C) or D
A= not defined $timeoutB= $timeout_ref ne ""C= not $timeout =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/D= $timeout <= 0
ABCDresult
1XXX1
01XX1
001X1
00011
00000
38 if !defined($timeout)
39 || $timeout_ref ne ''
40 || $timeout !~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/
41 || $timeout <= 0;
42  
43 181 my $transport = delete $args{transport};
44 181 my $transport_class = blessed($transport);
45 181 ▶ die "transport must be a code reference or object with request()\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $transport and ref $transport ne "CODE" and not defined $transport_class && $transport->can("request") 3 178 181
Condition100%9/9
defined $transport and ref $transport ne "CODE" and not defined $transport_class && $transport->can("request")
!ll&&!rl&&r
17443
defined $transport and ref $transport ne "CODE"
!ll&&!rl&&r
251497
defined $transport_class && $transport->can('request')
!ll&&!rl&&r
214
MC/DC100%4/4
defined $transport and ref $transport ne "CODE" and not defined $transport_class && $transport->can("request")
defined $transport ref $transport ne "CODE" defined $transport_class $transport->can('request')
Truth table100%5/5
A and B and not(C && D)
A= defined $transportB= ref $transport ne "CODE"C= defined $transport_classD= $transport->can('request')
ABCDresult
0XXX0
10XX0
11110
110X1
11101
46 if defined($transport)
47 && ref($transport) ne 'CODE'
48 && !(defined($transport_class) && $transport->can('request'));
49  
50 178 ▶ my $retry = exists $args{retry} ? delete($args{retry}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'retry'} ? : 106 72 178
51 178 ▶ die "retry must be a hash reference\n" if ref($retry) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $retry ne "HASH" 0 178 178
52 178 $retry = _normalize_retry($retry);
53  
54 156 ▶ my $hooks = exists $args{hooks} ? delete($args{hooks}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'hooks'} ? : 63 93 156
55 156 $hooks = _normalize_hooks($hooks);
56  
57 151 die "unknown constructor option: $_\n" for sort keys %args;
58  
59 150 my $self = bless {
60 base_url => $base_url,
61 headers => { %$headers },
62 timeout => $timeout,
63 transport => $transport,
64 retry => $retry,
65 hooks => $hooks,
66 }, $class;
67  
68 150 ▶ $self->{http} = HTTP::Tiny->new(timeout => $timeout) if !defined $transport;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $transport 6 144 150
69 150 return $self;
70 }
71  
72 6 ▶sub base_url { $_[0]->{base_url} }
Subroutine base_url: called CC=1 SCAR=0.0
Pod undocumented
73 4 ▶sub timeout { $_[0]->{timeout} }
Subroutine timeout: called CC=1 SCAR=0.0
Pod undocumented
74 10 ▶sub retry { +{ %{ $_[0]->{retry} }, methods => [ @{ $_[0]->{retry}{methods} } ] } }
Subroutine retry: called CC=1 SCAR=0.0
Pod undocumented
75 2 ▶sub hooks { _clone_hooks($_[0]->{hooks}) }
Subroutine hooks: called CC=1 SCAR=0.0
Pod undocumented
76  
77 161 ▶sub get { my ($self, $path) = splice @_, 0, 2; return $self->request('GET', $path, @_) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
78 32 ▶sub post { my ($self, $path) = splice @_, 0, 2; return $self->request('POST', $path, @_) }
Subroutine post: called CC=1 SCAR=0.0
Pod documented
79 0 ▶sub put { my ($self, $path) = splice @_, 0, 2; return $self->request('PUT', $path, @_) }
Subroutine put: not called CC=1 SCAR=6.9
Pod documented
80 0 ▶sub patch { my ($self, $path) = splice @_, 0, 2; return $self->request('PATCH', $path, @_) }
Subroutine patch: not called CC=1 SCAR=6.9
Pod documented
81 0 ▶sub delete { my ($self, $path) = splice @_, 0, 2; return $self->request('DELETE', $path, @_) }
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
82  
83 10 ▶sub paginate {
Subroutine paginate: called CC=1 SCAR=0.0
Pod documented
84 10 my ($self, $path) = splice @_, 0, 2;
85 10 return HTTP::API::Core::Pagination->new(
86 client => $self,
87 path => $path,
88 @_,
89 );
90 }
91  
92 200 ▶sub request {
Subroutine request: called CC=56 SCAR=40.6
Pod documented
93 200 my ($self, $method, $path) = splice @_, 0, 3;
94 200 ▶ die "request options must be key/value pairs\n" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 200 200
95 die "request option names must be scalars\n"
96 200 ▶ if grep { ref($_) ne '' } @_[ grep { $_ % 2 == 0 } 0 .. $#_ ];
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} @_[grep {$_ % 2 == 0;} 0 .. $#_] 0 200 200
97 200 my %opts = @_;
98 200 my $method_ref = ref($method);
99 200 ▶ die "method is required\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $method or $method_ref eq "" and $method eq "" 1 199 200
Condition83%5/6
not defined $method or $method_ref eq "" and $method eq ""
l!l&&r!l&&!r
01199
$method_ref eq "" and $method eq ""
!ll&&!rl&&r
21971
MC/DC66%2/3
not defined $method or $method_ref eq "" and $method eq ""
not defined $method $method_ref eq "" $method eq ""
Truth table75%3/4
not(A) or B and C
A= not defined $methodB= $method_ref eq ""C= $method eq ""
ABCresult
1XX1
0111
00X0
0100
100 if !defined($method) || ($method_ref eq '' && $method eq '');
101 199 ▶ die "method must be a non-empty scalar\n" if $method_ref ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $method_ref ne "" 2 197 199
102 197 ▶ die "path is required\n" if !defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 1 196 197
103 196 my $path_ref = ref($path);
104 196 ▶ die "path must be a scalar\n" if $path_ref ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $path_ref ne "" 2 194 196
105 194 $method = uc($method);
106  
107 # URI schemes are case-insensitive; align with request URL validation and
108 # the pagination continuation origin guard.
109 194 ▶ my $url = $path =~ m{\Ahttps?://}i ? $path : $self->_join_url($path);
Branch: 1 branch
BranchTrueFalseTotal
$path =~ m[\Ahttps?://]i ? : 8 186 194
110  
111 194 ▶ my $query = exists $opts{query} ? delete($opts{query}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'query'} ? : 17 177 194
112 194 ▶ die "query must be a hash reference\n" if ref($query) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $query ne "HASH" 2 192 194
113 192 $url = _append_query($url, $query);
114  
115 190 ▶ my $request_headers = exists $opts{headers} ? delete($opts{headers}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'headers'} ? : 18 172 190
116 190 ▶ die "headers must be a hash reference\n" if ref($request_headers) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $request_headers ne "HASH" 1 189 190
117 189 _validate_header_values($request_headers);
118 185 my %headers = %{ $self->{headers} };
119 # A header hash may contain multiple spellings of the same name.
120 # Matching values are safe; deterministic spelling avoids hash-order drift.
121 185 for my $name (sort keys %$request_headers) {
122 16 my $wanted = lc $name;
123 16 delete @headers{ grep { lc($_) eq $wanted } keys %headers };
124 16 $headers{$name} = $request_headers->{$name};
125 }
126  
127 185 ▶ if (exists $opts{idempotency}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"idempotency"}) 11 174 185
128 11 my $idempotency = delete $opts{idempotency};
129 11 ▶ die "idempotency must be a hash reference\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $idempotency ne "HASH" 1 10 11
130 if ref($idempotency) ne 'HASH';
131  
132 10 my %copy = %$idempotency;
133 10 my $key = delete $copy{key};
134 10 my $header = delete $copy{header};
135  
136 10 my $key_ref = ref($key);
137 10 ▶ die "idempotency key must be a non-empty scalar\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key_ref ne "" or $key eq "" 2 8 10
Condition83%5/6
not defined $key or $key_ref ne "" or $key eq ""
l!l&&r!l&&!r
118
not defined $key or $key_ref ne ""
l!l&&r!l&&!r
109
MC/DC66%2/3
not defined $key or $key_ref ne "" or $key eq ""
not defined $key $key_ref ne "" $key eq ""
Truth table75%3/4
not(A) or B or C
A= not defined $keyB= $key_ref ne ""C= $key eq ""
ABCresult
1XX1
01X1
0011
0000
138 if !defined($key) || $key_ref ne '' || $key eq '';
139 8 my $header_ref = ref($header);
140 8 ▶ die "idempotency header must be a non-empty scalar\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $header or $header_ref ne "" or $header eq "" 2 6 8
Condition83%5/6
not defined $header or $header_ref ne "" or $header eq ""
l!l&&r!l&&!r
116
not defined $header or $header_ref ne ""
l!l&&r!l&&!r
107
MC/DC66%2/3
not defined $header or $header_ref ne "" or $header eq ""
not defined $header $header_ref ne "" $header eq ""
Truth table75%3/4
not(A) or B or C
A= not defined $headerB= $header_ref ne ""C= $header eq ""
ABCresult
1XX1
01X1
0011
0000
141 if !defined($header) || $header_ref ne '' || $header eq '';
142 6 die "unknown idempotency option: $_\n" for sort keys %copy;
143  
144 5 my $wanted = lc $header;
145 5 my $already = grep { lc($_) eq $wanted } keys %headers;
146 5 ▶ $headers{$header} = "$key" if !$already;
Branch: 1 branch
BranchTrueFalseTotal
unless $already 4 1 5
147 }
148  
149 die "json and content request options are mutually exclusive\n"
150 179 ▶ if exists($opts{json}) && exists($opts{content});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"json"} and exists $opts{"content"} 1 178 179
Condition100%3/3
exists $opts{"json"} and exists $opts{"content"}
!ll&&!rl&&r
168101
MC/DC100%2/2
exists $opts{"json"} and exists $opts{"content"}
exists $opts{"json"} exists $opts{"content"}
Truth table100%3/3
A and B
A= exists $opts{"json"}B= exists $opts{"content"}
ABresult
0X0
100
111
151  
152 178 my $content;
153  
154 178 ▶ if (exists $opts{json}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts{'json'}) { } 10 168 178
elsif (exists $opts{'content'}) { } 9 159 168
155 10 my $value = delete $opts{json};
156 10 $content = eval { encode_json($value) };
157 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 9 10
158 1 die HTTP::API::Core::Error->new(
159 category => 'encode',
160 method => $method,
161 url => $url,
162 message => "failed to encode JSON request: $@",
163 );
164 }
165 9 _set_header_if_absent(\%headers, 'content-type', 'application/json');
166 9 _set_header_if_absent(\%headers, 'accept', 'application/json');
167 }
168 elsif (exists $opts{content}) {
169 9 $content = delete $opts{content};
170 }
171  
172 177 ▶ my $retry = exists $opts{retry} ? delete($opts{retry}) : $self->{retry};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'retry'} ? : 10 167 177
173 177 my $retry_ref = ref($retry);
174 177 ▶ if ($retry_ref eq 'HASH' && $retry != $self->{retry}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($retry_ref eq 'HASH' and $retry != $self->{'retry'}) { } 9 168 177
elsif ($retry_ref eq '') { } 1 167 168
elsif ($retry_ref ne 'HASH') { } 0 167 167
Condition100%3/3
$retry_ref eq "HASH" and $retry != $self->{"retry"}
!ll&&!rl&&r
11679
MC/DC100%2/2
$retry_ref eq "HASH" and $retry != $self->{"retry"}
$retry_ref eq "HASH" $retry != $self->{"retry"}
Truth table100%3/3
A and B
A= $retry_ref eq "HASH"B= $retry != $self->{"retry"}
ABresult
0X0
100
111
175 9 $retry = _normalize_retry($retry);
176 }
177 elsif ($retry_ref eq '') {
178 1 ▶ $retry = $retry ? $self->{retry} : _normalize_retry({ attempts => 1 });
Branch: 1 branch
BranchTrueFalseTotal
$retry ? : 0 1 1
179 }
180 elsif ($retry_ref ne 'HASH') {
181 0 die "retry must be a hash reference or scalar switch\n";
182 }
183  
184 my $request_hooks = exists $opts{hooks}
185 171 ▶ ? _normalize_hooks(delete($opts{hooks}))
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'hooks'} ? : 3 168 171
186 : {};
187 170 my $hooks = _merge_hooks($self->{hooks}, $request_hooks);
188  
189 170 die "unknown request option: $_\n" for sort keys %opts;
190  
191 my $attempts = _method_is_retryable($method, $retry)
192 ? $retry->{attempts}
193 169 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
_method_is_retryable($method, $retry) ? : 148 21 169
194 169 my $attempt = 0;
195  
196 169 ▶ while (++$attempt <= $attempts) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$attempt <= $attempts) 195 0 195
197 195 my $context = {
198 method => $method,
199 url => $url,
200 headers => { %headers },
201 content => $content,
202 attempt => $attempt,
203 };
204  
205 195 my $hook_error = _run_hooks($hooks->{before_request}, $context);
206 195 ▶ die _hook_error($hook_error, $method, $url) if defined $hook_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hook_error 5 190 195
207 190 my $context_method_ref = ref($context->{method});
208 die "method must be a non-empty scalar\n"
209 190 ▶ if !defined($context->{method}) || $context_method_ref ne '' || $context->{method} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $context->{"method"} or $context_method_ref ne "" or $context->{"method"} eq "" 1 189 190
Condition66%4/6
not defined $context->{"method"} or $context_method_ref ne "" or $context->{"method"} eq ""
l!l&&r!l&&!r
10189
not defined $context->{"method"} or $context_method_ref ne ""
l!l&&r!l&&!r
01189
MC/DC33%1/3
not defined $context->{"method"} or $context_method_ref ne "" or $context->{"method"} eq ""
not defined $context->{"method"} $context_method_ref ne "" $context->{"method"} eq ""
Truth table50%2/4
not(A) or B or C
A= not defined $context->{"method"}B= $context_method_ref ne ""C= $context->{"method"} eq ""
ABCresult
1XX1
01X1
0011
0000
210 # Hooks may supply a different method spelling. Normalize the actual
211 # dispatched method before transport, metadata, and retry decisions.
212 189 $context->{method} = uc($context->{method});
213 189 _validate_request_url($context->{url});
214 die "headers must be a hash reference\n"
215 181 ▶ if ref($context->{headers}) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $context->{"headers"} ne "HASH" 0 181 181
216 181 _validate_header_values($context->{headers});
217 179 my $context_content_ref = ref($context->{content});
218 die "content must be a scalar or undef\n"
219 179 ▶ if defined($context->{content}) && $context_content_ref ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $context->{"content"} and $context_content_ref ne "" 1 178 179
Condition100%3/3
defined $context->{"content"} and $context_content_ref ne ""
!ll&&!rl&&r
160181
MC/DC100%2/2
defined $context->{"content"} and $context_content_ref ne ""
defined $context->{"content"} $context_content_ref ne ""
Truth table100%3/3
A and B
A= defined $context->{"content"}B= $context_content_ref ne ""
ABresult
0X0
100
111
220  
221 # Hooks may change the method. Keep the actual dispatched method for
222 # retry eligibility even if on_error hooks later mutate the context.
223 178 my $dispatched_method = $context->{method};
224 178 my $started_at = time;
225 178 $context->{started_at} = $started_at;
226  
227 my ($response, $error, $elapsed) = $self->_request_once(
228 $dispatched_method,
229 $context->{url},
230 $context->{headers},
231 $context->{content},
232 178 );
233  
234 178 $context->{elapsed} = $elapsed;
235 178 ▶ $context->{request_id} = defined($response)
Branch: 2 branches
BranchTrueFalseTotal
defined $response ? : 93 85 178
defined $error ? : 85 0 85
236 ? $response->request_id
237 : defined($error) ? $error->request_id : undef;
238  
239 178 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 93 85 178
240 my $after_error = _run_hooks(
241 $hooks->{after_response},
242 93 $response,
243 $context,
244 );
245 die _hook_error(
246 $after_error,
247 $context->{method},
248 $context->{url},
249 93 ▶ ) if defined $after_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $after_error 0 93 93
250 93 return $response;
251 }
252  
253 85 my $on_error_error = _run_hooks($hooks->{on_error}, $error, $context);
254 die _hook_error(
255 $on_error_error,
256 $context->{method},
257 $context->{url},
258 85 ▶ ) if defined $on_error_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_error_error 0 85 85
259  
260 85 ▶ die $error if $attempt >= $attempts
Branch: 1 branch
BranchTrueFalseTotal
if $attempt >= $attempts or not $error->retryable or not _method_is_retryable($dispatched_method, $retry) 59 26 85
Condition100%6/6
$attempt >= $attempts or not $error->retryable or not _method_is_retryable($dispatched_method, $retry)
l!l&&r!l&&!r
54526
$attempt >= $attempts or not $error->retryable
l!l&&r!l&&!r
47731
MC/DC100%3/3
$attempt >= $attempts or not $error->retryable or not _method_is_retryable($dispatched_method, $retry)
$attempt >= $attempts not $error->retryable not _method_is_retryable($dispatched_method, $retry)
Truth table100%4/4
A or not(B) or not(C)
A= $attempt >= $attemptsB= not $error->retryableC= not _method_is_retryable($dispatched_method, $retry)
ABCresult
1XX1
01X1
0011
0000
261 || !$error->retryable
262 || !_method_is_retryable($dispatched_method, $retry);
263  
264 26 my $delay = _retry_delay($retry, $attempt, $error);
265 26 ▶ sleep($delay) if $delay > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > 0 0 26 26
266 }
267  
268 0 die "unreachable retry state\n";
269 }
270  
271 178 ▶sub _request_once {
Subroutine _request_once: called CC=34 SCAR=35.4
272 178 my ($self, $method, $url, $headers, $content) = @_;
273 178 my $raw;
274 178 my $started_at = time;
275  
276 eval {
277 178 ▶ if (defined $self->{transport}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'transport'}) { } 172 6 178
278 172 ▶ my $request_opts = {
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 17 155 172
279 headers => $headers,
280 (defined($content) ? (content => $content) : ()),
281 };
282  
283 172 ▶ if (ref($self->{transport}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'transport'} eq 'CODE') { } 168 4 172
284 168 $raw = $self->{transport}->($method, $url, $request_opts);
285 }
286 else {
287 4 $raw = $self->{transport}->request($method, $url, $request_opts);
288 }
289 }
290 else {
291 6 ▶ $raw = $self->{http}->request($method, $url, {
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 1 5 6
292 headers => $headers,
293 (defined($content) ? (content => $content) : ()),
294 });
295 }
296 169 1;
297 178 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { if (defined $self->{"transport"}) { my $request_opts = {"headers", $headers, defined $content ? ("content", $content) : ()}; if (ref $self->{"transport"} eq "CODE") { $raw = $self->{"transport"}->($method, $url, $request_opts); } else { $raw = $self->{"transport"}->request($method, $url, $request_opts); } ; } else { $raw = $self->{"http"}->request($method, $url, {"headers", $headers, defined $content ? ("content", $content) : ()}); } ; 1 } } or do { my $cause = $@; my $cause_class = builtin::blessed($cause); return undef, $cause, &HTTP::API::Core::time() - $started_at if defined $cause_class and $cause->isa("HTTP::API::Core::Error"); my $elapsed = &HTTP::API::Core::time() - $started_at; return undef, "HTTP::API::Core::Error"->new("category", "transport", "method", $method, "url", $url, "retryable", 1, "elapsed", $elapsed, "message", "HTTP transport failed: $cause"), $elapsed } 169 9 178
298 9 my $cause = $@;
299 9 my $cause_class = blessed($cause);
300 9 ▶ return (undef, $cause, time - $started_at)
Branch: 1 branch
BranchTrueFalseTotal
if defined $cause_class and $cause->isa("HTTP::API::Core::Error") 2 7 9
Condition66%2/3
defined $cause_class and $cause->isa("HTTP::API::Core::Error")
!ll&&!rl&&r
702
MC/DC50%1/2
defined $cause_class and $cause->isa("HTTP::API::Core::Error")
defined $cause_class $cause->isa("HTTP::API::Core::Error")
Truth table66%2/3
A and B
A= defined $cause_classB= $cause->isa("HTTP::API::Core::Error")
ABresult
0X0
100
111
301 if defined($cause_class) && $cause->isa('HTTP::API::Core::Error');
302  
303 7 my $elapsed = time - $started_at;
304 7 return (undef, HTTP::API::Core::Error->new(
305 category => 'transport',
306 method => $method,
307 url => $url,
308 retryable => 1,
309 elapsed => $elapsed,
310 message => "HTTP transport failed: $cause",
311 ), $elapsed);
312 };
313  
314 169 ▶ if (ref($raw) ne 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"} or ref $raw->{"status"} ne "" or not $raw->{"status"} =~ /\A[0-9]{3}\z/ or $raw->{"status"} < 100 or $raw->{"status"} > 599 or not _valid_response_headers($raw->{"headers"}) or defined $raw->{"reason"} and ref $raw->{"reason"} ne "" or defined $raw->{"content"} and ref $raw->{"content"} ne "") 14 155 169
Condition93%31/33
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"} or ref $raw->{"status"} ne "" or not $raw->{"status"} =~ /\A[0-9]{3}\z/ or $raw->{"status"} < 100 or $raw->{"status"} > 599 or not _valid_response_headers($raw->{"headers"}) or defined $raw->{"reason"} and ref $raw->{"reason"} ne "" or defined $raw->{"content"} and ref $raw->{"content"} ne ""
l!l&&r!l&&!r
131155
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"} or ref $raw->{"status"} ne "" or not $raw->{"status"} =~ /\A[0-9]{3}\z/ or $raw->{"status"} < 100 or $raw->{"status"} > 599 or not _valid_response_headers($raw->{"headers"}) or defined $raw->{"reason"} and ref $raw->{"reason"} ne ""
l!l&&r!l&&!r
121156
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"} or ref $raw->{"status"} ne "" or not $raw->{"status"} =~ /\A[0-9]{3}\z/ or $raw->{"status"} < 100 or $raw->{"status"} > 599 or not _valid_response_headers($raw->{"headers"})
l!l&&r!l&&!r
84157
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"} or ref $raw->{"status"} ne "" or not $raw->{"status"} =~ /\A[0-9]{3}\z/ or $raw->{"status"} < 100 or $raw->{"status"} > 599
l!l&&r!l&&!r
71161
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"} or ref $raw->{"status"} ne "" or not $raw->{"status"} =~ /\A[0-9]{3}\z/ or $raw->{"status"} < 100
l!l&&r!l&&!r
70162
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"} or ref $raw->{"status"} ne "" or not $raw->{"status"} =~ /\A[0-9]{3}\z/
l!l&&r!l&&!r
43162
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"} or ref $raw->{"status"} ne ""
l!l&&r!l&&!r
40165
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"}
l!l&&r!l&&!r
31165
ref $raw ne "HASH" or not exists $raw->{"status"}
l!l&&r!l&&!r
12166
defined $raw->{"reason"} and ref $raw->{"reason"} ne ""
!ll&&!rl&&r
351211
defined $raw->{"content"} and ref $raw->{"content"} ne ""
!ll&&!rl&&r
21531
MC/DC83%10/12
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"} or ref $raw->{"status"} ne "" or not $raw->{"status"} =~ /\A[0-9]{3}\z/ or $raw->{"status"} < 100 or $raw->{"status"} > 599 or not _valid_response_headers($raw->{"headers"}) or defined $raw->{"reason"} and ref $raw->{"reason"} ne "" or defined $raw->{"content"} and ref $raw->{"content"} ne ""
ref $raw ne "HASH" not exists $raw->{"status"} not defined $raw->{"status"} ref $raw->{"status"} ne "" not $raw->{"status"} =~ /\A[0-9]{3}\z/ $raw->{"status"} < 100 $raw->{"status"} > 599 not _valid_response_headers($raw->{"headers"}) defined $raw->{"reason"} ref $raw->{"reason"} ne "" defined $raw->{"content"} ref $raw->{"content"} ne ""
Truth table73%11/15
A or not(B) or not(C) or D or not(E) or F or G or not(H) or I and J or K and L
A= ref $raw ne "HASH"B= not exists $raw->{"status"}C= not defined $raw->{"status"}D= ref $raw->{"status"} ne ""E= not $raw->{"status"} =~ /\A[0-9]{3}\z/F= $raw->{"status"} < 100G= $raw->{"status"} > 599H= not _valid_response_headers($raw->{"headers"})I= defined $raw->{"reason"}J= ref $raw->{"reason"} ne ""K= defined $raw->{"content"}L= ref $raw->{"content"} ne ""
ABCDEFGHIJKLresult
1XXXXXXXXXXX1
01XXXXXXXXXX1
001XXXXXXXXX1
0001XXXXXXXX1
00001XXXXXXX1
000001XXXXXX1
0000001XXXXX1
00000001XXXX1
0000000011XX1
000000000X111
0000000010111
000000000X0X0
000000000X100
00000000100X0
0000000010100
315 || !exists($raw->{status})
316 || !defined($raw->{status})
317 || ref($raw->{status}) ne ''
318 || $raw->{status} !~ /\A[0-9]{3}\z/
319 || $raw->{status} < 100
320 || $raw->{status} > 599
321 || !_valid_response_headers($raw->{headers})
322 || (defined($raw->{reason}) && ref($raw->{reason}) ne '')
323 || (defined($raw->{content}) && ref($raw->{content}) ne '')) {
324 14 my $elapsed = time - $started_at;
325 14 return (undef, HTTP::API::Core::Error->new(
326 category => 'transport',
327 method => $method,
328 url => $url,
329 retryable => 1,
330 elapsed => $elapsed,
331 message => 'HTTP transport returned an invalid response',
332 ), $elapsed);
333 }
334  
335 # HTTP::Tiny reports socket, TLS, DNS, and timeout failures as a
336 # pseudo-response instead of throwing. Do not treat that sentinel as
337 # an actual HTTP 599 response from the server. Custom transports
338 # keep their own documented response/status contract.
339 155 ▶ if (!defined($self->{transport})
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"transport"} and $raw->{"status"} == 599 and defined $raw->{"reason"} and $raw->{"reason"} eq "Internal Exception") 3 152 155
Condition88%8/9
not defined $self->{"transport"} and $raw->{"status"} == 599 and defined $raw->{"reason"} and $raw->{"reason"} eq "Internal Exception"
!ll&&!rl&&r
15113
not defined $self->{"transport"} and $raw->{"status"} == 599 and defined $raw->{"reason"}
!ll&&!rl&&r
15104
not defined $self->{"transport"} and $raw->{"status"} == 599
!ll&&!rl&&r
14924
MC/DC75%3/4
not defined $self->{"transport"} and $raw->{"status"} == 599 and defined $raw->{"reason"} and $raw->{"reason"} eq "Internal Exception"
not defined $self->{"transport"} $raw->{"status"} == 599 defined $raw->{"reason"} $raw->{"reason"} eq "Internal Exception"
Truth table80%4/5
not(A) and B and C and D
A= not defined $self->{"transport"}B= $raw->{"status"} == 599C= defined $raw->{"reason"}D= $raw->{"reason"} eq "Internal Exception"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
340 && $raw->{status} == 599
341 && defined($raw->{reason})
342 && $raw->{reason} eq 'Internal Exception') {
343 3 my $elapsed = time - $started_at;
344 my $cause = defined($raw->{content}) && length($raw->{content})
345 ? $raw->{content}
346 3 ▶ : $raw->{reason};
Branch: 1 branch
BranchTrueFalseTotal
defined $raw->{'content'} && length $raw->{'content'} ? : 3 0 3
Condition33%1/3
defined $raw->{"content"} and length $raw->{"content"}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $raw->{"content"} and length $raw->{"content"}
defined $raw->{"content"} length $raw->{"content"}
Truth table33%1/3
A and B
A= defined $raw->{"content"}B= length $raw->{"content"}
ABresult
0X0
100
111
347 3 return (undef, HTTP::API::Core::Error->new(
348 category => 'transport',
349 method => $method,
350 url => $url,
351 retryable => 1,
352 elapsed => $elapsed,
353 message => "HTTP::Tiny transport failed: $cause",
354 ), $elapsed);
355 }
356  
357 152 my $elapsed = time - $started_at;
358 my $response = HTTP::API::Core::Response->new(
359 status => 0 + $raw->{status},
360 reason => $raw->{reason},
361 headers => defined($raw->{headers}) ? $raw->{headers} : {},
362 152 ▶ content => defined($raw->{content}) ? $raw->{content} : '',
Branch: 2 branches
BranchTrueFalseTotal
defined $raw->{'headers'} ? : 150 2 152
defined $raw->{'content'} ? : 150 2 152
363 method => $method,
364 url => $url,
365 elapsed => $elapsed,
366 );
367  
368 152 ▶ return ($response, undef, $elapsed) if $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $response->is_success 93 59 152
369  
370 59 my $rate_limit = $response->rate_limit;
371 59 ▶ my $rate_limited = ($response->status == 403 || $response->status == 429)
Condition100%6/6
($response->status == 403 || $response->status == 429) && $rate_limit->exhausted
!ll&&!rl&&r
4676
$response->status == 403 or $response->status == 429
l!l&&r!l&&!r
8546
MC/DC100%3/3
($response->status == 403 || $response->status == 429) && $rate_limit->exhausted
$response->status == 403 $response->status == 429 $rate_limit->exhausted
Truth table100%5/5
A or B && C
A= $response->status == 403B= $response->status == 429C= $rate_limit->exhausted
ABCresult
00X0
1X00
0100
1X11
0111
372 && $rate_limit->exhausted;
373  
374 59 ▶ return (undef, HTTP::API::Core::Error->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $response->reason && length $response->reason ? : 55 4 59
Condition83%5/6
_retryable_status($response->status) || $rate_limited
l!l&&r!l&&!r
4658
defined $response->reason and length $response->reason
!ll&&!rl&&r
4055
MC/DC100%2/2
_retryable_status($response->status) || $rate_limited
_retryable_status($response->status) $rate_limited
MC/DC50%1/2
defined $response->reason and length $response->reason
defined $response->reason length $response->reason
Truth table100%3/3
A || B
A= _retryable_status($response->status)B= $rate_limited
ABresult
1X1
011
000
Truth table66%2/3
A and B
A= defined $response->reasonB= length $response->reason
ABresult
0X0
100
111
375 category => 'http',
376 status => $response->status,
377 method => $method,
378 url => $url,
379 retryable => _retryable_status($response->status) || $rate_limited,
380 retry_after => $response->header('retry-after'),
381 request_id => $response->request_id,
382 elapsed => $elapsed,
383 response => $response,
384 message => sprintf(
385 'HTTP %d%s',
386 $response->status,
387 defined($response->reason) && length($response->reason)
388 ? ' ' . $response->reason
389 : '',
390 ),
391 ), $elapsed);
392 }
393  
394 159 ▶sub _normalize_hooks {
Subroutine _normalize_hooks: called CC=9 SCAR=22.0
395 159 my ($hooks) = @_;
396 159 ▶ $hooks = {} if !defined $hooks;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hooks 0 159 159
397 159 ▶ die "hooks must be a hash reference\n" if ref($hooks) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hooks ne "HASH" 2 157 159
398  
399 157 my %copy = %$hooks;
400 157 ▶ die "hook names must be scalars\n" if grep { ref($_) ne '' } keys %copy;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} keys %copy 0 157 157
401 157 my %normalized;
402 157 for my $name (qw(before_request after_response on_error)) {
403 467 my $value = delete $copy{$name};
404 467 ▶ next if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 395 72 467
405  
406 72 ▶ my @callbacks = ref($value) eq 'ARRAY' ? @$value : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 5 67 72
407 die "hook $name must be a code reference or array reference of code references\n"
408 72 ▶ if grep { ref($_) ne 'CODE' } @callbacks;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "CODE";} @callbacks 2 70 72
409 70 $normalized{$name} = \@callbacks;
410 }
411  
412 155 die "unknown hook: $_\n" for sort keys %copy;
413 153 return \%normalized;
414 }
415  
416 2 ▶sub _clone_hooks {
Subroutine _clone_hooks: called CC=2 SCAR=6.9
417 2 my ($hooks) = @_;
418 return {
419 2 ▶ map { $_ => [ @{ defined($hooks->{$_}) ? $hooks->{$_} : [] } ] }
Branch: 1 branch
BranchTrueFalseTotal
defined $hooks->{$_} ? : 2 4 6
420 qw(before_request after_response on_error)
421 };
422 }
423  
424 170 ▶sub _merge_hooks {
Subroutine _merge_hooks: called CC=3 SCAR=11.0
425 170 my ($first, $second) = @_;
426 return {
427 map {
428 170 $_ => [
429 510 ▶ @{ defined($first->{$_}) ? $first->{$_} : [] },
Branch: 1 branch
BranchTrueFalseTotal
defined $first->{$_} ? : 73 437 510
430 510 ▶ @{ defined($second->{$_}) ? $second->{$_} : [] },
Branch: 1 branch
BranchTrueFalseTotal
defined $second->{$_} ? : 2 508 510
431 ]
432 } qw(before_request after_response on_error)
433 };
434 }
435  
436 373 ▶sub _run_hooks {
Subroutine _run_hooks: called CC=4 SCAR=13.9
437 373 my ($callbacks, @args) = @_;
438 373 ▶ for my $callback (@{ defined($callbacks) ? $callbacks : [] }) {
Branch: 1 branch
BranchTrueFalseTotal
defined $callbacks ? : 373 0 373
439 83 my $ok = eval { $callback->(@args); 1 };
440 83 ▶ return $@ if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 5 78 83
441 }
442 368 return undef;
443 }
444  
445 5 ▶sub _hook_error {
Subroutine _hook_error: called CC=3 SCAR=11.0
446 5 my ($cause, $method, $url) = @_;
447 5 my $cause_class = blessed($cause);
448 5 ▶ return $cause if defined($cause_class) && $cause->isa('HTTP::API::Core::Error');
Branch: 1 branch
BranchTrueFalseTotal
if defined $cause_class and $cause->isa("HTTP::API::Core::Error") 2 3 5
Condition66%2/3
defined $cause_class and $cause->isa("HTTP::API::Core::Error")
!ll&&!rl&&r
302
MC/DC50%1/2
defined $cause_class and $cause->isa("HTTP::API::Core::Error")
defined $cause_class $cause->isa("HTTP::API::Core::Error")
Truth table66%2/3
A and B
A= defined $cause_classB= $cause->isa("HTTP::API::Core::Error")
ABresult
0X0
100
111
449 3 return HTTP::API::Core::Error->new(
450 category => 'hook',
451 method => $method,
452 url => $url,
453 retryable => 0,
454 message => "HTTP API core hook failed: $cause",
455 );
456 }
457  
458 188 ▶sub _normalize_retry {
Subroutine _normalize_retry: called CC=20 SCAR=30.0
459 188 my ($retry) = @_;
460 188 my %copy = %$retry;
461 188 ▶ die "retry option names must be scalars\n" if grep { ref($_) ne '' } keys %copy;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} keys %copy 0 188 188
462  
463 188 ▶ my $attempts = exists $copy{attempts} ? delete($copy{attempts}) : 3;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'attempts'} ? : 103 85 188
464 188 my $attempts_ref = ref($attempts);
465 188 ▶ die "retry attempts must be a positive integer\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $attempts or $attempts_ref ne "" or not $attempts =~ /\A[0-9]+\z/ or $attempts < 1 14 174 188
Condition100%9/9
not defined $attempts or $attempts_ref ne "" or not $attempts =~ /\A[0-9]+\z/ or $attempts < 1
l!l&&r!l&&!r
113174
not defined $attempts or $attempts_ref ne "" or not $attempts =~ /\A[0-9]+\z/
l!l&&r!l&&!r
47177
not defined $attempts or $attempts_ref ne ""
l!l&&r!l&&!r
22184
MC/DC100%4/4
not defined $attempts or $attempts_ref ne "" or not $attempts =~ /\A[0-9]+\z/ or $attempts < 1
not defined $attempts $attempts_ref ne "" not $attempts =~ /\A[0-9]+\z/ $attempts < 1
Truth table100%5/5
not(A) or B or not(C) or D
A= not defined $attemptsB= $attempts_ref ne ""C= not $attempts =~ /\A[0-9]+\z/D= $attempts < 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
466 if !defined($attempts) || $attempts_ref ne ''
467 || $attempts !~ /\A[0-9]+\z/ || $attempts < 1;
468  
469 my $base_delay = exists $copy{base_delay}
470 ? delete($copy{base_delay})
471 174 ▶ : 0.25;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'base_delay'} ? : 50 124 174
472 174 ▶ die "retry base_delay must be a non-negative number\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _non_negative_number($base_delay) 6 168 174
473 if !_non_negative_number($base_delay);
474  
475 my $max_delay = exists $copy{max_delay}
476 ? delete($copy{max_delay})
477 168 ▶ : 5;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'max_delay'} ? : 23 145 168
478 168 ▶ die "retry max_delay must be a non-negative number\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _non_negative_number($max_delay) 4 164 168
479 if !_non_negative_number($max_delay);
480  
481 164 ▶ my $jitter = exists $copy{jitter} ? delete($copy{jitter}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'jitter'} ? : 44 120 164
482 164 ▶ die "retry jitter must be a scalar\n" if ref($jitter) ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ref $jitter ne "" 0 164 164
483 164 ▶ $jitter = $jitter ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$jitter ? : 120 44 164
484  
485 my $methods = exists $copy{methods}
486 ? delete($copy{methods})
487 164 ▶ : [qw(GET HEAD PUT DELETE OPTIONS)];
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'methods'} ? : 10 154 164
488 164 ▶ die "retry methods must be an array reference\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $methods ne "ARRAY" 1 163 164
489 if ref($methods) ne 'ARRAY';
490  
491 die "retry methods must contain only scalar values\n"
492 163 ▶ if grep { ref($_) ne '' } @$methods;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} @$methods 1 162 163
493 die "retry methods must not contain empty values\n"
494 162 ▶ if grep { !defined($_) || $_ eq '' } @$methods;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ or $_ eq "";} @$methods 1 161 162
Condition66%2/3
!defined($_) || $_ eq ''
l!l&&r!l&&!r
01781
MC/DC50%1/2
!defined($_) || $_ eq ''
!defined($_) $_ eq ''
Truth table66%2/3
not(A) || B
A= !defined($_)B= $_ eq ''
ABresult
1X1
011
000
495 161 my @methods = map { uc($_) } @$methods;
496  
497 161 die "unknown retry option: $_\n" for sort keys %copy;
498  
499 return {
500 160 attempts => 0 + $attempts,
501 base_delay => 0 + $base_delay,
502 max_delay => 0 + $max_delay,
503 jitter => $jitter,
504 methods => \@methods,
505 };
506 }
507  
508 200 ▶sub _method_is_retryable {
Subroutine _method_is_retryable: called CC=2 SCAR=6.9
509 200 my ($method, $retry) = @_;
510 200 my %allowed = map { $_ => 1 } @{ $retry->{methods} };
511 200 ▶ return $allowed{$method} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$allowed{$method} ? : 174 26 200
512 }
513  
514 32 ▶sub _retry_delay {
Subroutine _retry_delay: called CC=8 SCAR=21.3
515 32 my ($retry, $attempt, $error) = @_;
516  
517 32 my $retry_after = _retry_after_delay($error->retry_after, time);
518 32 ▶ return $retry_after if defined $retry_after;
Branch: 1 branch
BranchTrueFalseTotal
if defined $retry_after 0 32 32
519  
520 32 my $rate_limit = $error->rate_limit;
521 32 ▶ if (defined($rate_limit) && $rate_limit->exhausted) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rate_limit and $rate_limit->exhausted) 7 25 32
Condition100%3/3
defined $rate_limit and $rate_limit->exhausted
!ll&&!rl&&r
2237
MC/DC100%2/2
defined $rate_limit and $rate_limit->exhausted
defined $rate_limit $rate_limit->exhausted
Truth table100%3/3
A and B
A= defined $rate_limitB= $rate_limit->exhausted
ABresult
0X0
100
111
522 7 my $wait = $rate_limit->wait_seconds;
523 7 ▶ return $wait if defined $wait;
Branch: 1 branch
BranchTrueFalseTotal
if defined $wait 5 2 7
524 }
525  
526 27 my $delay = $retry->{base_delay} * (2 ** ($attempt - 1));
527 27 ▶ $delay = $retry->{max_delay} if $delay > $retry->{max_delay};
Branch: 1 branch
BranchTrueFalseTotal
if $delay > $retry->{"max_delay"} 0 27 27
528 27 ▶ $delay = rand($delay) if $retry->{jitter} && $delay > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $retry->{"jitter"} and $delay > 0 0 27 27
Condition33%1/3
$retry->{"jitter"} and $delay > 0
!ll&&!rl&&r
2700
MC/DC0%0/2
$retry->{"jitter"} and $delay > 0
$retry->{"jitter"} $delay > 0
Truth table33%1/3
A and B
A= $retry->{"jitter"}B= $delay > 0
ABresult
0X0
100
111
529 27 return $delay;
530 }
531  
532 57 ▶sub _retry_after_delay {
Subroutine _retry_after_delay: called CC=12 SCAR=25.1
533 57 my ($value, $now) = @_;
534 57 ▶ return undef if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 26 31 57
535  
536 31 ▶ return 0 + $value
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/ 4 27 31
537 if $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/;
538  
539 27 my %month = (
540 Jan => 0, Feb => 1, Mar => 2, Apr => 3,
541 May => 4, Jun => 5, Jul => 6, Aug => 7,
542 Sep => 8, Oct => 9, Nov => 10, Dec => 11,
543 );
544  
545 return undef
546 27 ▶ if $value !~ /\A(Mon|Tue|Wed|Thu|Fri|Sat|Sun), ([0-9]{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2}) GMT\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /\A(Mon|Tue|Wed|Thu|Fri|Sat|Sun), ([0-9]{2}) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2}) GMT\z/ 7 20 27
547  
548 20 my ($weekday, $day, $mon, $year, $hour, $min, $sec) =
549 ($1, $2, $3, $4, $5, $6, $7);
550  
551 20 ▶ return undef if $hour > 23 || $min > 59 || $sec > 60;
Branch: 1 branch
BranchTrueFalseTotal
if $hour > 23 or $min > 59 or $sec > 60 1 19 20
Condition66%4/6
$hour > 23 or $min > 59 or $sec > 60
l!l&&r!l&&!r
1019
$hour > 23 or $min > 59
l!l&&r!l&&!r
1019
MC/DC33%1/3
$hour > 23 or $min > 59 or $sec > 60
$hour > 23 $min > 59 $sec > 60
Truth table50%2/4
A or B or C
A= $hour > 23B= $min > 59C= $sec > 60
ABCresult
1XX1
01X1
0011
0000
552  
553 19 my $epoch = eval {
554 19 timegm($sec, $min, $hour, $day, $month{$mon}, $year);
555 };
556 19 ▶ return undef if $@ || !defined $epoch;
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not defined $epoch 0 19 19
Condition33%1/3
$@ or not defined $epoch
l!l&&r!l&&!r
0019
MC/DC0%0/2
$@ or not defined $epoch
$@ not defined $epoch
Truth table33%1/3
A or not(B)
A= $@B= not defined $epoch
ABresult
1X1
011
000
557  
558 # Time::Local interprets some small year numbers relative to 1900 or
559 # 2000. An HTTP-date year is literal and must survive the conversion.
560 19 my @utc = gmtime($epoch);
561 19 ▶ return undef if $utc[5] + 1900 != $year;
Branch: 1 branch
BranchTrueFalseTotal
if $utc[5] + 1900 != $year 5 14 19
562  
563 # IMF-fixdate includes a weekday, which must agree with the date.
564 # Inconsistent values are malformed, so use the normal retry fallback.
565 14 my %weekday_number = (
566 Sun => 0, Mon => 1, Tue => 2, Wed => 3,
567 Thu => 4, Fri => 5, Sat => 6,
568 );
569 14 ▶ return undef if $utc[6] != $weekday_number{$weekday};
Branch: 1 branch
BranchTrueFalseTotal
if $utc[6] != $weekday_number{$weekday} 6 8 14
570  
571 8 my $delay = $epoch - $now;
572 8 ▶ return $delay > 0 ? $delay : 0;
Branch: 1 branch
BranchTrueFalseTotal
$delay > 0 ? : 6 2 8
573 }
574  
575 192 ▶sub _append_query {
Subroutine _append_query: called CC=11 SCAR=24.0
576 192 my ($url, $query) = @_;
577 192 my @pairs;
578  
579 192 for my $key (sort keys %$query) {
580 18 ▶ die "query parameter names must be scalars\n" if ref($key) ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ref $key ne "" 0 18 18
581 18 my $value = $query->{$key};
582 18 ▶ next if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 17 18
583  
584 17 my @values;
585 17 ▶ if (ref($value) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 2 15 17
elsif (ref $value ne '') { } 1 14 15
586 die "query parameter array values must contain only scalars or undef\n"
587 2 ▶ if grep { defined($_) && ref($_) ne '' } @$value;
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_ and ref $_ ne "";} @$value 1 1 2
Condition66%2/3
defined $_ && ref $_ ne ''
!ll&&!rl&&r
031
MC/DC50%1/2
defined $_ && ref $_ ne ''
defined $_ ref $_ ne ''
Truth table66%2/3
A && B
A= defined $_B= ref $_ ne ''
ABresult
0X0
100
111
588 1 @values = grep { defined $_ } @$value;
589 }
590 elsif (ref($value) ne '') {
591 1 die "query values must be scalars, array references, or undef\n";
592 }
593 else {
594 14 @values = ($value);
595 }
596  
597 push @pairs, map {
598 15 _uri_escape($key) . '=' . _uri_escape($_)
599 } @values;
600 }
601  
602 190 ▶ return $url if !@pairs;
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs 178 12 190
603  
604 12 my $fragment = '';
605 12 ▶ if ($url =~ s/(#.*)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ s/(#.*)\z//) 2 10 12
606 2 $fragment = $1;
607 }
608  
609 12 ▶ my $separator = index($url, '?') >= 0 ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
index($url, '?') != -1 ? : 2 10 12
610 12 return $url . $separator . join('&', @pairs) . $fragment;
611 }
612  
613 32 ▶sub _uri_escape {
Subroutine _uri_escape: called CC=3 SCAR=11.0
614 32 my ($value) = @_;
615 32 ▶ my $bytes = defined($value) ? "$value" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 32 0 32
616 32 ▶ utf8::encode($bytes) if utf8::is_utf8($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) 1 31 32
617 32 $bytes =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
618 32 return $bytes;
619 }
620  
621 189 ▶sub _validate_request_url {
Subroutine _validate_request_url: called CC=5 SCAR=16.1
622 189 my ($url) = @_;
623 189 my $url_ref = ref($url);
624 189 ▶ die "url must be a non-empty scalar\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $url or $url_ref ne "" or $url eq "" 2 187 189
Condition83%5/6
not defined $url or $url_ref ne "" or $url eq ""
l!l&&r!l&&!r
11187
not defined $url or $url_ref ne ""
l!l&&r!l&&!r
01188
MC/DC66%2/3
not defined $url or $url_ref ne "" or $url eq ""
not defined $url $url_ref ne "" $url eq ""
Truth table75%3/4
not(A) or B or C
A= not defined $urlB= $url_ref ne ""C= $url eq ""
ABCresult
1XX1
01X1
0011
0000
625 if !defined($url) || $url_ref ne '' || $url eq '';
626 187 ▶ die "url must be an absolute HTTP(S) URL\n"
Branch: 1 branch
BranchTrueFalseTotal
if not $url =~ m(\Ahttps?://(?:[^/?#\s@]+@)?(?:\[[0-9A-Fa-f:.]+\]|[^/?#\s:@]+)(?::[0-9]+)?(?:[/?#]|\z))i 6 181 187
627 if $url !~ m{\Ahttps?://(?:[^/?#\s@]+@)?(?:\[[0-9A-Fa-f:.]+\]|[^/?#\s:@]+)(?::[0-9]+)?(?:[/?#]|\z)}i;
628 }
629  
630 # HTTP::Tiny may return array references for repeated response headers.
631 # Request headers retain their stricter scalar-only contract.
632 161 ▶sub _valid_response_headers {
Subroutine _valid_response_headers: called CC=9 SCAR=22.0
633 161 my ($headers) = @_;
634 161 ▶ return 1 if !defined $headers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headers 4 157 161
635 157 ▶ return 0 if ref($headers) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers ne "HASH" 1 156 157
636  
637 156 for my $value (values %$headers) {
638 85 ▶ next if !defined($value) || ref($value) eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value eq "" 77 8 85
Condition66%2/3
not defined $value or ref $value eq ""
l!l&&r!l&&!r
0778
MC/DC50%1/2
not defined $value or ref $value eq ""
not defined $value ref $value eq ""
Truth table66%2/3
not(A) or B
A= not defined $valueB= ref $value eq ""
ABresult
1X1
011
000
639 8 ▶ return 0 if ref($value) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "ARRAY" 1 7 8
640 7 ▶ return 0 if grep { defined($_) && ref($_) ne '' } @$value;
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_ and ref $_ ne "";} @$value 2 5 7
Condition66%2/3
defined $_ && ref $_ ne ''
!ll&&!rl&&r
0112
MC/DC50%1/2
defined $_ && ref $_ ne ''
defined $_ ref $_ ne ''
Truth table66%2/3
A && B
A= defined $_B= ref $_ ne ''
ABresult
0X0
100
111
641 }
642 153 return 1;
643 }
644  
645 559 ▶sub _validate_header_values {
Subroutine _validate_header_values: called CC=13 SCAR=25.8
646 559 my ($headers) = @_;
647 die "header names must be scalars\n"
648 559 ▶ if grep { ref($_) ne '' } keys %$headers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} keys %$headers 0 559 559
649 die "header values must be scalars or undef\n"
650 559 ▶ if grep { defined($_) && ref($_) ne '' } values %$headers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_ and ref $_ ne "";} values %$headers 3 556 559
Condition100%3/3
defined $_ && ref $_ ne ''
!ll&&!rl&&r
51173
MC/DC100%2/2
defined $_ && ref $_ ne ''
defined $_ ref $_ ne ''
Truth table100%3/3
A && B
A= defined $_B= ref $_ ne ''
ABresult
0X0
100
111
651  
652 # Reject ambiguous case-insensitive names before sending a request.
653 # Equal values (or two undefined values) are not conflicting.
654 556 my %seen;
655 556 for my $name (sort keys %$headers) {
656 122 my $key = lc $name;
657 122 my $value = $headers->{$name};
658 122 ▶ if (exists $seen{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$key}) { } 9 113 122
659 9 my $previous = $seen{$key};
660 9 ▶ die "conflicting case-insensitive header values: $key\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $previous and not defined $value or not defined $previous and defined $value or defined $previous and defined $value and $previous ne $value 5 4 9
Condition83%15/18
defined $previous and not defined $value or not defined $previous and defined $value or defined $previous and defined $value and $previous ne $value
l!l&&r!l&&!r
144
defined $previous and not defined $value or not defined $previous and defined $value
l!l&&r!l&&!r
018
defined $previous and not defined $value
!ll&&!rl&&r
360
not defined $previous and defined $value
!ll&&!rl&&r
621
defined $previous and defined $value and $previous ne $value
!ll&&!rl&&r
224
defined $previous and defined $value
!ll&&!rl&&r
206
MC/DC28%2/7
defined $previous and not defined $value or not defined $previous and defined $value or defined $previous and defined $value and $previous ne $value
defined $previous not defined $value not defined $previous defined $value defined $previous defined $value $previous ne $value
Truth table21%4/19
A and not(B) or not(C) and D or E and F and G
A= defined $previousB= not defined $valueC= not defined $previousD= defined $valueE= defined $previousF= defined $valueG= $previous ne $value
ABCDEFGresult
11XXXXX1
0X11XXX1
1011XXX1
0X0X1111
0X101111
100X1111
10101111
0X0X0XX0
0X0X10X0
0X0X1100
0X100XX0
0X1010X0
0X101100
100X0XX0
100X10X0
100X1100
10100XX0
101010X0
10101100
661 if (defined($previous) && !defined($value))
662 || (!defined($previous) && defined($value))
663 || (defined($previous) && defined($value)
664 && $previous ne $value);
665 }
666 else {
667 113 $seen{$key} = $value;
668 }
669 }
670 }
671  
672 18 ▶sub _set_header_if_absent {
Subroutine _set_header_if_absent: called CC=2 SCAR=6.9
673 18 my ($headers, $name, $value) = @_;
674 18 my $wanted = lc $name;
675 18 ▶ return if grep { lc($_) eq $wanted } keys %$headers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {lc $_ eq $wanted;} keys %$headers 4 14 18
676 14 $headers->{$name} = $value;
677 }
678  
679 342 ▶sub _non_negative_number {
Subroutine _non_negative_number: called CC=4 SCAR=14.1
680 342 my ($value) = @_;
681 342 ▶ return defined($value)
Condition77%7/9
defined $value && ref $value eq '' && $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/ && $value >= 0
!ll&&!rl&&r
100332
defined $value and ref $value eq "" and $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/
!ll&&!rl&&r
28332
defined $value and ref $value eq ""
!ll&&!rl&&r
02340
MC/DC50%2/4
defined $value && ref $value eq '' && $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/ && $value >= 0
defined $value ref $value eq "" $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/ $value >= 0
Truth table60%3/5
A and B and C && D
A= defined $valueB= ref $value eq ""C= $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/D= $value >= 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
682 && ref($value) eq ''
683 && $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/
684 && $value >= 0;
685 }
686  
687 186 ▶sub _join_url {
Subroutine _join_url: called CC=1 SCAR=0.0
688 186 my ($self, $path) = @_;
689 186 $path =~ s{\A/+}{};
690 186 return $self->{base_url} . '/' . $path;
691 }
692  
693 59 ▶sub _retryable_status {
Subroutine _retryable_status: called CC=6 SCAR=17.9
694 59 my ($status) = @_;
695 59 ▶ return 1 if $status == 408 || $status == 425 || $status == 429;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 408 or $status == 425 or $status == 429 9 50 59
Condition100%6/6
$status == 408 or $status == 425 or $status == 429
l!l&&r!l&&!r
4550
$status == 408 or $status == 425
l!l&&r!l&&!r
2255
MC/DC100%3/3
$status == 408 or $status == 425 or $status == 429
$status == 408 $status == 425 $status == 429
Truth table100%4/4
A or B or C
A= $status == 408B= $status == 425C= $status == 429
ABCresult
1XX1
01X1
0011
0000
696 50 ▶ return 1 if $status >= 500 && $status <= 599;
Branch: 1 branch
BranchTrueFalseTotal
if $status >= 500 and $status <= 599 37 13 50
Condition66%2/3
$status >= 500 and $status <= 599
!ll&&!rl&&r
13037
MC/DC50%1/2
$status >= 500 and $status <= 599
$status >= 500 $status <= 599
Truth table66%2/3
A and B
A= $status >= 500B= $status <= 599
ABresult
0X0
100
111
697 13 return 0;
698 }
699  
700 39 1;
701  
702 __END__
« blib/lib/HTTP/API/Core/Pagination.pm ↑ Summary blib/lib/HTTP/API/Core/RateLimit.pm »