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