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.14';
16  
17 191 ▶sub new {
Subroutine new: called CC=23 SCAR=31.5
Pod documented
18 191 my $class = shift;
19 191 ▶ die "constructor options must be key/value pairs\n" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 191 191
20 die "constructor option names must be scalars\n"
21 191 ▶ if grep { ref($_) ne '' } @_[ grep { $_ % 2 == 0 } 0 .. $#_ ];
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} @_[grep {$_ % 2 == 0;} 0 .. $#_] 0 191 191
22 191 my %args = @_;
23  
24 191 my $base_url = delete $args{base_url};
25 191 my $base_url_ref = ref($base_url);
26 191 ▶ 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 190 191
Condition66%4/6
not defined $base_url or $base_url_ref eq "" and $base_url eq ""
l!l&&r!l&&!r
10190
$base_url_ref eq "" and $base_url eq ""
!ll&&!rl&&r
21880
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 190 ▶ 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 188 190
29 188 $base_url =~ s{/+\z}{};
30  
31 188 ▶ my $headers = exists($args{headers}) ? delete($args{headers}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'headers'} ? : 14 174 188
32 188 ▶ die "headers must be a hash reference\n" if ref($headers) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers ne "HASH" 1 187 188
33 187 _validate_header_values($headers);
34  
35 185 ▶ my $timeout = exists $args{timeout} ? delete($args{timeout}) : 10;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'timeout'} ? : 9 176 185
36 185 my $timeout_ref = ref($timeout);
37 185 ▶ 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 179 185
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
51179
not defined $timeout or $timeout_ref ne "" or not $timeout =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/
l!l&&r!l&&!r
23180
not defined $timeout or $timeout_ref ne ""
l!l&&r!l&&!r
02183
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 179 my $transport = delete $args{transport};
44 179 my $transport_class = blessed($transport);
45 179 ▶ 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 176 179
Condition100%9/9
defined $transport and ref $transport ne "CODE" and not defined $transport_class && $transport->can("request")
!ll&&!rl&&r
17243
defined $transport and ref $transport ne "CODE"
!ll&&!rl&&r
251477
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 176 ▶ my $retry = exists $args{retry} ? delete($args{retry}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'retry'} ? : 104 72 176
51 176 ▶ die "retry must be a hash reference\n" if ref($retry) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $retry ne "HASH" 0 176 176
52 176 $retry = _normalize_retry($retry);
53  
54 154 ▶ my $hooks = exists $args{hooks} ? delete($args{hooks}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'hooks'} ? : 63 91 154
55 154 $hooks = _normalize_hooks($hooks);
56  
57 149 die "unknown constructor option: $_\n" for sort keys %args;
58  
59 148 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 148 ▶ $self->{http} = HTTP::Tiny->new(timeout => $timeout) if !defined $transport;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $transport 6 142 148
69 148 return $self;
70 }
71  
72 4 ▶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 156 ▶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 8 ▶sub paginate {
Subroutine paginate: called CC=1 SCAR=0.0
Pod documented
84 8 my ($self, $path) = splice @_, 0, 2;
85 8 return HTTP::API::Core::Pagination->new(
86 client => $self,
87 path => $path,
88 @_,
89 );
90 }
91  
92 195 ▶sub request {
Subroutine request: called CC=56 SCAR=40.6
Pod documented
93 195 my ($self, $method, $path) = splice @_, 0, 3;
94 195 ▶ die "request options must be key/value pairs\n" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 195 195
95 die "request option names must be scalars\n"
96 195 ▶ if grep { ref($_) ne '' } @_[ grep { $_ % 2 == 0 } 0 .. $#_ ];
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} @_[grep {$_ % 2 == 0;} 0 .. $#_] 0 195 195
97 195 my %opts = @_;
98 195 my $method_ref = ref($method);
99 195 ▶ die "method is required\n"
Branch: 1 branch
BranchTrueFalseTotal
if not defined $method or $method_ref eq "" and $method eq "" 1 194 195
Condition83%5/6
not defined $method or $method_ref eq "" and $method eq ""
l!l&&r!l&&!r
01194
$method_ref eq "" and $method eq ""
!ll&&!rl&&r
21921
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 194 ▶ die "method must be a non-empty scalar\n" if $method_ref ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $method_ref ne "" 2 192 194
102 192 ▶ die "path is required\n" if !defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 1 191 192
103 191 my $path_ref = ref($path);
104 191 ▶ die "path must be a scalar\n" if $path_ref ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $path_ref ne "" 2 189 191
105 189 $method = uc($method);
106  
107 189 ▶ my $url = $path =~ m{\Ahttps?://} ? $path : $self->_join_url($path);
Branch: 1 branch
BranchTrueFalseTotal
$path =~ m[\Ahttps?://] ? : 5 184 189
108  
109 189 ▶ my $query = exists $opts{query} ? delete($opts{query}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'query'} ? : 17 172 189
110 189 ▶ die "query must be a hash reference\n" if ref($query) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $query ne "HASH" 2 187 189
111 187 $url = _append_query($url, $query);
112  
113 185 ▶ my $request_headers = exists $opts{headers} ? delete($opts{headers}) : {};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'headers'} ? : 18 167 185
114 185 ▶ 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 184 185
115 184 _validate_header_values($request_headers);
116 180 my %headers = %{ $self->{headers} };
117 # A header hash may contain multiple spellings of the same name.
118 # Matching values are safe; deterministic spelling avoids hash-order drift.
119 180 for my $name (sort keys %$request_headers) {
120 16 my $wanted = lc $name;
121 16 delete @headers{ grep { lc($_) eq $wanted } keys %headers };
122 16 $headers{$name} = $request_headers->{$name};
123 }
124  
125 180 ▶ if (exists $opts{idempotency}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"idempotency"}) 11 169 180
126 11 my $idempotency = delete $opts{idempotency};
127 11 ▶ die "idempotency must be a hash reference\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $idempotency ne "HASH" 1 10 11
128 if ref($idempotency) ne 'HASH';
129  
130 10 my %copy = %$idempotency;
131 10 my $key = delete $copy{key};
132 10 my $header = delete $copy{header};
133  
134 10 my $key_ref = ref($key);
135 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
136 if !defined($key) || $key_ref ne '' || $key eq '';
137 8 my $header_ref = ref($header);
138 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
139 if !defined($header) || $header_ref ne '' || $header eq '';
140 6 die "unknown idempotency option: $_\n" for sort keys %copy;
141  
142 5 my $wanted = lc $header;
143 5 my $already = grep { lc($_) eq $wanted } keys %headers;
144 5 ▶ $headers{$header} = "$key" if !$already;
Branch: 1 branch
BranchTrueFalseTotal
unless $already 4 1 5
145 }
146  
147 die "json and content request options are mutually exclusive\n"
148 174 ▶ if exists($opts{json}) && exists($opts{content});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"json"} and exists $opts{"content"} 1 173 174
Condition100%3/3
exists $opts{"json"} and exists $opts{"content"}
!ll&&!rl&&r
163101
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
149  
150 173 my $content;
151  
152 173 ▶ if (exists $opts{json}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts{'json'}) { } 10 163 173
elsif (exists $opts{'content'}) { } 9 154 163
153 10 my $value = delete $opts{json};
154 10 $content = eval { encode_json($value) };
155 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 9 10
156 1 die HTTP::API::Core::Error->new(
157 category => 'encode',
158 method => $method,
159 url => $url,
160 message => "failed to encode JSON request: $@",
161 );
162 }
163 9 _set_header_if_absent(\%headers, 'content-type', 'application/json');
164 9 _set_header_if_absent(\%headers, 'accept', 'application/json');
165 }
166 elsif (exists $opts{content}) {
167 9 $content = delete $opts{content};
168 }
169  
170 172 ▶ my $retry = exists $opts{retry} ? delete($opts{retry}) : $self->{retry};
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'retry'} ? : 10 162 172
171 172 my $retry_ref = ref($retry);
172 172 ▶ if ($retry_ref eq 'HASH' && $retry != $self->{retry}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($retry_ref eq 'HASH' and $retry != $self->{'retry'}) { } 9 163 172
elsif ($retry_ref eq '') { } 1 162 163
elsif ($retry_ref ne 'HASH') { } 0 162 162
Condition100%3/3
$retry_ref eq "HASH" and $retry != $self->{"retry"}
!ll&&!rl&&r
11629
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
173 9 $retry = _normalize_retry($retry);
174 }
175 elsif ($retry_ref eq '') {
176 1 ▶ $retry = $retry ? $self->{retry} : _normalize_retry({ attempts => 1 });
Branch: 1 branch
BranchTrueFalseTotal
$retry ? : 0 1 1
177 }
178 elsif ($retry_ref ne 'HASH') {
179 0 die "retry must be a hash reference or scalar switch\n";
180 }
181  
182 my $request_hooks = exists $opts{hooks}
183 166 ▶ ? _normalize_hooks(delete($opts{hooks}))
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'hooks'} ? : 3 163 166
184 : {};
185 165 my $hooks = _merge_hooks($self->{hooks}, $request_hooks);
186  
187 165 die "unknown request option: $_\n" for sort keys %opts;
188  
189 my $attempts = _method_is_retryable($method, $retry)
190 ? $retry->{attempts}
191 164 ▶ : 1;
Branch: 1 branch
BranchTrueFalseTotal
_method_is_retryable($method, $retry) ? : 143 21 164
192 164 my $attempt = 0;
193  
194 164 ▶ while (++$attempt <= $attempts) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$attempt <= $attempts) 190 0 190
195 190 my $context = {
196 method => $method,
197 url => $url,
198 headers => { %headers },
199 content => $content,
200 attempt => $attempt,
201 };
202  
203 190 my $hook_error = _run_hooks($hooks->{before_request}, $context);
204 190 ▶ die _hook_error($hook_error, $method, $url) if defined $hook_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $hook_error 5 185 190
205 185 my $context_method_ref = ref($context->{method});
206 die "method must be a non-empty scalar\n"
207 185 ▶ 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 184 185
Condition66%4/6
not defined $context->{"method"} or $context_method_ref ne "" or $context->{"method"} eq ""
l!l&&r!l&&!r
10184
not defined $context->{"method"} or $context_method_ref ne ""
l!l&&r!l&&!r
01184
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
208 # Hooks may supply a different method spelling. Normalize the actual
209 # dispatched method before transport, metadata, and retry decisions.
210 184 $context->{method} = uc($context->{method});
211 184 _validate_request_url($context->{url});
212 die "headers must be a hash reference\n"
213 176 ▶ if ref($context->{headers}) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $context->{"headers"} ne "HASH" 0 176 176
214 176 _validate_header_values($context->{headers});
215 174 my $context_content_ref = ref($context->{content});
216 die "content must be a scalar or undef\n"
217 174 ▶ if defined($context->{content}) && $context_content_ref ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $context->{"content"} and $context_content_ref ne "" 1 173 174
Condition100%3/3
defined $context->{"content"} and $context_content_ref ne ""
!ll&&!rl&&r
155181
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
218  
219 # Hooks may change the method. Keep the actual dispatched method for
220 # retry eligibility even if on_error hooks later mutate the context.
221 173 my $dispatched_method = $context->{method};
222 173 my $started_at = time;
223 173 $context->{started_at} = $started_at;
224  
225 my ($response, $error, $elapsed) = $self->_request_once(
226 $dispatched_method,
227 $context->{url},
228 $context->{headers},
229 $context->{content},
230 173 );
231  
232 173 $context->{elapsed} = $elapsed;
233 173 ▶ $context->{request_id} = defined($response)
Branch: 2 branches
BranchTrueFalseTotal
defined $response ? : 88 85 173
defined $error ? : 85 0 85
234 ? $response->request_id
235 : defined($error) ? $error->request_id : undef;
236  
237 173 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 88 85 173
238 my $after_error = _run_hooks(
239 $hooks->{after_response},
240 88 $response,
241 $context,
242 );
243 die _hook_error(
244 $after_error,
245 $context->{method},
246 $context->{url},
247 88 ▶ ) if defined $after_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $after_error 0 88 88
248 88 return $response;
249 }
250  
251 85 my $on_error_error = _run_hooks($hooks->{on_error}, $error, $context);
252 die _hook_error(
253 $on_error_error,
254 $context->{method},
255 $context->{url},
256 85 ▶ ) if defined $on_error_error;
Branch: 1 branch
BranchTrueFalseTotal
if defined $on_error_error 0 85 85
257  
258 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
259 || !$error->retryable
260 || !_method_is_retryable($dispatched_method, $retry);
261  
262 26 my $delay = _retry_delay($retry, $attempt, $error);
263 26 ▶ sleep($delay) if $delay > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > 0 0 26 26
264 }
265  
266 0 die "unreachable retry state\n";
267 }
268  
269 173 ▶sub _request_once {
Subroutine _request_once: called CC=34 SCAR=35.4
270 173 my ($self, $method, $url, $headers, $content) = @_;
271 173 my $raw;
272 173 my $started_at = time;
273  
274 eval {
275 173 ▶ if (defined $self->{transport}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'transport'}) { } 167 6 173
276 167 ▶ my $request_opts = {
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 17 150 167
277 headers => $headers,
278 (defined($content) ? (content => $content) : ()),
279 };
280  
281 167 ▶ if (ref($self->{transport}) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'transport'} eq 'CODE') { } 163 4 167
282 163 $raw = $self->{transport}->($method, $url, $request_opts);
283 }
284 else {
285 4 $raw = $self->{transport}->request($method, $url, $request_opts);
286 }
287 }
288 else {
289 6 ▶ $raw = $self->{http}->request($method, $url, {
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 1 5 6
290 headers => $headers,
291 (defined($content) ? (content => $content) : ()),
292 });
293 }
294 164 1;
295 173 ▶ } 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 } 164 9 173
296 9 my $cause = $@;
297 9 my $cause_class = blessed($cause);
298 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
299 if defined($cause_class) && $cause->isa('HTTP::API::Core::Error');
300  
301 7 my $elapsed = time - $started_at;
302 7 return (undef, HTTP::API::Core::Error->new(
303 category => 'transport',
304 method => $method,
305 url => $url,
306 retryable => 1,
307 elapsed => $elapsed,
308 message => "HTTP transport failed: $cause",
309 ), $elapsed);
310 };
311  
312 164 ▶ 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 150 164
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
131150
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
121151
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
84152
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
71156
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
70157
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
43157
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"} or ref $raw->{"status"} ne ""
l!l&&r!l&&!r
40160
ref $raw ne "HASH" or not exists $raw->{"status"} or not defined $raw->{"status"}
l!l&&r!l&&!r
31160
ref $raw ne "HASH" or not exists $raw->{"status"}
l!l&&r!l&&!r
12161
defined $raw->{"reason"} and ref $raw->{"reason"} ne ""
!ll&&!rl&&r
351161
defined $raw->{"content"} and ref $raw->{"content"} ne ""
!ll&&!rl&&r
21481
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
313 || !exists($raw->{status})
314 || !defined($raw->{status})
315 || ref($raw->{status}) ne ''
316 || $raw->{status} !~ /\A[0-9]{3}\z/
317 || $raw->{status} < 100
318 || $raw->{status} > 599
319 || !_valid_response_headers($raw->{headers})
320 || (defined($raw->{reason}) && ref($raw->{reason}) ne '')
321 || (defined($raw->{content}) && ref($raw->{content}) ne '')) {
322 14 my $elapsed = time - $started_at;
323 14 return (undef, HTTP::API::Core::Error->new(
324 category => 'transport',
325 method => $method,
326 url => $url,
327 retryable => 1,
328 elapsed => $elapsed,
329 message => 'HTTP transport returned an invalid response',
330 ), $elapsed);
331 }
332  
333 # HTTP::Tiny reports socket, TLS, DNS, and timeout failures as a
334 # pseudo-response instead of throwing. Do not treat that sentinel as
335 # an actual HTTP 599 response from the server. Custom transports
336 # keep their own documented response/status contract.
337 150 ▶ 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 147 150
Condition88%8/9
not defined $self->{"transport"} and $raw->{"status"} == 599 and defined $raw->{"reason"} and $raw->{"reason"} eq "Internal Exception"
!ll&&!rl&&r
14613
not defined $self->{"transport"} and $raw->{"status"} == 599 and defined $raw->{"reason"}
!ll&&!rl&&r
14604
not defined $self->{"transport"} and $raw->{"status"} == 599
!ll&&!rl&&r
14424
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
338 && $raw->{status} == 599
339 && defined($raw->{reason})
340 && $raw->{reason} eq 'Internal Exception') {
341 3 my $elapsed = time - $started_at;
342 my $cause = defined($raw->{content}) && length($raw->{content})
343 ? $raw->{content}
344 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
345 3 return (undef, HTTP::API::Core::Error->new(
346 category => 'transport',
347 method => $method,
348 url => $url,
349 retryable => 1,
350 elapsed => $elapsed,
351 message => "HTTP::Tiny transport failed: $cause",
352 ), $elapsed);
353 }
354  
355 147 my $elapsed = time - $started_at;
356 my $response = HTTP::API::Core::Response->new(
357 status => 0 + $raw->{status},
358 reason => $raw->{reason},
359 headers => defined($raw->{headers}) ? $raw->{headers} : {},
360 147 ▶ content => defined($raw->{content}) ? $raw->{content} : '',
Branch: 2 branches
BranchTrueFalseTotal
defined $raw->{'headers'} ? : 145 2 147
defined $raw->{'content'} ? : 145 2 147
361 method => $method,
362 url => $url,
363 elapsed => $elapsed,
364 );
365  
366 147 ▶ return ($response, undef, $elapsed) if $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $response->is_success 88 59 147
367  
368 59 my $rate_limit = $response->rate_limit;
369 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
370 && $rate_limit->exhausted;
371  
372 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
373 category => 'http',
374 status => $response->status,
375 method => $method,
376 url => $url,
377 retryable => _retryable_status($response->status) || $rate_limited,
378 retry_after => $response->header('retry-after'),
379 request_id => $response->request_id,
380 elapsed => $elapsed,
381 response => $response,
382 message => sprintf(
383 'HTTP %d%s',
384 $response->status,
385 defined($response->reason) && length($response->reason)
386 ? ' ' . $response->reason
387 : '',
388 ),
389 ), $elapsed);
390 }
391  
392 157 ▶sub _normalize_hooks {
Subroutine _normalize_hooks: called CC=9 SCAR=22.0
393 157 my ($hooks) = @_;
394 157 ▶ $hooks = {} if !defined $hooks;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hooks 0 157 157
395 157 ▶ die "hooks must be a hash reference\n" if ref($hooks) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $hooks ne "HASH" 2 155 157
396  
397 155 my %copy = %$hooks;
398 155 ▶ die "hook names must be scalars\n" if grep { ref($_) ne '' } keys %copy;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} keys %copy 0 155 155
399 155 my %normalized;
400 155 for my $name (qw(before_request after_response on_error)) {
401 461 my $value = delete $copy{$name};
402 461 ▶ next if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 389 72 461
403  
404 72 ▶ my @callbacks = ref($value) eq 'ARRAY' ? @$value : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 5 67 72
405 die "hook $name must be a code reference or array reference of code references\n"
406 72 ▶ if grep { ref($_) ne 'CODE' } @callbacks;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "CODE";} @callbacks 2 70 72
407 70 $normalized{$name} = \@callbacks;
408 }
409  
410 153 die "unknown hook: $_\n" for sort keys %copy;
411 151 return \%normalized;
412 }
413  
414 2 ▶sub _clone_hooks {
Subroutine _clone_hooks: called CC=2 SCAR=6.9
415 2 my ($hooks) = @_;
416 return {
417 2 ▶ map { $_ => [ @{ defined($hooks->{$_}) ? $hooks->{$_} : [] } ] }
Branch: 1 branch
BranchTrueFalseTotal
defined $hooks->{$_} ? : 2 4 6
418 qw(before_request after_response on_error)
419 };
420 }
421  
422 165 ▶sub _merge_hooks {
Subroutine _merge_hooks: called CC=3 SCAR=11.0
423 165 my ($first, $second) = @_;
424 return {
425 map {
426 165 $_ => [
427 495 ▶ @{ defined($first->{$_}) ? $first->{$_} : [] },
Branch: 1 branch
BranchTrueFalseTotal
defined $first->{$_} ? : 73 422 495
428 495 ▶ @{ defined($second->{$_}) ? $second->{$_} : [] },
Branch: 1 branch
BranchTrueFalseTotal
defined $second->{$_} ? : 2 493 495
429 ]
430 } qw(before_request after_response on_error)
431 };
432 }
433  
434 363 ▶sub _run_hooks {
Subroutine _run_hooks: called CC=4 SCAR=13.9
435 363 my ($callbacks, @args) = @_;
436 363 ▶ for my $callback (@{ defined($callbacks) ? $callbacks : [] }) {
Branch: 1 branch
BranchTrueFalseTotal
defined $callbacks ? : 363 0 363
437 83 my $ok = eval { $callback->(@args); 1 };
438 83 ▶ return $@ if !$ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 5 78 83
439 }
440 358 return undef;
441 }
442  
443 5 ▶sub _hook_error {
Subroutine _hook_error: called CC=3 SCAR=11.0
444 5 my ($cause, $method, $url) = @_;
445 5 my $cause_class = blessed($cause);
446 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
447 3 return HTTP::API::Core::Error->new(
448 category => 'hook',
449 method => $method,
450 url => $url,
451 retryable => 0,
452 message => "HTTP API core hook failed: $cause",
453 );
454 }
455  
456 186 ▶sub _normalize_retry {
Subroutine _normalize_retry: called CC=20 SCAR=30.0
457 186 my ($retry) = @_;
458 186 my %copy = %$retry;
459 186 ▶ 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 186 186
460  
461 186 ▶ my $attempts = exists $copy{attempts} ? delete($copy{attempts}) : 3;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'attempts'} ? : 101 85 186
462 186 my $attempts_ref = ref($attempts);
463 186 ▶ 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 172 186
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
113172
not defined $attempts or $attempts_ref ne "" or not $attempts =~ /\A[0-9]+\z/
l!l&&r!l&&!r
47175
not defined $attempts or $attempts_ref ne ""
l!l&&r!l&&!r
22182
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
464 if !defined($attempts) || $attempts_ref ne ''
465 || $attempts !~ /\A[0-9]+\z/ || $attempts < 1;
466  
467 my $base_delay = exists $copy{base_delay}
468 ? delete($copy{base_delay})
469 172 ▶ : 0.25;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'base_delay'} ? : 50 122 172
470 172 ▶ die "retry base_delay must be a non-negative number\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _non_negative_number($base_delay) 6 166 172
471 if !_non_negative_number($base_delay);
472  
473 my $max_delay = exists $copy{max_delay}
474 ? delete($copy{max_delay})
475 166 ▶ : 5;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'max_delay'} ? : 23 143 166
476 166 ▶ die "retry max_delay must be a non-negative number\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _non_negative_number($max_delay) 4 162 166
477 if !_non_negative_number($max_delay);
478  
479 162 ▶ my $jitter = exists $copy{jitter} ? delete($copy{jitter}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'jitter'} ? : 44 118 162
480 162 ▶ die "retry jitter must be a scalar\n" if ref($jitter) ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ref $jitter ne "" 0 162 162
481 162 ▶ $jitter = $jitter ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$jitter ? : 118 44 162
482  
483 my $methods = exists $copy{methods}
484 ? delete($copy{methods})
485 162 ▶ : [qw(GET HEAD PUT DELETE OPTIONS)];
Branch: 1 branch
BranchTrueFalseTotal
exists $copy{'methods'} ? : 10 152 162
486 162 ▶ die "retry methods must be an array reference\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $methods ne "ARRAY" 1 161 162
487 if ref($methods) ne 'ARRAY';
488  
489 die "retry methods must contain only scalar values\n"
490 161 ▶ if grep { ref($_) ne '' } @$methods;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} @$methods 1 160 161
491 die "retry methods must not contain empty values\n"
492 160 ▶ if grep { !defined($_) || $_ eq '' } @$methods;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_ or $_ eq "";} @$methods 1 159 160
Condition66%2/3
!defined($_) || $_ eq ''
l!l&&r!l&&!r
01771
MC/DC50%1/2
!defined($_) || $_ eq ''
!defined($_) $_ eq ''
Truth table66%2/3
not(A) || B
A= !defined($_)B= $_ eq ''
ABresult
1X1
011
000
493 159 my @methods = map { uc($_) } @$methods;
494  
495 159 die "unknown retry option: $_\n" for sort keys %copy;
496  
497 return {
498 158 attempts => 0 + $attempts,
499 base_delay => 0 + $base_delay,
500 max_delay => 0 + $max_delay,
501 jitter => $jitter,
502 methods => \@methods,
503 };
504 }
505  
506 195 ▶sub _method_is_retryable {
Subroutine _method_is_retryable: called CC=2 SCAR=6.9
507 195 my ($method, $retry) = @_;
508 195 my %allowed = map { $_ => 1 } @{ $retry->{methods} };
509 195 ▶ return $allowed{$method} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$allowed{$method} ? : 169 26 195
510 }
511  
512 32 ▶sub _retry_delay {
Subroutine _retry_delay: called CC=8 SCAR=21.3
513 32 my ($retry, $attempt, $error) = @_;
514  
515 32 my $retry_after = _retry_after_delay($error->retry_after, time);
516 32 ▶ return $retry_after if defined $retry_after;
Branch: 1 branch
BranchTrueFalseTotal
if defined $retry_after 0 32 32
517  
518 32 my $rate_limit = $error->rate_limit;
519 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
520 7 my $wait = $rate_limit->wait_seconds;
521 7 ▶ return $wait if defined $wait;
Branch: 1 branch
BranchTrueFalseTotal
if defined $wait 5 2 7
522 }
523  
524 27 my $delay = $retry->{base_delay} * (2 ** ($attempt - 1));
525 27 ▶ $delay = $retry->{max_delay} if $delay > $retry->{max_delay};
Branch: 1 branch
BranchTrueFalseTotal
if $delay > $retry->{"max_delay"} 0 27 27
526 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
527 27 return $delay;
528 }
529  
530 57 ▶sub _retry_after_delay {
Subroutine _retry_after_delay: called CC=12 SCAR=25.1
531 57 my ($value, $now) = @_;
532 57 ▶ return undef if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 26 31 57
533  
534 31 ▶ return 0 + $value
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/ 4 27 31
535 if $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/;
536  
537 27 my %month = (
538 Jan => 0, Feb => 1, Mar => 2, Apr => 3,
539 May => 4, Jun => 5, Jul => 6, Aug => 7,
540 Sep => 8, Oct => 9, Nov => 10, Dec => 11,
541 );
542  
543 return undef
544 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
545  
546 20 my ($weekday, $day, $mon, $year, $hour, $min, $sec) =
547 ($1, $2, $3, $4, $5, $6, $7);
548  
549 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
550  
551 19 my $epoch = eval {
552 19 timegm($sec, $min, $hour, $day, $month{$mon}, $year);
553 };
554 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
555  
556 # Time::Local interprets some small year numbers relative to 1900 or
557 # 2000. An HTTP-date year is literal and must survive the conversion.
558 19 my @utc = gmtime($epoch);
559 19 ▶ return undef if $utc[5] + 1900 != $year;
Branch: 1 branch
BranchTrueFalseTotal
if $utc[5] + 1900 != $year 5 14 19
560  
561 # IMF-fixdate includes a weekday, which must agree with the date.
562 # Inconsistent values are malformed, so use the normal retry fallback.
563 14 my %weekday_number = (
564 Sun => 0, Mon => 1, Tue => 2, Wed => 3,
565 Thu => 4, Fri => 5, Sat => 6,
566 );
567 14 ▶ return undef if $utc[6] != $weekday_number{$weekday};
Branch: 1 branch
BranchTrueFalseTotal
if $utc[6] != $weekday_number{$weekday} 6 8 14
568  
569 8 my $delay = $epoch - $now;
570 8 ▶ return $delay > 0 ? $delay : 0;
Branch: 1 branch
BranchTrueFalseTotal
$delay > 0 ? : 6 2 8
571 }
572  
573 187 ▶sub _append_query {
Subroutine _append_query: called CC=11 SCAR=24.0
574 187 my ($url, $query) = @_;
575 187 my @pairs;
576  
577 187 for my $key (sort keys %$query) {
578 18 ▶ die "query parameter names must be scalars\n" if ref($key) ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ref $key ne "" 0 18 18
579 18 my $value = $query->{$key};
580 18 ▶ next if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 17 18
581  
582 17 my @values;
583 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
584 die "query parameter array values must contain only scalars or undef\n"
585 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
586 1 @values = grep { defined $_ } @$value;
587 }
588 elsif (ref($value) ne '') {
589 1 die "query values must be scalars, array references, or undef\n";
590 }
591 else {
592 14 @values = ($value);
593 }
594  
595 push @pairs, map {
596 15 _uri_escape($key) . '=' . _uri_escape($_)
597 } @values;
598 }
599  
600 185 ▶ return $url if !@pairs;
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs 173 12 185
601  
602 12 my $fragment = '';
603 12 ▶ if ($url =~ s/(#.*)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ s/(#.*)\z//) 2 10 12
604 2 $fragment = $1;
605 }
606  
607 12 ▶ my $separator = index($url, '?') >= 0 ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
index($url, '?') != -1 ? : 2 10 12
608 12 return $url . $separator . join('&', @pairs) . $fragment;
609 }
610  
611 32 ▶sub _uri_escape {
Subroutine _uri_escape: called CC=3 SCAR=11.0
612 32 my ($value) = @_;
613 32 ▶ my $bytes = defined($value) ? "$value" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 32 0 32
614 32 ▶ utf8::encode($bytes) if utf8::is_utf8($bytes);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($bytes) 1 31 32
615 32 $bytes =~ s/([^A-Za-z0-9\-._~])/sprintf('%%%02X', ord($1))/ge;
616 32 return $bytes;
617 }
618  
619 184 ▶sub _validate_request_url {
Subroutine _validate_request_url: called CC=5 SCAR=16.1
620 184 my ($url) = @_;
621 184 my $url_ref = ref($url);
622 184 ▶ 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 182 184
Condition83%5/6
not defined $url or $url_ref ne "" or $url eq ""
l!l&&r!l&&!r
11182
not defined $url or $url_ref ne ""
l!l&&r!l&&!r
01183
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
623 if !defined($url) || $url_ref ne '' || $url eq '';
624 182 ▶ 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 176 182
625 if $url !~ m{\Ahttps?://(?:[^/?#\s@]+@)?(?:\[[0-9A-Fa-f:.]+\]|[^/?#\s:@]+)(?::[0-9]+)?(?:[/?#]|\z)}i;
626 }
627  
628 # HTTP::Tiny may return array references for repeated response headers.
629 # Request headers retain their stricter scalar-only contract.
630 156 ▶sub _valid_response_headers {
Subroutine _valid_response_headers: called CC=9 SCAR=22.0
631 156 my ($headers) = @_;
632 156 ▶ return 1 if !defined $headers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headers 4 152 156
633 152 ▶ return 0 if ref($headers) ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $headers ne "HASH" 1 151 152
634  
635 151 for my $value (values %$headers) {
636 79 ▶ next if !defined($value) || ref($value) eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or ref $value eq "" 71 8 79
Condition66%2/3
not defined $value or ref $value eq ""
l!l&&r!l&&!r
0718
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
637 8 ▶ return 0 if ref($value) ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value ne "ARRAY" 1 7 8
638 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
639 }
640 148 return 1;
641 }
642  
643 547 ▶sub _validate_header_values {
Subroutine _validate_header_values: called CC=13 SCAR=25.8
644 547 my ($headers) = @_;
645 die "header names must be scalars\n"
646 547 ▶ if grep { ref($_) ne '' } keys %$headers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {ref $_ ne "";} keys %$headers 0 547 547
647 die "header values must be scalars or undef\n"
648 547 ▶ if grep { defined($_) && ref($_) ne '' } values %$headers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {defined $_ and ref $_ ne "";} values %$headers 3 544 547
Condition100%3/3
defined $_ && ref $_ ne ''
!ll&&!rl&&r
51153
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
649  
650 # Reject ambiguous case-insensitive names before sending a request.
651 # Equal values (or two undefined values) are not conflicting.
652 544 my %seen;
653 544 for my $name (sort keys %$headers) {
654 120 my $key = lc $name;
655 120 my $value = $headers->{$name};
656 120 ▶ if (exists $seen{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$key}) { } 9 111 120
657 9 my $previous = $seen{$key};
658 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
659 if (defined($previous) && !defined($value))
660 || (!defined($previous) && defined($value))
661 || (defined($previous) && defined($value)
662 && $previous ne $value);
663 }
664 else {
665 111 $seen{$key} = $value;
666 }
667 }
668 }
669  
670 18 ▶sub _set_header_if_absent {
Subroutine _set_header_if_absent: called CC=2 SCAR=6.9
671 18 my ($headers, $name, $value) = @_;
672 18 my $wanted = lc $name;
673 18 ▶ return if grep { lc($_) eq $wanted } keys %$headers;
Branch: 1 branch
BranchTrueFalseTotal
if grep {lc $_ eq $wanted;} keys %$headers 4 14 18
674 14 $headers->{$name} = $value;
675 }
676  
677 338 ▶sub _non_negative_number {
Subroutine _non_negative_number: called CC=4 SCAR=14.1
678 338 my ($value) = @_;
679 338 ▶ 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
100328
defined $value and ref $value eq "" and $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/
!ll&&!rl&&r
28328
defined $value and ref $value eq ""
!ll&&!rl&&r
02336
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
680 && ref($value) eq ''
681 && $value =~ /\A(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)\z/
682 && $value >= 0;
683 }
684  
685 184 ▶sub _join_url {
Subroutine _join_url: called CC=1 SCAR=0.0
686 184 my ($self, $path) = @_;
687 184 $path =~ s{\A/+}{};
688 184 return $self->{base_url} . '/' . $path;
689 }
690  
691 59 ▶sub _retryable_status {
Subroutine _retryable_status: called CC=6 SCAR=17.9
692 59 my ($status) = @_;
693 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
694 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
695 13 return 0;
696 }
697  
698 39 1;
699  
700 __END__
« blib/lib/HTTP/API/Core/Pagination.pm ↑ Summary blib/lib/HTTP/API/Core/RateLimit.pm »