| 1 | package Net::Async::Hetzner::Robot; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # ABSTRACT: Async Hetzner Robot API client for IO::Async | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use parent 'IO::Async::Notifier'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Future; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use WWW::Hetzner::Robot; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use WWW::Hetzner::HTTPResponse; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | our $VERSION = '0.003'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 8 | ▶ | sub configure { | ||||||||||||||||||||||||||
|
Subroutine
configure: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 8 | my ($self, %params) = @_; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | $self->{user} = delete $params{user} if exists $params{user}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 8 | ▶ | $self->{password} = delete $params{password} if exists $params{password}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 8 | ▶ | $self->{base_url} = delete $params{base_url} if exists $params{base_url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 8 | $self->SUPER::configure(%params); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 8 | ▶ | sub user { | ||||||||||||||||||||||||||
|
Subroutine
user: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||
| 30 | 8 | ▶ | $self->{user} // $ENV{HETZNER_ROBOT_USER}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"user"} // $ENV{"HETZNER_ROBOT_USER"}
MC/DC50%1/2
$self->{"user"} // $ENV{"HETZNER_ROBOT_USER"}
$self->{"user"}
$ENV{"HETZNER_ROBOT_USER"}
Truth table66%2/3
A // B
A= $self->{"user"}B= $ENV{"HETZNER_ROBOT_USER"}
| |||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 7 | ▶ | sub password { | ||||||||||||||||||||||||||
|
Subroutine
password: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 35 | 7 | ▶ | $self->{password} // $ENV{HETZNER_ROBOT_PASSWORD}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"password"} // $ENV{"HETZNER_ROBOT_PASSWORD"}
MC/DC0%0/2
$self->{"password"} // $ENV{"HETZNER_ROBOT_PASSWORD"}
$self->{"password"}
$ENV{"HETZNER_ROBOT_PASSWORD"}
Truth table33%1/3
A // B
A= $self->{"password"}B= $ENV{"HETZNER_ROBOT_PASSWORD"}
| |||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | sub _check_auth { | ||||||||||||||||||||||||||
|
Subroutine
_check_auth: called CC=3 SCAR=12.5
| |||||||||||||||||||||||||||||
| 39 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 40 | 1 | ▶ | unless ($self->user && $self->password) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->user and $self->password
MC/DC0%0/2
$self->user and $self->password
$self->user
$self->password
Truth table33%1/3
A and B
A= $self->userB= $self->password
| |||||||||||||||||||||||||||||
| 41 | 1 | croak "No Robot credentials configured.\n\n" | |||||||||||||||||||||||||||
| 42 | . "Set credentials via:\n" | ||||||||||||||||||||||||||||
| 43 | . " Environment: HETZNER_ROBOT_USER and HETZNER_ROBOT_PASSWORD\n" | ||||||||||||||||||||||||||||
| 44 | . " Options: user => \$user, password => \$password\n\n" | ||||||||||||||||||||||||||||
| 45 | . "Get credentials at: https://robot.hetzner.com/preferences/index\n"; | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 7 | ▶ | sub base_url { $_[0]->{base_url} // 'https://robot-ws.your-server.de' } | ||||||||||||||||||||||||||
|
Subroutine
base_url: called CC=2 SCAR=7.6
Pod
documented
Condition50%1/2
$_[0]{"base_url"} // "https://robot-ws.your-server.de"
MC/DC0%0/1
$_[0]{"base_url"} // "https://robot-ws.your-server.de"
$_[0]{"base_url"}
Truth table50%1/2
A
A= $_[0]{"base_url"}
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Internal: sync Robot instance for request building and response parsing | ||||||||||||||||||||||||||||
| 52 | 6 | ▶ | sub _robot { | ||||||||||||||||||||||||||
|
Subroutine
_robot: called CC=2 SCAR=8.1
| |||||||||||||||||||||||||||||
| 53 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||
| 54 | 6 | ▶ | $self->{_robot} //= WWW::Hetzner::Robot->new( | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"_robot"} //= "WWW::Hetzner::Robot"->new("user", $self->user, "password", $self->password, "base_url", $self->base_url)
MC/DC0%0/2
$self->{"_robot"} //= "WWW::Hetzner::Robot"->new("user", $self->user, "password", $self->password, "base_url", $self->base_url)
$self->{"_robot"}
"WWW::Hetzner::Robot"->new("user", $self->user, "password", $self->password, "base_url", $self->base_url)
Truth table33%1/3
A //= B
A= $self->{"_robot"}B= "WWW::Hetzner::Robot"->new("user", $self->user, "password", $self->password, "base_url", $self->base_url)
| |||||||||||||||||||||||||||||
| 55 | user => $self->user, | ||||||||||||||||||||||||||||
| 56 | password => $self->password, | ||||||||||||||||||||||||||||
| 57 | base_url => $self->base_url, | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Internal: Net::Async::HTTP instance | ||||||||||||||||||||||||||||
| 62 | 10 | ▶ | sub _http { | ||||||||||||||||||||||||||
|
Subroutine
_http: called CC=2 SCAR=8.4
| |||||||||||||||||||||||||||||
| 63 | 10 | my ($self) = @_; | |||||||||||||||||||||||||||
| 64 | 10 | ▶ | unless ($self->{_http}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | require Net::Async::HTTP; | |||||||||||||||||||||||||||
| 66 | 0 | $self->{_http} = Net::Async::HTTP->new( | |||||||||||||||||||||||||||
| 67 | user_agent => 'Net-Async-Hetzner/' . $VERSION, | ||||||||||||||||||||||||||||
| 68 | max_connections_per_host => 0, | ||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 10 | return $self->{_http}; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 5 | ▶ | sub _add_to_loop { | ||||||||||||||||||||||||||
|
Subroutine
_add_to_loop: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 5 | my ($self, $loop) = @_; | |||||||||||||||||||||||||||
| 76 | 5 | $self->add_child($self->_http); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # ============================================================================ | ||||||||||||||||||||||||||||
| 80 | # ASYNC HTTP TRANSPORT | ||||||||||||||||||||||||||||
| 81 | # ============================================================================ | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 5 | ▶ | sub _do_request { | ||||||||||||||||||||||||||
|
Subroutine
_do_request: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 84 | 5 | my ($self, $req) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 5 | my $uri = URI->new($req->url); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 5 | my $http_req = HTTP::Request->new($req->method => $uri); | |||||||||||||||||||||||||||
| 89 | 5 | my $headers = $req->headers; | |||||||||||||||||||||||||||
| 90 | 5 | for my $key (keys %$headers) { | |||||||||||||||||||||||||||
| 91 | 10 | $http_req->header($key => $headers->{$key}); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 5 | ▶ | $http_req->content($req->content) if $req->has_content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | return $self->_http->do_request( | ||||||||||||||||||||||||||||
| 96 | request => $http_req, | ||||||||||||||||||||||||||||
| 97 | 5 | ▶ | )->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=13.1
| |||||||||||||||||||||||||||||
| 98 | 5 | my ($response) = @_; | |||||||||||||||||||||||||||
| 99 | 5 | ▶ | return Future->done(WWW::Hetzner::HTTPResponse->new( | ||||||||||||||||||||||||||
|
Condition40%2/5
($response->decoded_content // $response->content) // ""
$response->decoded_content // $response->content
MC/DC0%0/2
($response->decoded_content // $response->content) // ""
$response->decoded_content
$response->content
Truth table33%1/3
A // B
A= $response->decoded_contentB= $response->content
| |||||||||||||||||||||||||||||
| 100 | status => $response->code, | ||||||||||||||||||||||||||||
| 101 | content => $response->decoded_content // $response->content // '', | ||||||||||||||||||||||||||||
| 102 | )); | ||||||||||||||||||||||||||||
| 103 | 5 | }); | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # ============================================================================ | ||||||||||||||||||||||||||||
| 107 | # ASYNC API METHODS - all return Futures | ||||||||||||||||||||||||||||
| 108 | # ============================================================================ | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 2 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 2 | my ($self, $path, %params) = @_; | |||||||||||||||||||||||||||
| 112 | 2 | my $robot = $self->_robot; | |||||||||||||||||||||||||||
| 113 | 2 | my $req = $robot->_build_request('GET', $path, %params); | |||||||||||||||||||||||||||
| 114 | 2 | ▶ | return $self->_do_request($req)->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 115 | 2 | my ($response) = @_; | |||||||||||||||||||||||||||
| 116 | 2 | return Future->done($robot->_parse_response($response, 'GET', $path)); | |||||||||||||||||||||||||||
| 117 | 2 | }); | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | ▶ | sub post { | ||||||||||||||||||||||||||
|
Subroutine
post: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 1 | my ($self, $path, $data) = @_; | |||||||||||||||||||||||||||
| 122 | 1 | my $robot = $self->_robot; | |||||||||||||||||||||||||||
| 123 | 1 | my $req = $robot->_build_request('POST', $path, body => $data); | |||||||||||||||||||||||||||
| 124 | 1 | ▶ | return $self->_do_request($req)->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | 1 | my ($response) = @_; | |||||||||||||||||||||||||||
| 126 | 1 | return Future->done($robot->_parse_response($response, 'POST', $path)); | |||||||||||||||||||||||||||
| 127 | 1 | }); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | ▶ | sub put { | ||||||||||||||||||||||||||
|
Subroutine
put: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 131 | 1 | my ($self, $path, $data) = @_; | |||||||||||||||||||||||||||
| 132 | 1 | my $robot = $self->_robot; | |||||||||||||||||||||||||||
| 133 | 1 | my $req = $robot->_build_request('PUT', $path, body => $data); | |||||||||||||||||||||||||||
| 134 | 1 | ▶ | return $self->_do_request($req)->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | 1 | my ($response) = @_; | |||||||||||||||||||||||||||
| 136 | 1 | return Future->done($robot->_parse_response($response, 'PUT', $path)); | |||||||||||||||||||||||||||
| 137 | 1 | }); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 1 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 141 | 1 | my ($self, $path) = @_; | |||||||||||||||||||||||||||
| 142 | 1 | my $robot = $self->_robot; | |||||||||||||||||||||||||||
| 143 | 1 | my $req = $robot->_build_request('DELETE', $path); | |||||||||||||||||||||||||||
| 144 | 1 | ▶ | return $self->_do_request($req)->then(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 145 | 1 | my ($response) = @_; | |||||||||||||||||||||||||||
| 146 | 1 | return Future->done($robot->_parse_response($response, 'DELETE', $path)); | |||||||||||||||||||||||||||
| 147 | 1 | }); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 2 | 1; | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | __END__ | ||||||||||||||||||||||||||||