blib/lib/Linode/API.pm

filter: Statementstmt 25.0% 34 / 136 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 45.8% 11 / 24 Podpod 100% 1 / 1 totaltotal 17.1% 46 / 268 CC 45 SCAR 38.1
CC
45
Cov
16%
_fix_api_version
90.0
_fix_closed_all_of
90.0
_fix_json_headers
72.0
CRAP 45.2
↑ Summary
1 package Linode::API;
2 1 $Linode::API::VERSION = '0.003';
3 #ABSTRACT: A client for the Linode API, generated from Linode's own OpenAPI specification
4  
5 1 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use parent qw{OpenAPI::Client};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Carp qw{croak};
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Cpanel::JSON::XS qw{decode_json};
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Data::Validate::IP();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::ShareDir qw{dist_file};
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use File::Slurper qw{read_binary};
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Mojo::Util qw{monkey_patch};
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
23 0 my ( $class, %options ) = @_;
24  
25 0 my $token = delete $options{token};
26 0 ▶ my $api_version = delete $options{api_version} // 'v4';
Condition0%0/2
delete $options{"api_version"} // "v4"
l!l
00
MC/DC0%0/1
delete $options{"api_version"} // "v4"
delete $options{"api_version"}
Truth table0%0/2
A
A= delete $options{"api_version"}
Aresult
11
00
27 0 ▶ my $specification = delete $options{specification} // dist_file( 'Linode-API', 'openapi.json' );
Condition0%0/3
delete $options{"specification"} // dist_file("Linode-API", "openapi.json")
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $options{"specification"} // dist_file("Linode-API", "openapi.json")
delete $options{"specification"} dist_file("Linode-API", "openapi.json")
Truth table0%0/3
A // B
A= delete $options{"specification"}B= dist_file("Linode-API", "openapi.json")
ABresult
1X1
011
000
28  
29 0 my $spec = _specification( $specification, $api_version );
30 0 my $self = $class->SUPER::new( $spec, %options );
31 0 _alias_operations( ref $self, $self->validator->routes );
32 0 _add_formats( $self->validator->formats, $spec );
33 0 $self->on( after_build_tx => \&_unescape_slashes );
34 0 ▶ if ( defined $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token) 0 0 0
35 0 ▶ $self->on( after_build_tx => sub { $_[1]->req->headers->authorization("Bearer $token") } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 }
37  
38 0 return $self;
39 }
40  
41 # An id Linode hands out can hold a slash, as private/123 does, and Linode wants
42 # it in the path as a slash, where OpenAPI::Client escapes it within one segment.
43 0 ▶sub _unescape_slashes {
Subroutine _unescape_slashes: not called CC=1 SCAR=6.9
44 0 my ( undef, $tx ) = @_;
45  
46 0 my $path = $tx->req->url->path;
47 0 $path->parts( [ map { split q{/}, $_ } @{ $path->parts } ] );
48 0 return;
49 }
50  
51 # OpenAPI::Client names the class it generates after the hashref's address, so
52 # handing it the same hashref every time is what stops each client leaking a class.
53 0 ▶sub _specification {
Subroutine _specification: not called CC=2 SCAR=17.9
54 0 my ( $file, $api_version ) = @_;
55  
56 0 state %specifications;
57 0 ▶ return $specifications{"$api_version\0$file"} //= do {
Condition0%0/3
$specifications{"$api_version\000$file"} //= do { my $spec = &decode_json(scalar read_binary($file)); _fix_api_version($spec, $api_version); _fix_json_headers($spec); _fix_closed_all_of($spec); $spec }
l!l&&r!l&&!r
000
MC/DC0%0/2
$specifications{"$api_version\000$file"} //= do { my $spec = &decode_json(scalar read_binary($file)); _fix_api_version($spec, $api_version); _fix_json_headers($spec); _fix_closed_all_of($spec); $spec }
$specifications{"$api_version\000$file"} do { my $spec = &decode_json(scalar read_binary($file)); _fix_api_version($spec, $api_version); _fix_json_headers($spec); _fix_closed_all_of($spec); $spec }
Truth table0%0/3
A //= B
A= $specifications{"$api_version\000$file"}B= do { my $spec = &decode_json(scalar read_binary($file)); _fix_api_version($spec, $api_version); _fix_json_headers($spec); _fix_closed_all_of($spec); $spec }
ABresult
1X1
011
000
58 0 my $spec = decode_json( read_binary($file) );
59 0 _fix_api_version( $spec, $api_version );
60 0 _fix_json_headers($spec);
61 0 _fix_closed_all_of($spec);
62 0 $spec;
63 };
64 }
65  
66 # Write the version into each path, because OpenAPI::Client fills a path in
67 # before it validates, so a schema default for {apiVersion} never reaches the URL.
68 # A path that does not offer the version asked for gets the first one it does.
69 0 ▶sub _fix_api_version {
Subroutine _fix_api_version: not called CC=9 SCAR=45.0
70 0 my ( $spec, $api_version ) = @_;
71  
72 0 my ( $paths, %offered ) = ( $spec->{paths} );
73 0 foreach my $path ( keys %$paths ) {
74 0 my $item = delete $paths->{$path};
75  
76 0 ▶ my ($parameter) = grep { ( $_->{name} // q{} ) eq 'apiVersion' } @{ $item->{parameters} // [] };
Condition0%0/4
$_->{"name"} // ""
l!l
00
$item->{"parameters"} // []
l!l
00
MC/DC0%0/1
$_->{"name"} // ""
$_->{"name"}
MC/DC0%0/1
$item->{"parameters"} // []
$item->{"parameters"}
Truth table0%0/2
A
A= $_->{"name"}
Aresult
11
00
Truth table0%0/2
A
A= $item->{"parameters"}
Aresult
11
00
77 0 ▶ my @versions = @{ $parameter->{schema}{enum} // [] };
Condition0%0/2
$parameter->{"schema"}{"enum"} // []
l!l
00
MC/DC0%0/1
$parameter->{"schema"}{"enum"} // []
$parameter->{"schema"}{"enum"}
Truth table0%0/2
A
A= $parameter->{"schema"}{"enum"}
Aresult
11
00
78 0 $offered{$_} = 1 for @versions;
79  
80 0 ▶ if (@versions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@versions) 0 0 0
81 0 ▶ my $version = ( grep { $_ eq $api_version } @versions ) ? $api_version : $versions[0];
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $api_version;} @versions) ? : 0 0 0
82 0 $item->{parameters} = [ grep { $_ != $parameter } @{ $item->{parameters} } ];
83 0 $path =~ s{\{apiVersion\}}{$version}g;
84 }
85 0 $paths->{$path} = $item;
86 }
87  
88 0 ▶ croak "api_version '$api_version' is not in the specification, which offers [" . join( ', ', sort keys %offered ) . ']' unless $offered{$api_version};
Branch: 1 branch
BranchTrueFalseTotal
unless $offered{$api_version} 0 0 0
89  
90 0 return $spec;
91 }
92  
93 # The specification describes X-Filter as the object its JSON encodes, and
94 # OpenAPI::Client would send that object as HASH(0x...), so a header is a string.
95 0 ▶sub _fix_json_headers {
Subroutine _fix_json_headers: not called CC=8 SCAR=42.8
96 0 my ($spec) = @_;
97  
98 0 foreach my $item ( values %{ $spec->{paths} } ) {
99 0 my @operations = grep { ref $_ eq 'HASH' } values %$item;
100 0 ▶ foreach my $parameter ( map { @{ $_->{parameters} // [] } } $item, @operations ) {
Condition0%0/2
$_->{"parameters"} // []
l!l
00
MC/DC0%0/1
$_->{"parameters"} // []
$_->{"parameters"}
Truth table0%0/2
A
A= $_->{"parameters"}
Aresult
11
00
101 0 ▶ next unless ( $parameter->{in} // q{} ) eq 'header';
Branch: 1 branch
BranchTrueFalseTotal
unless ($parameter->{"in"} // "") eq "header" 0 0 0
Condition0%0/2
$parameter->{"in"} // ""
l!l
00
MC/DC0%0/1
$parameter->{"in"} // ""
$parameter->{"in"}
Truth table0%0/2
A
A= $parameter->{"in"}
Aresult
11
00
102 0 ▶ next if ( $parameter->{schema}{type} // q{} ) eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if ($parameter->{"schema"}{"type"} // "") eq "string" 0 0 0
Condition0%0/2
$parameter->{"schema"}{"type"} // ""
l!l
00
MC/DC0%0/1
$parameter->{"schema"}{"type"} // ""
$parameter->{"schema"}{"type"}
Truth table0%0/2
A
A= $parameter->{"schema"}{"type"}
Aresult
11
00
103 0 $parameter->{schema} = { type => 'string', description => 'JSON' };
104 }
105 }
106  
107 0 return $spec;
108 }
109  
110 # A member of an allOf that says additionalProperties: false forbids what its
111 # siblings declare, so Linode's check of the body is the one that has to do.
112 0 ▶sub _fix_closed_all_of {
Subroutine _fix_closed_all_of: not called CC=9 SCAR=45.0
113 0 my ($node) = @_;
114  
115 0 ▶ if ( ref $node eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $node eq 'ARRAY') { } 0 0 0
elsif (ref $node eq 'HASH') { } 0 0 0
116 0 _fix_closed_all_of($_) foreach @$node;
117 }
118 elsif ( ref $node eq 'HASH' ) {
119 0 ▶ if ( ref $node->{allOf} eq 'ARRAY' && @{ $node->{allOf} } > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node->{"allOf"} eq "ARRAY" and @{$node->{"allOf"};} > 1) 0 0 0
Condition0%0/3
ref $node->{"allOf"} eq "ARRAY" and @{$node->{"allOf"};} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
ref $node->{"allOf"} eq "ARRAY" and @{$node->{"allOf"};} > 1
ref $node->{"allOf"} eq "ARRAY" @{$node->{"allOf"};} > 1
Truth table0%0/3
A and B
A= ref $node->{"allOf"} eq "ARRAY"B= @{$node->{"allOf"};} > 1
ABresult
0X0
100
111
120 0 ▶ foreach my $member ( grep { ref $_ eq 'HASH' && _is_closed($_) } @{ $node->{allOf} } ) {
Condition0%0/3
ref $_ eq 'HASH' && _is_closed($_)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ eq 'HASH' && _is_closed($_)
ref $_ eq 'HASH' _is_closed($_)
Truth table0%0/3
A && B
A= ref $_ eq 'HASH'B= _is_closed($_)
ABresult
0X0
100
111
121 0 delete $member->{additionalProperties};
122 }
123 }
124 0 _fix_closed_all_of($_) foreach values %$node;
125 }
126  
127 0 return $node;
128 }
129  
130 # A JSON false is an object, so ask it what it is rather than whether it is a ref.
131 0 ▶sub _is_closed {
Subroutine _is_closed: not called CC=3 SCAR=24.8
132 0 my ($schema) = @_;
133 0 my $additional = $schema->{additionalProperties};
134 0 ▶ return defined $additional && ref $additional ne 'HASH' && !$additional;
Condition0%0/6
defined $additional && ref $additional ne 'HASH' && !$additional
!ll&&!rl&&r
000
defined $additional and ref $additional ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $additional && ref $additional ne 'HASH' && !$additional
defined $additional ref $additional ne "HASH" !$additional
Truth table0%0/4
A and B && not(C)
A= defined $additionalB= ref $additional ne "HASH"C= !$additional
ABCresult
0XX0
10X0
1100
1111
135 }
136  
137 # JSON::Validator warns about a format that it has no rule for, on every value
138 # of it, and lets the value through. Linode names formats of its own.
139 0 ▶sub _add_formats {
Subroutine _add_formats: not called CC=4 SCAR=30.0
140 0 my ( $formats, $spec ) = @_;
141  
142 0 ▶ $formats->{ip} //= sub { return Data::Validate::IP::is_ip( $_[0] ) ? undef : 'Does not match ip format.' };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
Data::Validate::IP::is_ip($_[0]) ? : 0 0 0
Condition0%0/3
$formats->{"ip"} //= sub { return Data::Validate::IP::is_ip($_[0]) ? undef : "Does not match ip format."; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$formats->{"ip"} //= sub { return Data::Validate::IP::is_ip($_[0]) ? undef : "Does not match ip format."; }
$formats->{"ip"} sub { return Data::Validate::IP::is_ip($_[0]) ? undef : "Does not match ip format."; }
Truth table0%0/3
A //= B
A= $formats->{"ip"}B= sub { return Data::Validate::IP::is_ip($_[0]) ? undef : "Does not match ip format."; }
ABresult
1X1
011
000
143 0 ▶ $formats->{$_} //= sub { return }
Subroutine __ANON__: not called CC=1 SCAR=6.9
144 0 ▶ foreach _formats_in($spec);
Condition0%0/3
$formats->{$_} //= sub { return; }
l!l&&r!l&&!r
000
MC/DC0%0/2
$formats->{$_} //= sub { return; }
$formats->{$_} sub { return; }
Truth table0%0/3
A //= B
A= $formats->{$_}B= sub { return; }
ABresult
1X1
011
000
145  
146 0 return $formats;
147 }
148  
149 # Every format that the specification names, which does not change while it is loaded.
150 0 ▶sub _formats_in {
Subroutine _formats_in: not called CC=7 SCAR=40.3
151 0 my ($spec) = @_;
152  
153 0 state %found;
154 return @{
155 0 ▶ $found{$spec} //= do {
Condition0%0/3
$found{$spec} //= do { my(%formats, @nodes); @nodes = $spec; while (@nodes) { my $node = shift @nodes; if (ref $node eq "ARRAY") { push @nodes, @$node; } elsif (ref $node eq "HASH") { $formats{$node->{"format"}} = 1 if defined $node->{"format"} and not ref $node->{"format"}; push @nodes, values %$node; } ; } ; [sort(keys %formats)] }
l!l&&r!l&&!r
000
MC/DC0%0/2
$found{$spec} //= do { my(%formats, @nodes); @nodes = $spec; while (@nodes) { my $node = shift @nodes; if (ref $node eq "ARRAY") { push @nodes, @$node; } elsif (ref $node eq "HASH") { $formats{$node->{"format"}} = 1 if defined $node->{"format"} and not ref $node->{"format"}; push @nodes, values %$node; } ; } ; [sort(keys %formats)] }
$found{$spec} do { my(%formats, @nodes); @nodes = $spec; while (@nodes) { my $node = shift @nodes; if (ref $node eq "ARRAY") { push @nodes, @$node; } elsif (ref $node eq "HASH") { $formats{$node->{"format"}} = 1 if defined $node->{"format"} and not ref $node->{"format"}; push @nodes, values %$node; } ; } ; [sort(keys %formats)] }
Truth table0%0/3
A //= B
A= $found{$spec}B= do { my(%formats, @nodes); @nodes = $spec; while (@nodes) { my $node = shift @nodes; if (ref $node eq "ARRAY") { push @nodes, @$node; } elsif (ref $node eq "HASH") { $formats{$node->{"format"}} = 1 if defined $node->{"format"} and not ref $node->{"format"}; push @nodes, values %$node; } ; } ; [sort(keys %formats)] }
ABresult
1X1
011
000
156 0 my ( %formats, @nodes );
157 0 @nodes = ($spec);
158 0 ▶ while (@nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes) 0 0 0
159 0 my $node = shift @nodes;
160 0 ▶ if ( ref $node eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $node eq 'ARRAY') { } 0 0 0
elsif (ref $node eq 'HASH') { } 0 0 0
161 0 push @nodes, @$node;
162 }
163 elsif ( ref $node eq 'HASH' ) {
164 0 ▶ $formats{ $node->{format} } = 1 if defined $node->{format} && !ref $node->{format};
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->{"format"} and not ref $node->{"format"} 0 0 0
Condition0%0/3
defined $node->{"format"} and not ref $node->{"format"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $node->{"format"} and not ref $node->{"format"}
defined $node->{"format"} not ref $node->{"format"}
Truth table0%0/3
A and not(B)
A= defined $node->{"format"}B= not ref $node->{"format"}
ABresult
0X0
100
111
165 0 push @nodes, values %$node;
166 }
167 }
168 0 [ sort keys %formats ];
169 }
170 };
171 }
172  
173 # Give each kebab-case operation a snake_case name a caller can write as a
174 # bareword, never replacing a method the class already has.
175 0 ▶sub _alias_operations {
Subroutine _alias_operations: not called CC=6 SCAR=37.4
176 0 my ( $class, $routes ) = @_;
177  
178 0 state %aliased;
179 0 ▶ return if $aliased{$class}++;
Branch: 1 branch
BranchTrueFalseTotal
if $aliased{$class}++ 0 0 0
180  
181 0 ▶ foreach my $operation ( grep { defined && index( $_, q{-} ) >= 0 } map { $_->{operation_id} } $routes->each ) {
Condition0%0/3
defined $_ && index($_, '-') != -1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && index($_, '-') != -1
defined $_ index($_, '-') != -1
Truth table0%0/3
A && B
A= defined $_B= index($_, '-') != -1
ABresult
0X0
100
111
182 0 foreach my $method ( $operation, "${operation}_p" ) {
183 0 ( my $alias = $method ) =~ tr/-/_/;
184 0 ▶ monkey_patch( $class, $alias => $class->can($method) ) unless $class->can($alias);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($alias) 0 0 0
185 }
186 }
187  
188 0 return;
189 }
190  
191 1 1;
192  
193 __END__
↑ Summary