line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package VM::HetznerCloud::APIBase; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: Base class for all entity classes |
4
|
|
|
|
|
|
|
|
5
|
4
|
|
|
4
|
|
2413
|
use v5.24; |
|
4
|
|
|
|
|
51
|
|
6
|
|
|
|
|
|
|
|
7
|
4
|
|
|
4
|
|
31
|
use Carp; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
244
|
|
8
|
4
|
|
|
4
|
|
26
|
use Moo; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
25
|
|
9
|
4
|
|
|
4
|
|
1911
|
use Mojo::UserAgent; |
|
4
|
|
|
|
|
270337
|
|
|
4
|
|
|
|
|
39
|
|
10
|
4
|
|
|
4
|
|
206
|
use Mojo::Util qw(url_escape); |
|
4
|
|
|
|
|
11
|
|
|
4
|
|
|
|
|
262
|
|
11
|
4
|
|
|
4
|
|
547
|
use Types::Mojo qw(:all); |
|
4
|
|
|
|
|
49237
|
|
|
4
|
|
|
|
|
36
|
|
12
|
4
|
|
|
4
|
|
14255
|
use Types::Standard qw(Str); |
|
4
|
|
|
|
|
14
|
|
|
4
|
|
|
|
|
24
|
|
13
|
|
|
|
|
|
|
|
14
|
4
|
|
|
4
|
|
39581
|
use VM::HetznerCloud::Schema; |
|
4
|
|
|
|
|
42
|
|
|
4
|
|
|
|
|
238
|
|
15
|
|
|
|
|
|
|
|
16
|
4
|
|
|
4
|
|
37
|
use Mojo::Base -strict, -signatures; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
29
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.0.3'; # VERSION |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has token => ( is => 'ro', isa => Str, required => 1 ); |
21
|
|
|
|
|
|
|
has host => ( is => 'ro', isa => MojoURL["https?"], default => sub { 'https://api.hetzner.cloud' }, coerce => 1 ); |
22
|
|
|
|
|
|
|
has base_uri => ( is => 'ro', isa => Str, default => sub { 'v1' } ); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
has client => ( |
25
|
|
|
|
|
|
|
is => 'ro', |
26
|
|
|
|
|
|
|
lazy => 1, |
27
|
|
|
|
|
|
|
isa => MojoUserAgent, |
28
|
|
|
|
|
|
|
default => sub { |
29
|
|
|
|
|
|
|
Mojo::UserAgent->new, |
30
|
|
|
|
|
|
|
} |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
7
|
|
|
7
|
|
15
|
sub _request ( $self, $partial_uri, $params = {}, $param_def = {}, $opts = {} ) { |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
14
|
|
|
7
|
|
|
|
|
13
|
|
|
7
|
|
|
|
|
15
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my ($validated_params, @errors) = VM::HetznerCloud::Schema->validate( |
36
|
|
|
|
|
|
|
$opts->{oid}, |
37
|
7
|
|
|
|
|
54
|
$params, |
38
|
|
|
|
|
|
|
); |
39
|
|
|
|
|
|
|
|
40
|
7
|
|
50
|
|
|
20
|
$validated_params ||= {}; |
41
|
|
|
|
|
|
|
|
42
|
7
|
50
|
|
|
|
18
|
if ( @errors ) { |
43
|
0
|
|
|
|
|
0
|
croak 'Invalid parameters'; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
7
|
|
50
|
|
|
29
|
my $method = delete $opts->{type} // 'get'; |
47
|
7
|
|
|
|
|
244
|
my $sub = $self->client->can(lc $method); |
48
|
|
|
|
|
|
|
|
49
|
7
|
50
|
|
|
|
129
|
if ( !$sub ) { |
50
|
0
|
|
|
|
|
0
|
croak sprintf 'Invalid request method %s', $method; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
7
|
|
100
|
|
|
38
|
$partial_uri ||= ''; |
54
|
4
|
|
100
|
4
|
|
4910
|
$partial_uri =~ s{:(?\w+)\b}{ ( delete $validated_params->{path}->{$+{mandatory}} ) // '' }xmsge; |
|
4
|
|
|
|
|
1617
|
|
|
4
|
|
|
|
|
1625
|
|
|
7
|
|
|
|
|
25
|
|
|
3
|
|
|
|
|
44
|
|
55
|
7
|
|
|
|
|
28
|
$partial_uri =~ s{\A/}{}; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $query = join '&', map{ |
58
|
2
|
|
|
|
|
13
|
$_ . '=' . url_escape( delete $validated_params->{query}->{$_} ) |
59
|
|
|
|
|
|
|
} grep { |
60
|
7
|
100
|
|
|
|
55
|
$param_def->{$_}->{in} eq 'query' && $validated_params->{query}->{$_} |
|
19
|
|
|
|
|
81
|
|
61
|
|
|
|
|
|
|
}sort keys $param_def->%*; |
62
|
|
|
|
|
|
|
|
63
|
7
|
|
|
|
|
34
|
my %request_opts; |
64
|
7
|
100
|
|
|
|
22
|
if ( $params->%* ) { |
65
|
6
|
|
|
|
|
21
|
%request_opts = ( json => $validated_params->{body} ); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
7
|
100
|
|
|
|
86
|
my $uri = join '/', |
69
|
|
|
|
|
|
|
$self->host, |
70
|
|
|
|
|
|
|
$self->base_uri, |
71
|
|
|
|
|
|
|
$self->endpoint, |
72
|
|
|
|
|
|
|
( $partial_uri ? $partial_uri : () ); |
73
|
|
|
|
|
|
|
|
74
|
7
|
100
|
|
|
|
2575
|
$uri .= '?' . $query if $query; |
75
|
|
|
|
|
|
|
|
76
|
7
|
|
|
|
|
148
|
my $tx = $self->client->$method( |
77
|
|
|
|
|
|
|
$uri, |
78
|
|
|
|
|
|
|
{ |
79
|
|
|
|
|
|
|
Authorization => 'Bearer ' . $self->token, |
80
|
|
|
|
|
|
|
}, |
81
|
|
|
|
|
|
|
%request_opts, |
82
|
|
|
|
|
|
|
); |
83
|
|
|
|
|
|
|
|
84
|
7
|
|
|
|
|
10025
|
my $response = $tx->res; |
85
|
|
|
|
|
|
|
|
86
|
7
|
50
|
|
|
|
51
|
say STDERR $tx->req->to_string if $ENV{VM_HETZNERCLOUD_DEBUG}; |
87
|
7
|
50
|
|
|
|
19
|
say STDERR $tx->res->to_string if $ENV{VM_HETZNERCLOUD_DEBUG}; |
88
|
|
|
|
|
|
|
|
89
|
7
|
100
|
|
|
|
31
|
return if $response->is_error; |
90
|
6
|
|
|
|
|
140
|
return $response->json; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |