Hetzner/Cloud.pm

filter: Statementstmt 97.4% 77 / 79 Branchbran 87.5% 7 / 8 Conditioncond 66.6% 10 / 15 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 25 / 25 Podpod 100% 7 / 7 totaltotal 90.7% 128 / 141 CC 12 SCAR 6.5
CC
12
Cov
92%
__ANON__
3.7
_do_request
3.0
token
3.0
CRAP 1.9
« Hetzner/Robot.pm ↑ Summary Hetzner.pm »
1 package Net::Async::Hetzner::Cloud;
2  
3 # ABSTRACT: Async Hetzner Cloud 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::Cloud;
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 11 ▶sub configure {
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
19 11 my ($self, %params) = @_;
20  
21 11 ▶ $self->{token} = delete $params{token} if exists $params{token};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"token"} 10 1 11
22 11 ▶ $self->{base_url} = delete $params{base_url} if exists $params{base_url};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{"base_url"} 1 10 11
23  
24 11 $self->SUPER::configure(%params);
25 }
26  
27 10 ▶sub token {
Subroutine token: called CC=3 SCAR=11.1
Pod documented
28 10 my ($self) = @_;
29 $self->{token} // $ENV{HETZNER_API_TOKEN}
30 10 ▶ // croak "No Cloud API token configured.\n\n"
Condition80%4/5
($self->{"token"} // $ENV{"HETZNER_API_TOKEN"}) // croak("No Cloud API token configured.\n\nSet token via:\n Environment: HETZNER_API_TOKEN\n Option: token => \$token\n\nGet token at: https://console.hetzner.cloud/ -> Select project -> Security -> API tokens\n")
l!l
91
$self->{"token"} // $ENV{"HETZNER_API_TOKEN"}
l!l&&r!l&&!r
901
MC/DC50%1/2
($self->{"token"} // $ENV{"HETZNER_API_TOKEN"}) // croak("No Cloud API token configured.\n\nSet token via:\n Environment: HETZNER_API_TOKEN\n Option: token => \$token\n\nGet token at: https://console.hetzner.cloud/ -> Select project -> Security -> API tokens\n")
$self->{"token"} $ENV{"HETZNER_API_TOKEN"}
Truth table66%2/3
A // B
A= $self->{"token"}B= $ENV{"HETZNER_API_TOKEN"}
ABresult
1X1
011
000
31 . "Set token via:\n"
32 . " Environment: HETZNER_API_TOKEN\n"
33 . " Option: token => \$token\n\n"
34 . "Get token at: https://console.hetzner.cloud/ -> Select project -> Security -> API tokens\n";
35 }
36  
37 10 ▶sub base_url { $_[0]->{base_url} // 'https://api.hetzner.cloud/v1' }
Subroutine base_url: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[0]{"base_url"} // "https://api.hetzner.cloud/v1"
l!l
19
MC/DC100%1/1
$_[0]{"base_url"} // "https://api.hetzner.cloud/v1"
$_[0]{"base_url"}
Truth table100%2/2
A
A= $_[0]{"base_url"}
Aresult
11
00
38  
39 # Internal: sync Cloud instance for request building and response parsing
40 9 ▶sub _cloud {
Subroutine _cloud: called CC=2 SCAR=7.1
41 9 my ($self) = @_;
42 9 ▶ $self->{_cloud} //= WWW::Hetzner::Cloud->new(
Condition66%2/3
$self->{"_cloud"} //= "WWW::Hetzner::Cloud"->new("token", $self->token, "base_url", $self->base_url)
l!l&&r!l&&!r
180
MC/DC0%0/2
$self->{"_cloud"} //= "WWW::Hetzner::Cloud"->new("token", $self->token, "base_url", $self->base_url)
$self->{"_cloud"} "WWW::Hetzner::Cloud"->new("token", $self->token, "base_url", $self->base_url)
Truth table66%2/3
A //= B
A= $self->{"_cloud"}B= "WWW::Hetzner::Cloud"->new("token", $self->token, "base_url", $self->base_url)
ABresult
1X1
011
000
43 token => $self->token,
44 base_url => $self->base_url,
45 );
46 }
47  
48 # Internal: Net::Async::HTTP instance
49 15 ▶sub _http {
Subroutine _http: called CC=2 SCAR=8.4
50 15 my ($self) = @_;
51 15 ▶ unless ($self->{_http}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_http"}) 0 15 15
52 0 require Net::Async::HTTP;
53 0 $self->{_http} = Net::Async::HTTP->new(
54 user_agent => 'Net-Async-Hetzner/' . $VERSION,
55 max_connections_per_host => 0,
56 );
57 }
58 15 return $self->{_http};
59 }
60  
61 7 ▶sub _add_to_loop {
Subroutine _add_to_loop: called CC=1 SCAR=0.0
62 7 my ($self, $loop) = @_;
63 7 $self->add_child($self->_http);
64 }
65  
66 # ============================================================================
67 # ASYNC HTTP TRANSPORT
68 # ============================================================================
69  
70 8 ▶sub _do_request {
Subroutine _do_request: called CC=3 SCAR=11.1
71 8 my ($self, $req) = @_;
72  
73 8 my $uri = URI->new($req->url);
74  
75 8 my $http_req = HTTP::Request->new($req->method => $uri);
76 8 my $headers = $req->headers;
77 8 for my $key (keys %$headers) {
78 16 $http_req->header($key => $headers->{$key});
79 }
80 8 ▶ $http_req->content($req->content) if $req->has_content;
Branch: 1 branch
BranchTrueFalseTotal
if $req->has_content 2 6 8
81  
82 return $self->_http->do_request(
83 request => $http_req,
84 8 ▶ )->then(sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
85 8 my ($response) = @_;
86 8 ▶ return Future->done(WWW::Hetzner::HTTPResponse->new(
Condition40%2/5
($response->decoded_content // $response->content) // ""
l!l
80
$response->decoded_content // $response->content
l!l&&r!l&&!r
800
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
ABresult
1X1
011
000
87 status => $response->code,
88 content => $response->decoded_content // $response->content // '',
89 ));
90 8 });
91 }
92  
93 # ============================================================================
94 # ASYNC API METHODS - all return Futures
95 # ============================================================================
96  
97 5 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
98 5 my ($self, $path, %params) = @_;
99 5 my $cloud = $self->_cloud;
100 5 my $req = $cloud->_build_request('GET', $path, %params);
101 5 ▶ return $self->_do_request($req)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
102 5 my ($response) = @_;
103 5 return Future->done($cloud->_parse_response($response, 'GET', $path));
104 5 });
105 }
106  
107 1 ▶sub post {
Subroutine post: called CC=1 SCAR=0.0
Pod documented
108 1 my ($self, $path, $data) = @_;
109 1 my $cloud = $self->_cloud;
110 1 my $req = $cloud->_build_request('POST', $path, body => $data);
111 1 ▶ return $self->_do_request($req)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
112 1 my ($response) = @_;
113 1 return Future->done($cloud->_parse_response($response, 'POST', $path));
114 1 });
115 }
116  
117 1 ▶sub put {
Subroutine put: called CC=1 SCAR=0.0
Pod documented
118 1 my ($self, $path, $data) = @_;
119 1 my $cloud = $self->_cloud;
120 1 my $req = $cloud->_build_request('PUT', $path, body => $data);
121 1 ▶ return $self->_do_request($req)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
122 1 my ($response) = @_;
123 1 return Future->done($cloud->_parse_response($response, 'PUT', $path));
124 1 });
125 }
126  
127 1 ▶sub delete {
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
128 1 my ($self, $path) = @_;
129 1 my $cloud = $self->_cloud;
130 1 my $req = $cloud->_build_request('DELETE', $path);
131 1 ▶ return $self->_do_request($req)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
132 1 my ($response) = @_;
133 1 return Future->done($cloud->_parse_response($response, 'DELETE', $path));
134 1 });
135 }
136  
137 2 1;
138  
139 __END__
« Hetzner/Robot.pm ↑ Summary Hetzner.pm »