| 1 | package DNS::Hetzner::APIBase; | ||||||||||||
| 2 | 2 | $DNS::Hetzner::APIBase::VERSION = '0.05'; | |||||||||||
| 3 | # ABSTRACT: Base class for all entity classes | ||||||||||||
| 4 | |||||||||||||
| 5 | 2 | ▶ | use v5.24; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 2 | ▶ | use Mojo::UserAgent; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 2 | ▶ | use Mojo::Util qw(url_escape); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 2 | ▶ | use Types::Mojo qw(:all); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 2 | ▶ | use Types::Standard qw(Str); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | 2 | ▶ | use DNS::Hetzner::Schema; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | |||||||||||||
| 16 | 2 | ▶ | use Mojo::Base -strict, -signatures; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | |||||||||||||
| 18 | 2 | has token => ( is => 'ro', isa => Str, required => 1 ); | |||||||||||
| 19 | 2 | ▶ | has host => ( is => 'ro', isa => MojoURL["https?"], default => sub { 'https://dns.hetzner.com' }, coerce => 1 ); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 20 | 0 | ▶ | has base_uri => ( is => 'ro', isa => Str, default => sub { 'api/v1' } ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 21 | |||||||||||||
| 22 | has client => ( | ||||||||||||
| 23 | is => 'ro', | ||||||||||||
| 24 | lazy => 1, | ||||||||||||
| 25 | isa => MojoUserAgent, | ||||||||||||
| 26 | 0 | ▶ | default => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 27 | 0 | Mojo::UserAgent->new, | |||||||||||
| 28 | } | ||||||||||||
| 29 | 2 | ); | |||||||||||
| 30 | |||||||||||||
| 31 | 0 | ▶ | sub _do ( $self, $op, $params, $path, $opts ) { | ||||||||||
|
Subroutine
_do: not called CC=2 SCAR=17.9
| |||||||||||||
| 32 | 0 | my ($req_params, @errors) = DNS::Hetzner::Schema->validate( $op, $params ); | |||||||||||
| 33 | |||||||||||||
| 34 | 0 | ▶ | croak 'invalid parameters' if @errors; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 35 | |||||||||||||
| 36 | 0 | $self->request( $path, $req_params, $opts ); | |||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | 0 | ▶ | sub request ( $self, $partial_uri, $params = {}, $opts = {} ) { | ||||||||||
|
Subroutine
request: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||
| 40 | |||||||||||||
| 41 | 0 | ▶ | my $method = delete $opts->{type} // 'get'; | ||||||||||
|
Condition0%0/2
delete $opts->{"type"} // "get"
MC/DC0%0/1
delete $opts->{"type"} // "get"
delete $opts->{"type"}
Truth table0%0/2
A
A= delete $opts->{"type"}
| |||||||||||||
| 42 | 0 | my $sub = $self->client->can(lc $method); | |||||||||||
| 43 | |||||||||||||
| 44 | 0 | ▶ | croak sprintf 'Invalid request method %s', $method if !$sub; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | |||||||||||||
| 46 | 0 | ▶ | $params->{path} //= {}; | ||||||||||
|
Condition0%0/2
$params->{"path"} //= {}
MC/DC0%0/1
$params->{"path"} //= {}
$params->{"path"}
Truth table0%0/2
A
A= $params->{"path"}
| |||||||||||||
| 47 | 0 | my %path_params = $params->{path}->%*; | |||||||||||
| 48 | |||||||||||||
| 49 | 0 | $partial_uri =~ s{ | |||||||||||
| 50 | :(?<mandatory>\w+)\b | ||||||||||||
| 51 | }{ | ||||||||||||
| 52 | $path_params{$+{mandatory}} | ||||||||||||
| 53 | 0 | }xmsge; | |||||||||||
| 54 | |||||||||||||
| 55 | 0 | my %request_opts; | |||||||||||
| 56 | 0 | ▶ | $params->{body} //= {}; | ||||||||||
|
Condition0%0/2
$params->{"body"} //= {}
MC/DC0%0/1
$params->{"body"} //= {}
$params->{"body"}
Truth table0%0/2
A
A= $params->{"body"}
| |||||||||||||
| 57 | 0 | ▶ | if ( $params->{body}->%* ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 58 | 0 | %request_opts = ( json => $params->{body} ); | |||||||||||
| 59 | } | ||||||||||||
| 60 | |||||||||||||
| 61 | 0 | ▶ | $params->{query} //= {}; | ||||||||||
|
Condition0%0/2
$params->{"query"} //= {}
MC/DC0%0/1
$params->{"query"} //= {}
$params->{"query"}
Truth table0%0/2
A
A= $params->{"query"}
| |||||||||||||
| 62 | 0 | my $query = ''; | |||||||||||
| 63 | 0 | ▶ | if ( $params->{query}->%* ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 0 | my $query_params = delete $params->{query}; | |||||||||||
| 65 | |||||||||||||
| 66 | $query = join '&', map{ | ||||||||||||
| 67 | 0 | $_ . '=' . url_escape($query_params->{$_}) | |||||||||||
| 68 | }sort keys $query_params->%*; | ||||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | 0 | my $uri = join '/', | |||||||||||
| 72 | $self->host, | ||||||||||||
| 73 | $self->base_uri, | ||||||||||||
| 74 | $self->endpoint, | ||||||||||||
| 75 | $partial_uri; | ||||||||||||
| 76 | |||||||||||||
| 77 | 0 | $uri =~ s{/\z}{}; | |||||||||||
| 78 | |||||||||||||
| 79 | 0 | ▶ | $uri .= '?' . $query if $query; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | |||||||||||||
| 81 | 0 | my $tx = $self->client->$method( | |||||||||||
| 82 | $uri, | ||||||||||||
| 83 | { | ||||||||||||
| 84 | 'Auth-API-Token' => $self->token, | ||||||||||||
| 85 | }, | ||||||||||||
| 86 | %request_opts, | ||||||||||||
| 87 | ); | ||||||||||||
| 88 | |||||||||||||
| 89 | 0 | my $response = $tx->res; | |||||||||||
| 90 | |||||||||||||
| 91 | 0 | ▶ | if ( $tx->error ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | 0 | carp $tx->error->{message}; | |||||||||||
| 93 | 0 | return; | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 0 | return $response->json; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | 2 | 1; | |||||||||||
| 100 | |||||||||||||
| 101 | __END__ | ||||||||||||