lib/WWW/Hetzner/Role/HTTP.pm

filter: Statementstmt 98.0% 98 / 100 Branchbran 89.2% 25 / 28 Conditioncond 78.5% 11 / 14 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 91.3% 21 / 23 Podpod 100% 4 / 4 totaltotal 92.7% 165 / 178 CC 23 SCAR 16.8
CC
23
Cov
94%
_parse_response
11.1
_build_request
9.0
_mask_body
4.0
CRAP 5.4
« lib/WWW/Hetzner/Cloud/API/PlacementGroups.pm ↑ Summary lib/WWW/Hetzner/Robot/Server.pm »
1 package WWW::Hetzner::Role::HTTP;
2  
3 # ABSTRACT: HTTP client role for Hetzner API clients
4  
5 60 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 60 ▶use WWW::Hetzner::HTTPRequest;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 60 ▶use WWW::Hetzner::HTTPResponse;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 60 ▶use WWW::Hetzner::LWPIO;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 60 ▶use JSON::MaybeXS qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 60 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 60 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 60 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 60 our $VERSION = '0.102';
15  
16  
17 60 requires 'token';
18 60 requires 'base_url';
19  
20 has io => (
21 is => 'lazy',
22 0 ▶ builder => sub { WWW::Hetzner::LWPIO->new },
Subroutine _build_io: not called CC=1 SCAR=1.2
Subroutine __ANON__: called CC=1 SCAR=1.2
23 60 );
24  
25  
26 has sleeper => (
27 is => 'rw',
28 0 ▶ default => sub { sub { sleep $_[0] } },
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
29 60 );
30  
31  
32 342 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
33 342 my ($self, $path, %params) = @_;
34 342 return $self->_request('GET', $path, %params);
35 }
36  
37  
38 188 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod documented
39 188 my ($self, $path, $data) = @_;
40 188 return $self->_request('POST', $path, body => $data);
41 }
42  
43  
44 18 ▶sub put {
Subroutine put: called CC=1 SCAR=0.0
Pod documented
45 18 my ($self, $path, $data) = @_;
46 18 return $self->_request('PUT', $path, body => $data);
47 }
48  
49  
50 51 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
51 51 my ($self, $path) = @_;
52 51 return $self->_request('DELETE', $path);
53 }
54  
55  
56 540 ▶sub _set_auth {
Subroutine _set_auth: called CC=1 SCAR=0.0
57 540 my ($self, $headers) = @_;
58 540 $headers->{Authorization} = 'Bearer ' . $self->token;
59 }
60  
61  
62 540 ▶sub _content_type { 'application/json' }
Subroutine _content_type: called CC=1 SCAR=0.0
63  
64  
65 370 ▶sub _encode_body {
Subroutine _encode_body: called CC=1 SCAR=0.0
66 370 my ($self, $body) = @_;
67 370 return encode_json($body);
68 }
69  
70  
71 607 ▶sub _build_request {
Subroutine _build_request: called CC=9 SCAR=22.0
72 607 my ($self, $method, $path, %opts) = @_;
73  
74 607 my $url = $self->base_url . $path;
75  
76 # Add query params for GET
77 607 ▶ if ($method eq 'GET' && $opts{params}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "GET" and $opts{"params"}) 189 418 607
Condition100%3/3
$method eq "GET" and $opts{"params"}
!ll&&!rl&&r
261157189
MC/DC100%2/2
$method eq "GET" and $opts{"params"}
$method eq "GET" $opts{"params"}
Truth table100%3/3
A and B
A= $method eq "GET"B= $opts{"params"}
ABresult
0X0
100
111
78 189 my @pairs;
79 189 for my $key (keys %{$opts{params}}) {
80 111 my $value = $opts{params}{$key};
81 111 ▶ my @values = ref $value eq 'ARRAY' ? @$value : ($value);
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 14 97 111
82 111 for my $value (@values) {
83 125 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 3 122 125
84 122 push @pairs, URI::Escape::uri_escape_utf8($key) . '=' .
85 URI::Escape::uri_escape_utf8($value);
86 }
87 }
88 189 ▶ $url .= '?' . join('&', @pairs) if @pairs;
Branch: 1 branch
BranchTrueFalseTotal
if @pairs 61 128 189
89 }
90  
91 607 $log->debug("$method $url");
92  
93 607 my %headers;
94 607 $self->_set_auth(\%headers);
95 607 $headers{'Content-Type'} = $self->_content_type;
96  
97 607 my %req_args = (
98 method => $method,
99 url => $url,
100 headers => \%headers,
101 );
102  
103 607 ▶ if ($opts{body}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"body"}) 210 397 607
104 210 $req_args{content} = $self->_encode_body($opts{body});
105 210 $log->debugf("Body: %s", $self->_encode_body($self->_mask_body($opts{body})));
106 }
107  
108 607 return WWW::Hetzner::HTTPRequest->new(%req_args);
109 }
110  
111  
112 60 my @SECRET_KEYS = qw(password private_key user_data);
113  
114 210 ▶sub _mask_body {
Subroutine _mask_body: called CC=4 SCAR=13.9
115 210 my ($self, $body) = @_;
116 210 ▶ return $body unless ref $body eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $body eq "HASH" 0 210 210
117 210 my %copy = %$body;
118 210 for my $key (@SECRET_KEYS) {
119 630 ▶ $copy{$key} = '********' if defined $copy{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $copy{$key} 11 619 630
120 }
121 210 return \%copy;
122 }
123  
124 603 ▶sub _parse_response {
Subroutine _parse_response: called CC=11 SCAR=24.1
125 603 my ($self, $response, $method, $path) = @_;
126  
127 603 $log->debugf("Response: %s", $response->status);
128  
129 603 my $data;
130 603 ▶ if ($response->content && $response->content =~ /^\s*[\{\[]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->content and $response->content =~ /^\s*[\{\[]/) 587 16 603
Condition100%3/3
$response->content and $response->content =~ /^\s*[\{\[]/
!ll&&!rl&&r
142587
MC/DC100%2/2
$response->content and $response->content =~ /^\s*[\{\[]/
$response->content $response->content =~ /^\s*[\{\[]/
Truth table100%3/3
A and B
A= $response->contentB= $response->content =~ /^\s*[\{\[]/
ABresult
0X0
100
111
131 587 $data = decode_json($response->content);
132 }
133  
134 603 ▶ unless ($response->status >= 200 && $response->status < 300) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->status >= 200 and $response->status < 300) 9 594 603
Condition66%2/3
$response->status >= 200 and $response->status < 300
!ll&&!rl&&r
09594
MC/DC50%1/2
$response->status >= 200 and $response->status < 300
$response->status >= 200 $response->status < 300
Truth table66%2/3
A and B
A= $response->status >= 200B= $response->status < 300
ABresult
0X0
100
111
135 my $error = (ref $data eq 'HASH' && ref $data->{error} eq 'HASH'
136 9 ▶ ? $data->{error}{message} : undef);
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq 'HASH' && ref $data->{'error'} eq 'HASH' ? : 6 3 9
Condition66%2/3
ref $data eq "HASH" and ref $data->{"error"} eq "HASH"
!ll&&!rl&&r
306
MC/DC50%1/2
ref $data eq "HASH" and ref $data->{"error"} eq "HASH"
ref $data eq "HASH" ref $data->{"error"} eq "HASH"
Truth table66%2/3
A and B
A= ref $data eq "HASH"B= ref $data->{"error"} eq "HASH"
ABresult
0X0
100
111
137 9 ▶ unless (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $error) 3 6 9
138 3 $error = $response->status;
139 3 ▶ my $text = $response->content // '';
Condition50%1/2
$response->content // ""
l!l
30
MC/DC0%0/1
$response->content // ""
$response->content
Truth table50%1/2
A
A= $response->content
Aresult
11
00
140 3 $text =~ s/<[^>]*>/ /g;
141 3 $text =~ s/\s+/ /g;
142 3 $text =~ s/^ //; $text =~ s/ $//;
143 3 ▶ $text = substr($text, 0, 200) . '...' if length $text > 200;
Branch: 1 branch
BranchTrueFalseTotal
if length $text > 200 0 3 3
144 3 ▶ $error .= " $text" if length $text;
Branch: 1 branch
BranchTrueFalseTotal
if length $text 2 1 3
145 }
146 9 $log->errorf("API error: %s", $error);
147 9 croak "Hetzner API error: $error";
148 }
149  
150 594 $log->infof("%s %s -> %s", $method, $path, $response->status);
151 594 return $data;
152 }
153  
154  
155 598 ▶sub _request {
Subroutine _request: called CC=2 SCAR=7.0
156 598 my ($self, $method, $path, %opts) = @_;
157  
158 598 ▶ croak "No API token configured" unless $self->token;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->token 0 598 598
159  
160 598 my $req = $self->_build_request($method, $path, %opts);
161 598 my $response = $self->io->call($req);
162 597 return $self->_parse_response($response, $method, $path);
163 }
164  
165  
166 60 1;
167  
168 __END__
« lib/WWW/Hetzner/Cloud/API/PlacementGroups.pm ↑ Summary lib/WWW/Hetzner/Robot/Server.pm »