Hetzner/APIBase.pm

filter: Statementstmt 50.0% 33 / 66 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 71.4% 10 / 14 Podpod 100% 1 / 1 totaltotal 41.9% 44 / 105 CC 13 SCAR 42.9
CC
13
Cov
38%
request
156.0
_do
6.0
__ANON__
2.0
CRAP 73.0
↑ Summary Hetzner/Schema.pm »
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
BranchTrueFalseTotal
if @errors 0 0 0
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"
l!l
00
MC/DC0%0/1
delete $opts->{"type"} // "get"
delete $opts->{"type"}
Truth table0%0/2
A
A= delete $opts->{"type"}
Aresult
11
00
42 0 my $sub = $self->client->can(lc $method);
43  
44 0 ▶ croak sprintf 'Invalid request method %s', $method if !$sub;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 0 0 0
45  
46 0 ▶ $params->{path} //= {};
Condition0%0/2
$params->{"path"} //= {}
l!l
00
MC/DC0%0/1
$params->{"path"} //= {}
$params->{"path"}
Truth table0%0/2
A
A= $params->{"path"}
Aresult
11
00
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"} //= {}
l!l
00
MC/DC0%0/1
$params->{"body"} //= {}
$params->{"body"}
Truth table0%0/2
A
A= $params->{"body"}
Aresult
11
00
57 0 ▶ if ( $params->{body}->%* ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$params->{"body"}}) 0 0 0
58 0 %request_opts = ( json => $params->{body} );
59 }
60  
61 0 ▶ $params->{query} //= {};
Condition0%0/2
$params->{"query"} //= {}
l!l
00
MC/DC0%0/1
$params->{"query"} //= {}
$params->{"query"}
Truth table0%0/2
A
A= $params->{"query"}
Aresult
11
00
62 0 my $query = '';
63 0 ▶ if ( $params->{query}->%* ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%{$params->{"query"}}) 0 0 0
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
BranchTrueFalseTotal
if $query 0 0 0
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
BranchTrueFalseTotal
if ($tx->error) 0 0 0
92 0 carp $tx->error->{message};
93 0 return;
94 }
95  
96 0 return $response->json;
97 }
98  
99 2 1;
100  
101 __END__
↑ Summary Hetzner/Schema.pm »