blib/lib/HTTP/API/Core.pm

filter: Statementstmt 99.7% 360 / 361 Branchbran 95.3% 246 / 258 Conditioncond 87.9% 182 / 207 MC/DCmcdc 73.7% 76 / 103 Subroutinesub 100% 41 / 41 Podpod 100% 12 / 12 totaltotal 93.3% 917 / 982 CC 210 SCAR 31.9
CC
210
Cov
95%
request
56.1
_request_once
34.2
new
23.0
CRAP 24.2
« 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.16';
16  
17 217 ▶sub new {
Subroutine new: called CC=23 SCAR=31.4
Pod documented
18 217 my $class = shift;
19 217 ▶ die "constructor options must be key/value pairs\n" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 216 217
20 die "constructor option names must be scalars\n"
21 216 ▶ if grep { ref($_) ne '' } @_[ grep { $_ % 2 == 0 } 0 .. $#_ ];
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} @_[grep {$_ % 2 == 0;} 0 .. $#_] 1 215 216
22 215 my %args = @_;
23  
24 215 my $base_url = delete $args{base_url};
25 215 my $base_url_ref = ref($base_url);
26 215 ▶ 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 214 215
Condition66%4/6
not defined $base_url or $base_url_ref eq "" and $base_url eq ""
l!l&&r!l&&!r
10214
$base_url_ref eq "" and $base_url eq ""
!ll&&!rl&&r
22120
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 214 ▶ 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 212 214
29 212 $base_url =~ s{/+\z}{};
30  
31 212 ▶ my $headers = exists($args{headers}) ? delete($args{headers}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'headers'} ? : 14 198 212
32 212 ▶ die "headers must be a hash reference\n" if ref($headers) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers ne "HASH" 1 211 212
33 211 _validate_header_values($headers);
34  
35 209 ▶ my $timeout = exists $args{timeout} ? delete($args{timeout}) : 10;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'timeout'} ? : 10 199 209
36 209 my $timeout_ref = ref($timeout);
37 209 ▶ 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 7 202 209
Condition100%9/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
61202
not defined $timeout or $timeout_ref ne "" or not $timeout =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/
l!l&&r!l&&!r
33203
not defined $timeout or $timeout_ref ne ""
l!l&&r!l&&!r
12206
MC/DC100%4/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 table100%5/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 202 my $transport = delete $args{transport};
44 202 my $transport_class = blessed($transport);
45 202 ▶ 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 199 202
Condition100%9/9
defined $transport and ref $transport ne "CODE" and not defined $transport_class && $transport->can("request")
!ll&&!rl&&r
19543
defined $transport and ref $transport ne "CODE"
!ll&&!rl&&r
261697
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 199 ▶ my $retry = exists $args{retry} ? delete($args{retry}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'retry'} ? : 121 78 199
51 199 ▶ die "retry must be a hash reference\n" if ref($retry) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $retry ne "HASH" 1 198 199
52 198 $retry = _normalize_retry($retry);
53  
54 176 ▶ my $hooks = exists $args{hooks} ? delete($args{hooks}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'hooks'} ? : 77 99 176
55 176 $hooks = _normalize_hooks($hooks);
56  
57 171 die "unknown constructor option: $_\n" for sort keys %args;
58  
59 170 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 170 ▶ $self->{http} = HTTP::Tiny->new(timeout => $timeout) if !defined $transport;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $transport 6 164 170
69 170 return $self;
70 }
71  
72 6 ▶sub base_url { $_[0]->{base_url} }
Subroutine base_url: called CC=1 SCAR=0.0
Pod documented
73 4 ▶sub timeout { $_[0]->{timeout} }
Subroutine timeout: called CC=1 SCAR=0.0
Pod documented
74 10 ▶sub retry { +{ %{ $_[0]->{retry} }, methods => [ @{ $_[0]->{retry}{methods} } ] } }
Subroutine retry: called CC=1 SCAR=0.0
Pod documented
75 2 ▶sub hooks { _clone_hooks($_[0]->{hooks}) }
Subroutine hooks: called CC=1 SCAR=0.0
Pod documented
76  
77 183 ▶sub get { my ($self, $path) = splice @_, 0, 2; return $self->request('GET', $path, @_) }
Subroutine get: called CC=1 SCAR=0.0
Pod documented
78 34 ▶sub post { my ($self, $path) = splice @_, 0, 2; return $self->request('POST', $path, @_) }
Subroutine post: called CC=1 SCAR=0.0
Pod documented
79 3 ▶sub put { my ($self, $path) = splice @_, 0, 2; return $self->request('PUT', $path, @_) }
Subroutine put: called CC=1 SCAR=0.0
Pod documented
80 3 ▶sub patch { my ($self, $path) = splice @_, 0, 2; return $self->request('PATCH', $path, @_) }
Subroutine patch: called CC=1 SCAR=0.0
Pod documented
81 3 ▶sub delete { my ($self, $path) = splice @_, 0, 2; return $self->request('DELETE', $path, @_) }
Subroutine delete: called CC=1 SCAR=0.0
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 233 ▶sub request {
Subroutine request: called CC=56 SCAR=40.3
Pod documented
93 233 my ($self, $method, $path) = splice @_, 0, 3;
94 233 ▶ die "request options must be key/value pairs\n" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 5 228 233
95 die "request option names must be scalars\n"
96 228 ▶ if grep { ref($_) ne '' } @_[ grep { $_ % 2 == 0 } 0 .. $#_ ];
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} @_[grep {$_ % 2 == 0;} 0 .. $#_] 5 223 228
97 223 my %opts = @_;
98 223 my $method_ref = ref($method);
99 223 ▶ die "method is required\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $method or $method_ref eq "" and $method eq "" 1 222 223
Condition83%5/6
not defined $method or $method_ref eq "" and $method eq ""
l!l&&r!l&&!r
01222
$method_ref eq "" and $method eq ""
!ll&&!rl&&r
22201
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 222 ▶ die "method must be a non-empty scalar\n" if $method_ref ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $method_ref ne "" 2 220 222
102 220 ▶ die "path is required\n" if !defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 1 219 220
103 219 my $path_ref = ref($path);
104 219 ▶ die "path must be a scalar\n" if $path_ref ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $path_ref ne "" 2 217 219
105 217 $method = uc($method);
106  
107 # URI schemes are case-insensitive; align with request URL validation and
108 # the pagination continuation origin guard.
109 217 ▶ my $url = $path =~ m{\Ahttps?://}i ? $path : $self->_join_url($path);
Branch: 1 branch
BranchTrueFalseTotal
$path =~ m[\Ahttps?://]i ? : 8 209 217
110  
111 217 ▶ my $query = exists $opts{query} ? delete($opts{query}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'query'} ? : 20 197 217
112 217 ▶ die "query must be a hash reference\n" if ref($query) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $query ne "HASH" 2 215 217
113 215 $url = _append_query($url, $query);
114  
115 213 ▶ my $request_headers = exists $opts{headers} ? delete($opts{headers}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'headers'} ? : 21 192 213
116 213 ▶ 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 212 213
117 212 _validate_header_values($request_headers);
118 208 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 208 for my $name (sort keys %$request_headers) {
122 19 my $wanted = lc $name;
123 19 delete @headers{ grep { lc($_) eq $wanted } keys %headers };
124 19 $headers{$name} = $request_headers->{$name};
125 }
126  
127 208 ▶ if (exists $opts{idempotency}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"idempotency"}) 11 197 208
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 202 ▶ if exists($opts{json}) && exists($opts{content});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"json"} and exists $opts{"content"} 1 201 202
Condition100%3/3
exists $opts{"json"} and exists $opts{"content"}
!ll&&!rl&&r
191101
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 201 my $content;
153  
154 201 ▶ if (exists $opts{json}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts{'json'}) { } 10 191 201
elsif (exists $opts{'content'}) { } 12 179 191
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 12 $content = delete $opts{content};
170 }
171  
172 200 ▶ my $retry = exists $opts{retry} ? delete($opts{retry}) : $self->{retry};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'retry'} ? : 11 189 200
173 200 my $retry_ref = ref($retry);
174 200 ▶ if ($retry_ref eq 'HASH' && $retry != $self->{retry}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($retry_ref eq 'HASH' and $retry != $self->{'retry'}) { } 9 191 200
elsif ($retry_ref eq '') { } 1 190 191
elsif ($retry_ref ne 'HASH') { } 1 189 190
Condition100%3/3
$retry_ref eq "HASH" and $retry != $self->{"retry"}
!ll&&!rl&&r
21899
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 1 die "retry must be a hash reference or scalar switch\n";
182 }
183  
184 my $request_hooks = exists $opts{hooks}
185 193 ▶ ? _normalize_hooks(delete($opts{hooks}))
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'hooks'} ? : 4 189 193
186 : {};
187 192 my $hooks = _merge_hooks($self->{hooks}, $request_hooks);
188  
189 192 die "unknown request option: $_\n" for sort keys %opts;
190  
191 my $attempts = _method_is_retryable($method, $retry)
192 ? $retry->{attempts}
193 191 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
_method_is_retryable($method, $retry) ? : 169 22 191
194 191 my $attempt = 0;
195  
196 191 ▶ while (++$attempt <= $attempts) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$attempt <= $attempts) 234 0 234
197 234 my $context = {
198 method => $method,
199 url => $url,
200 headers => { %headers },
201 content => $content,
202 attempt => $attempt,
203 };
204  
205 234 my $hook_error = _run_hooks($hooks->{before_request}, $context);
206 234 ▶ die _hook_error($hook_error, $method, $url) if defined $hook_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hook_error 5 229 234
207 229 my $context_method_ref = ref($context->{method});
208 die "method must be a non-empty scalar\n"
209 229 ▶ 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 "" 3 226 229
Condition100%6/6
not defined $context->{"method"} or $context_method_ref ne "" or $context->{"method"} eq ""
l!l&&r!l&&!r
21226
not defined $context->{"method"} or $context_method_ref ne ""
l!l&&r!l&&!r
11227
MC/DC100%3/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 table100%4/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 226 $context->{method} = uc($context->{method});
213 226 _validate_request_url($context->{url});
214 die "headers must be a hash reference\n"
215 217 ▶ if ref($context->{headers}) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $context->{"headers"} ne "HASH" 1 216 217
216 216 _validate_header_values($context->{headers});
217 214 my $context_content_ref = ref($context->{content});
218 die "content must be a scalar or undef\n"
219 214 ▶ if defined($context->{content}) && $context_content_ref ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $context->{"content"} and $context_content_ref ne "" 1 213 214
Condition100%3/3
defined $context->{"content"} and $context_content_ref ne ""
!ll&&!rl&&r
192211
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 213 my $dispatched_method = $context->{method};
224 213 my $started_at = time;
225 213 $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 213 );
233  
234 213 $context->{elapsed} = $elapsed;
235 213 ▶ $context->{request_id} = defined($response)
Branch: 2 branches
BranchTrueFalseTotal
defined $response ? : 110 103 213
defined $error ? : 103 0 103
236 ? $response->request_id
237 : defined($error) ? $error->request_id : undef;
238  
239 213 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 110 103 213
240 my $after_error = _run_hooks(
241 $hooks->{after_response},
242 110 $response,
243 $context,
244 );
245 die _hook_error(
246 $after_error,
247 $context->{method},
248 $context->{url},
249 110 ▶ ) if defined $after_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $after_error 1 109 110
250 109 return $response;
251 }
252  
253 103 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 103 ▶ ) if defined $on_error_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_error_error 1 102 103
259  
260 102 ▶ 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 43 102
Condition100%6/6
$attempt >= $attempts or not $error->retryable or not _method_is_retryable($dispatched_method, $retry)
l!l&&r!l&&!r
54543
$attempt >= $attempts or not $error->retryable
l!l&&r!l&&!r
47748
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 43 my $delay = _retry_delay($retry, $attempt, $error);
265 43 ▶ sleep($delay) if $delay > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > 0 8 35 43
266 }
267  
268 0 die "unreachable retry state\n";
269 }
270  
271 213 ▶sub _request_once {
Subroutine _request_once: called CC=34 SCAR=35.3
272 213 my ($self, $method, $url, $headers, $content) = @_;
273 213 my $raw;
274 213 my $started_at = time;
275  
276 eval {
277 213 ▶ if (defined $self->{transport}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'transport'}) { } 207 6 213
278 207 ▶ my $request_opts = {
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 20 187 207
279 headers => $headers,
280 (defined($content) ? (content => $content) : ()),
281 };
282  
283 207 ▶ if (ref($self->{transport}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'transport'} eq 'CODE') { } 203 4 207
284 203 $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 204 1;
297 213 ▶ } 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 } 204 9 213
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 204 ▶ 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 "") 21 183 204
Condition100%33/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
201183
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
191184
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
136185
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
112191
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
101193
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
64194
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"} or ref $raw->{"status"} ne ""
l!l&&r!l&&!r
51198
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"}
l!l&&r!l&&!r
32199
ref $raw ne "HASH" or not exists $raw->{"status"}
l!l&&r!l&&!r
12201
defined $raw->{"reason"} and ref $raw->{"reason"} ne ""
!ll&&!rl&&r
621221
defined $raw->{"content"} and ref $raw->{"content"} ne ""
!ll&&!rl&&r
261571
MC/DC100%12/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 table86%13/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 21 my $elapsed = time - $started_at;
325 21 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 183 ▶ 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 180 183
Condition88%8/9
not defined $self->{"transport"} and $raw->{"status"} == 599 and defined $raw->{"reason"} and $raw->{"reason"} eq "Internal Exception"
!ll&&!rl&&r
17913
not defined $self->{"transport"} and $raw->{"status"} == 599 and defined $raw->{"reason"}
!ll&&!rl&&r
17904
not defined $self->{"transport"} and $raw->{"status"} == 599
!ll&&!rl&&r
17724
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 180 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 180 ▶ content => defined($raw->{content}) ? $raw->{content} : '',
Branch: 2 branches
BranchTrueFalseTotal
defined $raw->{'headers'} ? : 162 18 180
defined $raw->{'content'} ? : 154 26 180
363 method => $method,
364 url => $url,
365 elapsed => $elapsed,
366 );
367  
368 180 ▶ return ($response, undef, $elapsed) if $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $response->is_success 110 70 180
369  
370 70 my $rate_limit = $response->rate_limit;
371 70 ▶ my $rate_limited = ($response->status == 403 || $response->status == 429)
Condition100%6/6
($response->status == 403 || $response->status == 429) && $rate_limit->exhausted
!ll&&!rl&&r
491110
$response->status == 403 or $response->status == 429
l!l&&r!l&&!r
81349
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 70 ▶ return (undef, HTTP::API::Core::Error->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $response->reason && length $response->reason ? : 55 15 70
Condition83%5/6
_retryable_status($response->status) || $rate_limited
l!l&&r!l&&!r
5758
defined $response->reason and length $response->reason
!ll&&!rl&&r
15055
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 180 ▶sub _normalize_hooks {
Subroutine _normalize_hooks: called CC=9 SCAR=22.0
395 180 my ($hooks) = @_;
396 180 ▶ $hooks = {} if !defined $hooks;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hooks 2 178 180
397 180 ▶ die "hooks must be a hash reference\n" if ref($hooks) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hooks ne "HASH" 2 178 180
398  
399 178 my %copy = %$hooks;
400 178 ▶ die "hook names must be scalars\n" if grep { ref($_) ne '' } keys %copy;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} keys %copy 0 178 178
401 178 my %normalized;
402 178 for my $name (qw(before_request after_response on_error)) {
403 530 my $value = delete $copy{$name};
404 530 ▶ next if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 443 87 530
405  
406 87 ▶ my @callbacks = ref($value) eq 'ARRAY' ? @$value : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 7 80 87
407 die "hook $name must be a code reference or array reference of code references\n"
408 87 ▶ if grep { ref($_) ne 'CODE' } @callbacks;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "CODE";} @callbacks 2 85 87
409 85 $normalized{$name} = \@callbacks;
410 }
411  
412 176 die "unknown hook: $_\n" for sort keys %copy;
413 174 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 192 ▶sub _merge_hooks {
Subroutine _merge_hooks: called CC=3 SCAR=11.0
425 192 my ($first, $second) = @_;
426 return {
427 map {
428 192 $_ => [
429 576 ▶ @{ defined($first->{$_}) ? $first->{$_} : [] },
Branch: 1 branch
BranchTrueFalseTotal
defined $first->{$_} ? : 88 488 576
430 576 ▶ @{ defined($second->{$_}) ? $second->{$_} : [] },
Branch: 1 branch
BranchTrueFalseTotal
defined $second->{$_} ? : 2 574 576
431 ]
432 } qw(before_request after_response on_error)
433 };
434 }
435  
436 447 ▶sub _run_hooks {
Subroutine _run_hooks: called CC=4 SCAR=13.9
437 447 my ($callbacks, @args) = @_;
438 447 ▶ for my $callback (@{ defined($callbacks) ? $callbacks : [] }) {
Branch: 1 branch
BranchTrueFalseTotal
defined $callbacks ? : 447 0 447
439 96 my $ok = eval { $callback->(@args); 1 };
440 96 ▶ return $@ if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 7 89 96
441 }
442 440 return undef;
443 }
444  
445 7 ▶sub _hook_error {
Subroutine _hook_error: called CC=3 SCAR=11.0
446 7 my ($cause, $method, $url) = @_;
447 7 my $cause_class = blessed($cause);
448 7 ▶ 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 5 7
Condition66%2/3
defined $cause_class and $cause->isa("HTTP::API::Core::Error")
!ll&&!rl&&r
502
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 5 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 208 ▶sub _normalize_retry {
Subroutine _normalize_retry: called CC=20 SCAR=30.0
459 208 my ($retry) = @_;
460 208 my %copy = %$retry;
461 208 ▶ 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 208 208
462  
463 208 ▶ my $attempts = exists $copy{attempts} ? delete($copy{attempts}) : 3;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'attempts'} ? : 117 91 208
464 208 my $attempts_ref = ref($attempts);
465 208 ▶ 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 194 208
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
113194
not defined $attempts or $attempts_ref ne "" or not $attempts =~ /\A[0-9]+\z/
l!l&&r!l&&!r
47197
not defined $attempts or $attempts_ref ne ""
l!l&&r!l&&!r
22204
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 194 ▶ : 0.25;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'base_delay'} ? : 64 130 194
472 194 ▶ die "retry base_delay must be a non-negative number\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _non_negative_number($base_delay) 6 188 194
473 if !_non_negative_number($base_delay);
474  
475 my $max_delay = exists $copy{max_delay}
476 ? delete($copy{max_delay})
477 188 ▶ : 5;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'max_delay'} ? : 28 160 188
478 188 ▶ die "retry max_delay must be a non-negative number\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _non_negative_number($max_delay) 4 184 188
479 if !_non_negative_number($max_delay);
480  
481 184 ▶ my $jitter = exists $copy{jitter} ? delete($copy{jitter}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'jitter'} ? : 58 126 184
482 184 ▶ die "retry jitter must be a scalar\n" if ref($jitter) ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ref $jitter ne "" 0 184 184
483 184 ▶ $jitter = $jitter ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$jitter ? : 127 57 184
484  
485 my $methods = exists $copy{methods}
486 ? delete($copy{methods})
487 184 ▶ : [qw(GET HEAD PUT DELETE OPTIONS)];
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'methods'} ? : 10 174 184
488 184 ▶ die "retry methods must be an array reference\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $methods ne "ARRAY" 1 183 184
489 if ref($methods) ne 'ARRAY';
490  
491 die "retry methods must contain only scalar values\n"
492 183 ▶ if grep { ref($_) ne '' } @$methods;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} @$methods 1 182 183
493 die "retry methods must not contain empty values\n"
494 182 ▶ if grep { !defined($_) || $_ eq '' } @$methods;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ or $_ eq "";} @$methods 1 181 182
Condition66%2/3
!defined($_) || $_ eq ''
l!l&&r!l&&!r
01881
MC/DC50%1/2
!defined($_) || $_ eq ''
!defined($_) $_ eq ''
Truth table66%2/3
not(A) || B
A= !defined($_)B= $_ eq ''
ABresult
1X1
011
000
495 181 my @methods = map { uc($_) } @$methods;
496  
497 181 die "unknown retry option: $_\n" for sort keys %copy;
498  
499 return {
500 180 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 239 ▶sub _method_is_retryable {
Subroutine _method_is_retryable: called CC=2 SCAR=6.9
509 239 my ($method, $retry) = @_;
510 239 my %allowed = map { $_ => 1 } @{ $retry->{methods} };
511 239 ▶ return $allowed{$method} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$allowed{$method} ? : 212 27 239
512 }
513  
514 49 ▶sub _retry_delay {
Subroutine _retry_delay: called CC=8 SCAR=20.8
515 49 my ($retry, $attempt, $error) = @_;
516  
517 49 my $retry_after = _retry_after_delay($error->retry_after, time);
518 49 ▶ return $retry_after if defined $retry_after;
Branch: 1 branch
BranchTrueFalseTotal
if defined $retry_after 4 45 49
519  
520 45 my $rate_limit = $error->rate_limit;
521 45 ▶ if (defined($rate_limit) && $rate_limit->exhausted) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rate_limit and $rate_limit->exhausted) 9 36 45
Condition100%3/3
defined $rate_limit and $rate_limit->exhausted
!ll&&!rl&&r
9279
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 9 my $wait = $rate_limit->wait_seconds;
523 9 ▶ return $wait if defined $wait;
Branch: 1 branch
BranchTrueFalseTotal
if defined $wait 7 2 9
524 }
525  
526 38 my $delay = $retry->{base_delay} * (2 ** ($attempt - 1));
527 38 ▶ $delay = $retry->{max_delay} if $delay > $retry->{max_delay};
Branch: 1 branch
BranchTrueFalseTotal
if $delay > $retry->{"max_delay"} 2 36 38
528 38 ▶ $delay = rand($delay) if $retry->{jitter} && $delay > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $retry->{"jitter"} and $delay > 0 2 36 38
Condition66%2/3
$retry->{"jitter"} and $delay > 0
!ll&&!rl&&r
3602
MC/DC50%1/2
$retry->{"jitter"} and $delay > 0
$retry->{"jitter"} $delay > 0
Truth table66%2/3
A and B
A= $retry->{"jitter"}B= $delay > 0
ABresult
0X0
100
111
529 38 return $delay;
530 }
531  
532 74 ▶sub _retry_after_delay {
Subroutine _retry_after_delay: called CC=12 SCAR=25.1
533 74 my ($value, $now) = @_;
534 74 ▶ return undef if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 39 35 74
535  
536 35 ▶ return 0 + $value
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/ 8 27 35
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 215 ▶sub _append_query {
Subroutine _append_query: called CC=11 SCAR=24.0
576 215 my ($url, $query) = @_;
577 215 my @pairs;
578  
579 215 for my $key (sort keys %$query) {
580 21 ▶ die "query parameter names must be scalars\n" if ref($key) ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ref $key ne "" 0 21 21
581 21 my $value = $query->{$key};
582 21 ▶ next if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 20 21
583  
584 20 my @values;
585 20 ▶ if (ref($value) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 2 18 20
elsif (ref $value ne '') { } 1 17 18
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 17 @values = ($value);
595 }
596  
597 push @pairs, map {
598 18 _uri_escape($key) . '=' . _uri_escape($_)
599 } @values;
600 }
601  
602 213 ▶ return $url if !@pairs;
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs 198 15 213
603  
604 15 my $fragment = '';
605 15 ▶ if ($url =~ s/(#.*)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ s/(#.*)\z//) 2 13 15
606 2 $fragment = $1;
607 }
608  
609 15 ▶ my $separator = index($url, '?') >= 0 ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
index($url, '?') != -1 ? : 2 13 15
610 15 return $url . $separator . join('&', @pairs) . $fragment;
611 }
612  
613 38 ▶sub _uri_escape {
Subroutine _uri_escape: called CC=3 SCAR=11.0
614 38 my ($value) = @_;
615 38 ▶ my $bytes = defined($value) ? "$value" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 38 0 38
616 38 ▶ utf8::encode($bytes) if utf8::is_utf8($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) 1 37 38
617 38 $bytes =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
618 38 return $bytes;
619 }
620  
621 226 ▶sub _validate_request_url {
Subroutine _validate_request_url: called CC=5 SCAR=16.1
622 226 my ($url) = @_;
623 226 my $url_ref = ref($url);
624 226 ▶ die "url must be a non-empty scalar\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $url or $url_ref ne "" or $url eq "" 3 223 226
Condition100%6/6
not defined $url or $url_ref ne "" or $url eq ""
l!l&&r!l&&!r
21223
not defined $url or $url_ref ne ""
l!l&&r!l&&!r
11224
MC/DC100%3/3
not defined $url or $url_ref ne "" or $url eq ""
not defined $url $url_ref ne "" $url eq ""
Truth table100%4/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 223 ▶ 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 217 223
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 191 ▶sub _valid_response_headers {
Subroutine _valid_response_headers: called CC=9 SCAR=22.0
633 191 my ($headers) = @_;
634 191 ▶ return 1 if !defined $headers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headers 20 171 191
635 171 ▶ return 0 if ref($headers) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers ne "HASH" 2 169 171
636  
637 169 for my $value (values %$headers) {
638 98 ▶ next if !defined($value) || ref($value) eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value eq "" 89 9 98
Condition66%2/3
not defined $value or ref $value eq ""
l!l&&r!l&&!r
0899
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 9 ▶ return 0 if ref($value) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "ARRAY" 1 8 9
640 8 ▶ return 0 if grep { defined($_) && ref($_) ne '' } @$value;
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_ and ref $_ ne "";} @$value 3 5 8
Condition100%3/3
defined $_ && ref $_ ne ''
!ll&&!rl&&r
1113
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
641 }
642 165 return 1;
643 }
644  
645 639 ▶sub _validate_header_values {
Subroutine _validate_header_values: called CC=13 SCAR=25.8
646 639 my ($headers) = @_;
647 die "header names must be scalars\n"
648 639 ▶ if grep { ref($_) ne '' } keys %$headers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} keys %$headers 0 639 639
649 die "header values must be scalars or undef\n"
650 639 ▶ if grep { defined($_) && ref($_) ne '' } values %$headers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_ and ref $_ ne "";} values %$headers 3 636 639
Condition100%3/3
defined $_ && ref $_ ne ''
!ll&&!rl&&r
51233
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 636 my %seen;
655 636 for my $name (sort keys %$headers) {
656 128 my $key = lc $name;
657 128 my $value = $headers->{$name};
658 128 ▶ if (exists $seen{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$key}) { } 9 119 128
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 119 $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 382 ▶sub _non_negative_number {
Subroutine _non_negative_number: called CC=4 SCAR=14.1
680 382 my ($value) = @_;
681 382 ▶ 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
100372
defined $value and ref $value eq "" and $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/
!ll&&!rl&&r
28372
defined $value and ref $value eq ""
!ll&&!rl&&r
02380
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 209 ▶sub _join_url {
Subroutine _join_url: called CC=1 SCAR=0.0
688 209 my ($self, $path) = @_;
689 209 $path =~ s{\A/+}{};
690 209 return $self->{base_url} . '/' . $path;
691 }
692  
693 70 ▶sub _retryable_status {
Subroutine _retryable_status: called CC=6 SCAR=17.9
694 70 my ($status) = @_;
695 70 ▶ return 1 if $status == 408 || $status == 425 || $status == 429;
Branch: 1 branch
BranchTrueFalseTotal
if $status == 408 or $status == 425 or $status == 429 17 53 70
Condition100%6/6
$status == 408 or $status == 425 or $status == 429
l!l&&r!l&&!r
41353
$status == 408 or $status == 425
l!l&&r!l&&!r
2266
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 53 ▶ return 1 if $status >= 500 && $status <= 599;
Branch: 1 branch
BranchTrueFalseTotal
if $status >= 500 and $status <= 599 40 13 53
Condition66%2/3
$status >= 500 and $status <= 599
!ll&&!rl&&r
13040
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 »