lib/WWW/Hetzner/LWPIO.pm

filter: Statementstmt 94.4% 34 / 36 Branchbran 50.0% 2 / 4 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 78.3% 47 / 60 CC 7 SCAR 14.9
CC
7
Cov
83%
call
7.9
__ANON__
1.1
_build_ua
1.1
CRAP 4.5
« lib/WWW/Hetzner/CLI/Cmd/Volume.pm ↑ Summary lib/WWW/Hetzner/CLI/Cmd/Firewall/Cmd/AddRule.pm »
1 package WWW::Hetzner::LWPIO;
2  
3 # ABSTRACT: Synchronous HTTP backend using LWP::UserAgent
4  
5 60 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 60 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 60 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 60 ▶use JSON::MaybeXS qw(encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 60 ▶use WWW::Hetzner::HTTPResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 60 with 'WWW::Hetzner::Role::IO';
12  
13 60 our $VERSION = '0.102';
14  
15  
16 60 has timeout => (is => 'ro', default => 30);
17  
18  
19 has ua => (
20 is => 'lazy',
21 0 ▶ builder => sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine _build_ua: called CC=1 SCAR=1.2
22 0 my $self = shift;
23 0 LWP::UserAgent->new(
24 agent => 'WWW-Hetzner/' . $VERSION,
25 timeout => $self->timeout,
26 );
27 },
28 60 );
29  
30  
31 1 ▶sub call {
Subroutine call: called CC=7 SCAR=20.6
Pod documented
32 1 my ($self, $req) = @_;
33  
34 1 my $http_req = HTTP::Request->new($req->method => $req->url);
35  
36 1 my $headers = $req->headers;
37 1 for my $header (keys %$headers) {
38 2 $http_req->header($header => $headers->{$header});
39 }
40  
41 1 ▶ $http_req->content($req->content) if $req->has_content;
Branch: 1 branch
BranchTrueFalseTotal
if $req->has_content 0 1 1
42  
43 1 my $response = $self->ua->request($http_req);
44  
45 1 ▶ my $content = $response->decoded_content // '';
Condition50%1/2
$response->decoded_content // ""
l!l
10
MC/DC0%0/1
$response->decoded_content // ""
$response->decoded_content
Truth table50%1/2
A
A= $response->decoded_content
Aresult
11
00
46  
47 # LWP synthesises a 5xx response when the request never reached the
48 # server (connection refused, DNS, TLS, timeout). Report its reason in
49 # the API error shape instead of a bare status code.
50 1 ▶ if (($response->header('Client-Warning') // '') eq 'Internal response') {
Branch: 1 branch
BranchTrueFalseTotal
if (($response->header("Client-Warning") // "") eq "Internal response") 1 0 1
Condition50%1/2
$response->header("Client-Warning") // ""
l!l
10
MC/DC0%0/1
$response->header("Client-Warning") // ""
$response->header("Client-Warning")
Truth table50%1/2
A
A= $response->header("Client-Warning")
Aresult
11
00
51 1 $content =~ s/\s+$//;
52 1 ▶ $content = encode_json({
Condition33%1/3
$content || $response->message
l!l&&r!l&&!r
100
MC/DC0%0/2
$content || $response->message
$content $response->message
Truth table33%1/3
A || B
A= $contentB= $response->message
ABresult
1X1
011
000
53 error => { code => 'transport_error', message => $content || $response->message },
54 });
55 }
56  
57 1 return WWW::Hetzner::HTTPResponse->new(
58 status => $response->code,
59 content => $content,
60 );
61 }
62  
63  
64  
65 60 1;
66  
67 __END__
« lib/WWW/Hetzner/CLI/Cmd/Volume.pm ↑ Summary lib/WWW/Hetzner/CLI/Cmd/Firewall/Cmd/AddRule.pm »