| 1 | package WWW::Hetzner::Role::HTTP; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: HTTP client role for Hetzner API clients | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 25 | ▶ | use WWW::Hetzner::HTTPRequest; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 25 | ▶ | use WWW::Hetzner::HTTPResponse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 25 | ▶ | use WWW::Hetzner::LWPIO; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use JSON::MaybeXS qw(decode_json encode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 25 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 25 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 25 | our $VERSION = '0.100'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 25 | requires 'token'; | |||||||||||||||||||||||||||
| 17 | 25 | requires 'base_url'; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | has io => ( | ||||||||||||||||||||||||||||
| 20 | is => 'lazy', | ||||||||||||||||||||||||||||
| 21 | 1 | ▶ | builder => sub { WWW::Hetzner::LWPIO->new }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=1.2
Subroutine
_build_io: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 22 | 25 | ); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub get { | ||||||||||||||||||||||||||||
| 26 | 69 | ▶ | my ($self, $path, %params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 69 | return $self->_request('GET', $path, %params); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub post { | ||||||||||||||||||||||||||||
| 32 | 54 | ▶ | my ($self, $path, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
post: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 33 | 54 | return $self->_request('POST', $path, body => $data); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | sub put { | ||||||||||||||||||||||||||||
| 38 | 4 | ▶ | my ($self, $path, $data) = @_; | ||||||||||||||||||||||||||
|
Subroutine
put: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | 4 | return $self->_request('PUT', $path, body => $data); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub delete { | ||||||||||||||||||||||||||||
| 44 | 12 | ▶ | my ($self, $path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 12 | return $self->_request('DELETE', $path); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | sub _set_auth { | ||||||||||||||||||||||||||||
| 50 | 129 | ▶ | my ($self, $headers) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_set_auth: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 129 | $headers->{Authorization} = 'Bearer ' . $self->token; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | sub _build_request { | ||||||||||||||||||||||||||||
| 56 | 142 | ▶ | my ($self, $method, $path, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_request: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 142 | my $url = $self->base_url . $path; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # Add query params for GET | ||||||||||||||||||||||||||||
| 61 | 142 | ▶ | 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"}
| |||||||||||||||||||||||||||||
| 62 | 36 | my @pairs; | |||||||||||||||||||||||||||
| 63 | 36 | for my $k (keys %{$opts{params}}) { | |||||||||||||||||||||||||||
| 64 | 10 | my $v = $opts{params}{$k}; | |||||||||||||||||||||||||||
| 65 | 10 | ▶ | next unless defined $v; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 9 | push @pairs, "$k=$v"; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 36 | ▶ | $url .= '?' . join('&', @pairs) if @pairs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 142 | $log->debug("$method $url"); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 142 | my %headers; | |||||||||||||||||||||||||||
| 74 | 142 | $self->_set_auth(\%headers); | |||||||||||||||||||||||||||
| 75 | 142 | $headers{'Content-Type'} = 'application/json'; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 142 | my %req_args = ( | |||||||||||||||||||||||||||
| 78 | method => $method, | ||||||||||||||||||||||||||||
| 79 | url => $url, | ||||||||||||||||||||||||||||
| 80 | headers => \%headers, | ||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 142 | ▶ | if ($opts{body}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 59 | $req_args{content} = encode_json($opts{body}); | |||||||||||||||||||||||||||
| 85 | 59 | $log->debugf("Body: %s", $req_args{content}); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 142 | return WWW::Hetzner::HTTPRequest->new(%req_args); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub _parse_response { | ||||||||||||||||||||||||||||
| 93 | 142 | ▶ | my ($self, $response, $method, $path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_parse_response: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 142 | $log->debugf("Response: %s", $response->status); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 142 | my $data; | |||||||||||||||||||||||||||
| 98 | 142 | ▶ | 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*[\{\[]/
| |||||||||||||||||||||||||||||
| 99 | 140 | $data = decode_json($response->content); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 142 | ▶ | 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
| |||||||||||||||||||||||||||||
| 103 | 4 | ▶ | 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
| |||||||||||||||||||||||||||||
| 104 | 4 | $log->errorf("API error: %s", $error); | |||||||||||||||||||||||||||
| 105 | 4 | croak "Hetzner API error: $error"; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 138 | $log->infof("%s %s -> %s", $method, $path, $response->status); | |||||||||||||||||||||||||||
| 109 | 138 | return $data; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | sub _request { | ||||||||||||||||||||||||||||
| 114 | 139 | ▶ | my ($self, $method, $path, %opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_request: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 139 | ▶ | croak "No API token configured" unless $self->token; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 139 | my $req = $self->_build_request($method, $path, %opts); | |||||||||||||||||||||||||||
| 119 | 139 | my $response = $self->io->call($req); | |||||||||||||||||||||||||||
| 120 | 138 | return $self->_parse_response($response, $method, $path); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 25 | 1; | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | __END__ | ||||||||||||||||||||||||||||