| 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
Condition100%3/3
$method eq "GET" and $opts{"params"}
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"}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 82 | 111 | for my $value (@values) { | |||||||||||||||||||||||||||
| 83 | 125 | ▶ | next unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 117 | 210 | my %copy = %$body; | |||||||||||||||||||||||||||
| 118 | 210 | for my $key (@SECRET_KEYS) { | |||||||||||||||||||||||||||
| 119 | 630 | ▶ | $copy{$key} = '********' if defined $copy{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$response->content and $response->content =~ /^\s*[\{\[]/
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*[\{\[]/
| |||||||||||||||||||||||||||||
| 131 | 587 | $data = decode_json($response->content); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 603 | ▶ | unless ($response->status >= 200 && $response->status < 300) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$response->status >= 200 and $response->status < 300
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
| |||||||||||||||||||||||||||||
| 135 | my $error = (ref $data eq 'HASH' && ref $data->{error} eq 'HASH' | ||||||||||||||||||||||||||||
| 136 | 9 | ▶ | ? $data->{error}{message} : undef); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $data eq "HASH" and ref $data->{"error"} eq "HASH"
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"
| |||||||||||||||||||||||||||||
| 137 | 9 | ▶ | unless (defined $error) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 3 | $error = $response->status; | |||||||||||||||||||||||||||
| 139 | 3 | ▶ | my $text = $response->content // ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$response->content // ""
MC/DC0%0/1
$response->content // ""
$response->content
Truth table50%1/2
A
A= $response->content
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 144 | 3 | ▶ | $error .= " $text" if length $text; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||