| 1 | package WWW::Hetzner::Role::HTTP; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: HTTP client role for Hetzner API clients | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 56 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 56 | ▶ | use WWW::Hetzner::HTTPRequest; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 56 | ▶ | use WWW::Hetzner::HTTPResponse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 56 | ▶ | use WWW::Hetzner::LWPIO; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 56 | ▶ | use JSON::MaybeXS qw(decode_json encode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 56 | ▶ | use URI::Escape (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 56 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 56 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 56 | our $VERSION = '0.101'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 56 | requires 'token'; | |||||||||||||||||||||||||||
| 18 | 56 | 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 | 56 | ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | has sleeper => ( | ||||||||||||||||||||||||||||
| 27 | is => 'rw', | ||||||||||||||||||||||||||||
| 28 | 187 | ▶ | default => sub { sub { sleep $_[0] } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 56 | ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 262 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 262 | my ($self, $path, %params) = @_; | |||||||||||||||||||||||||||
| 34 | 262 | return $self->_request('GET', $path, %params); | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 169 | ▶ | sub post { | ||||||||||||||||||||||||||
|
Subroutine
post: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 169 | my ($self, $path, $data) = @_; | |||||||||||||||||||||||||||
| 40 | 169 | 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 | 440 | ▶ | sub _set_auth { | ||||||||||||||||||||||||||
|
Subroutine
_set_auth: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | 440 | my ($self, $headers) = @_; | |||||||||||||||||||||||||||
| 58 | 440 | $headers->{Authorization} = 'Bearer ' . $self->token; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 440 | ▶ | sub _content_type { 'application/json' } | ||||||||||||||||||||||||||
|
Subroutine
_content_type: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 165 | ▶ | sub _encode_body { | ||||||||||||||||||||||||||
|
Subroutine
_encode_body: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | 165 | my ($self, $body) = @_; | |||||||||||||||||||||||||||
| 67 | 165 | return encode_json($body); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 507 | ▶ | sub _build_request { | ||||||||||||||||||||||||||
|
Subroutine
_build_request: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 72 | 507 | my ($self, $method, $path, %opts) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 507 | my $url = $self->base_url . $path; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | # Add query params for GET | ||||||||||||||||||||||||||||
| 77 | 507 | ▶ | 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 | 122 | my @pairs; | |||||||||||||||||||||||||||
| 79 | 122 | for my $key (keys %{$opts{params}}) { | |||||||||||||||||||||||||||
| 80 | 101 | my $value = $opts{params}{$key}; | |||||||||||||||||||||||||||
| 81 | 101 | ▶ | my @values = ref $value eq 'ARRAY' ? @$value : ($value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 101 | for my $value (@values) { | |||||||||||||||||||||||||||
| 83 | 115 | ▶ | next unless defined $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 112 | push @pairs, URI::Escape::uri_escape_utf8($key) . '=' . | |||||||||||||||||||||||||||
| 85 | URI::Escape::uri_escape_utf8($value); | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | 122 | ▶ | $url .= '?' . join('&', @pairs) if @pairs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 507 | $log->debug("$method $url"); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 507 | my %headers; | |||||||||||||||||||||||||||
| 94 | 507 | $self->_set_auth(\%headers); | |||||||||||||||||||||||||||
| 95 | 507 | $headers{'Content-Type'} = $self->_content_type; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 507 | my %req_args = ( | |||||||||||||||||||||||||||
| 98 | method => $method, | ||||||||||||||||||||||||||||
| 99 | url => $url, | ||||||||||||||||||||||||||||
| 100 | headers => \%headers, | ||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 507 | ▶ | if ($opts{body}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 190 | $req_args{content} = $self->_encode_body($opts{body}); | |||||||||||||||||||||||||||
| 105 | 190 | $log->debugf("Body: %s", $req_args{content}); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 507 | return WWW::Hetzner::HTTPRequest->new(%req_args); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 502 | ▶ | sub _parse_response { | ||||||||||||||||||||||||||
|
Subroutine
_parse_response: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 113 | 502 | my ($self, $response, $method, $path) = @_; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 502 | $log->debugf("Response: %s", $response->status); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 502 | my $data; | |||||||||||||||||||||||||||
| 118 | 502 | ▶ | 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*[\{\[]/
| |||||||||||||||||||||||||||||
| 119 | 488 | $data = decode_json($response->content); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 502 | ▶ | 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
| |||||||||||||||||||||||||||||
| 123 | 6 | ▶ | my $error = $data->{error}{message} // $response->status; | ||||||||||||||||||||||||||
|
Condition66%2/3
$data->{"error"}{"message"} // $response->status
MC/DC0%0/2
$data->{"error"}{"message"} // $response->status
$data->{"error"}{"message"}
$response->status
Truth table66%2/3
A // B
A= $data->{"error"}{"message"}B= $response->status
| |||||||||||||||||||||||||||||
| 124 | 6 | $log->errorf("API error: %s", $error); | |||||||||||||||||||||||||||
| 125 | 6 | croak "Hetzner API error: $error"; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 496 | $log->infof("%s %s -> %s", $method, $path, $response->status); | |||||||||||||||||||||||||||
| 129 | 496 | return $data; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 499 | ▶ | sub _request { | ||||||||||||||||||||||||||
|
Subroutine
_request: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 134 | 499 | my ($self, $method, $path, %opts) = @_; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 499 | ▶ | croak "No API token configured" unless $self->token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 499 | my $req = $self->_build_request($method, $path, %opts); | |||||||||||||||||||||||||||
| 139 | 499 | my $response = $self->io->call($req); | |||||||||||||||||||||||||||
| 140 | 498 | return $self->_parse_response($response, $method, $path); | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 56 | 1; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||