blib/lib/Linode/API.pm

filter: Statementstmt 26.2% 41 / 156 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 46.4% 13 / 28 Podpod 100% 2 / 2 totaltotal 18.2% 56 / 307 CC 54 SCAR 37.8
CC
54
Cov
16%
_fix_api_version
90.0
_fix_closed_all_of
90.0
_fix_json_headers
72.0
CRAP 43.8
↑ Summary
1 package Linode::API;
2 1 $Linode::API::VERSION = '0.004';
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 IO::Compress::Gzip qw{gzip $GzipError};
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use MIME::Base64 qw{encode_base64};
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Mojo::Util qw{monkey_patch};
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # The most user_data that Linode takes, in bytes after base64 decoding. Its
24 # specification says 65535, and the API refuses anything over this.
25 1 our $USER_DATA_MAX = 16384;
26  
27  
28 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
29 0 my ( $class, %options ) = @_;
30  
31 0 my $token = delete $options{token};
32 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
33 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
34  
35 0 my $spec = _specification( $specification, $api_version );
36 0 my $self = $class->SUPER::new( $spec, %options );
37 0 _alias_operations( ref $self, $self->validator->routes );
38 0 _add_formats( $self->validator->formats, $spec );
39 0 $self->on( after_build_tx => \&_unescape_slashes );
40 0 ▶ if ( defined $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token) 0 0 0
41 0 ▶ $self->on( after_build_tx => sub { $_[1]->req->headers->authorization("Bearer $token") } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 }
43  
44 0 return $self;
45 }
46  
47  
48 0 ▶sub encode_user_data {
Subroutine encode_user_data: not called CC=4 SCAR=30.0
Pod documented
49 0 my ( undef, $cloud_config ) = @_;
50  
51 0 ▶ croak 'encode_user_data needs the cloud-config to encode' unless defined $cloud_config;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cloud_config 0 0 0
52 0 ▶ gzip( \$cloud_config => \my $compressed, -Level => 9, Minimal => 1 ) or croak "Could not compress the user_data: $GzipError";
Branch: 1 branch
BranchTrueFalseTotal
gzip(\$cloud_config, \my $compressed, "-Level", 9, "Minimal", 1) or croak("Could not compress the user_data: $Linode::API::GzipError") 0 0 0
53 0 ▶ croak 'The user_data is ' . length($cloud_config) . ' bytes, and ' . length($compressed) . " bytes compressed. Linode takes at most $USER_DATA_MAX"
Branch: 1 branch
BranchTrueFalseTotal
if length $compressed > $Linode::API::USER_DATA_MAX 0 0 0
54 if length $compressed > $USER_DATA_MAX;
55  
56 0 return encode_base64( $compressed, q{} );
57 }
58  
59 # An id Linode hands out can hold a slash, as private/123 does, and Linode wants
60 # it in the path as a slash, where OpenAPI::Client escapes it within one segment.
61 0 ▶sub _unescape_slashes {
Subroutine _unescape_slashes: not called CC=1 SCAR=6.9
62 0 my ( undef, $tx ) = @_;
63  
64 0 my $path = $tx->req->url->path;
65 0 $path->parts( [ map { split q{/}, $_ } @{ $path->parts } ] );
66 0 return;
67 }
68  
69 # OpenAPI::Client names the class it generates after the hashref's address, so
70 # handing it the same hashref every time is what stops each client leaking a class.
71 0 ▶sub _specification {
Subroutine _specification: not called CC=2 SCAR=17.9
72 0 my ( $file, $api_version ) = @_;
73  
74 0 state %specifications;
75 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); _fix_user_data($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); _fix_user_data($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); _fix_user_data($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); _fix_user_data($spec); $spec }
ABresult
1X1
011
000
76 0 my $spec = decode_json( read_binary($file) );
77 0 _fix_api_version( $spec, $api_version );
78 0 _fix_json_headers($spec);
79 0 _fix_closed_all_of($spec);
80 0 _fix_user_data($spec);
81 0 $spec;
82 };
83 }
84  
85 # Write the version into each path, because OpenAPI::Client fills a path in
86 # before it validates, so a schema default for {apiVersion} never reaches the URL.
87 # A path that does not offer the version asked for gets the first one it does.
88 0 ▶sub _fix_api_version {
Subroutine _fix_api_version: not called CC=9 SCAR=45.0
89 0 my ( $spec, $api_version ) = @_;
90  
91 0 my ( $paths, %offered ) = ( $spec->{paths} );
92 0 foreach my $path ( keys %$paths ) {
93 0 my $item = delete $paths->{$path};
94  
95 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
96 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
97 0 $offered{$_} = 1 for @versions;
98  
99 0 ▶ if (@versions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@versions) 0 0 0
100 0 ▶ my $version = ( grep { $_ eq $api_version } @versions ) ? $api_version : $versions[0];
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $api_version;} @versions) ? : 0 0 0
101 0 $item->{parameters} = [ grep { $_ != $parameter } @{ $item->{parameters} } ];
102 0 $path =~ s{\{apiVersion\}}{$version}g;
103 }
104 0 $paths->{$path} = $item;
105 }
106  
107 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
108  
109 0 return $spec;
110 }
111  
112 # The specification describes X-Filter as the object its JSON encodes, and
113 # OpenAPI::Client would send that object as HASH(0x...), so a header is a string.
114 0 ▶sub _fix_json_headers {
Subroutine _fix_json_headers: not called CC=8 SCAR=42.8
115 0 my ($spec) = @_;
116  
117 0 foreach my $item ( values %{ $spec->{paths} } ) {
118 0 my @operations = grep { ref $_ eq 'HASH' } values %$item;
119 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
120 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
121 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
122 0 $parameter->{schema} = { type => 'string', description => 'JSON' };
123 }
124 }
125  
126 0 return $spec;
127 }
128  
129 # A member of an allOf that says additionalProperties: false forbids what its
130 # siblings declare, so Linode's check of the body is the one that has to do.
131 0 ▶sub _fix_closed_all_of {
Subroutine _fix_closed_all_of: not called CC=9 SCAR=45.0
132 0 my ($node) = @_;
133  
134 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
135 0 _fix_closed_all_of($_) foreach @$node;
136 }
137 elsif ( ref $node eq 'HASH' ) {
138 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
139 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
140 0 delete $member->{additionalProperties};
141 }
142 }
143 0 _fix_closed_all_of($_) foreach values %$node;
144 }
145  
146 0 return $node;
147 }
148  
149 # A JSON false is an object, so ask it what it is rather than whether it is a ref.
150 0 ▶sub _is_closed {
Subroutine _is_closed: not called CC=3 SCAR=24.8
151 0 my ($schema) = @_;
152 0 my $additional = $schema->{additionalProperties};
153 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
154 }
155  
156 # The base64 of $USER_DATA_MAX bytes is the longest user_data that Linode takes,
157 # whatever its specification says.
158 0 ▶sub _fix_user_data {
Subroutine _fix_user_data: not called CC=7 SCAR=40.3
159 0 my ($node) = @_;
160  
161 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
162 0 _fix_user_data($_) foreach @$node;
163 }
164 elsif ( ref $node eq 'HASH' ) {
165 0 ▶ my $user_data = ref $node->{properties} eq 'HASH' ? $node->{properties}{user_data} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $node->{'properties'} eq 'HASH' ? : 0 0 0
166 0 ▶ $user_data->{maxLength} = 4 * int( ( $USER_DATA_MAX + 2 ) / 3 ) if ref $user_data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $user_data eq "HASH" 0 0 0
167 0 _fix_user_data($_) foreach values %$node;
168 }
169  
170 0 return $node;
171 }
172  
173 # JSON::Validator warns about a format that it has no rule for, on every value
174 # of it, and lets the value through. Linode names formats of its own.
175 0 ▶sub _add_formats {
Subroutine _add_formats: not called CC=4 SCAR=30.0
176 0 my ( $formats, $spec ) = @_;
177  
178 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
179 0 ▶ $formats->{$_} //= sub { return }
Subroutine __ANON__: not called CC=1 SCAR=6.9
180 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
181  
182 0 return $formats;
183 }
184  
185 # Every format that the specification names, which does not change while it is loaded.
186 0 ▶sub _formats_in {
Subroutine _formats_in: not called CC=7 SCAR=40.3
187 0 my ($spec) = @_;
188  
189 0 state %found;
190 return @{
191 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
192 0 my ( %formats, @nodes );
193 0 @nodes = ($spec);
194 0 ▶ while (@nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes) 0 0 0
195 0 my $node = shift @nodes;
196 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
197 0 push @nodes, @$node;
198 }
199 elsif ( ref $node eq 'HASH' ) {
200 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
201 0 push @nodes, values %$node;
202 }
203 }
204 0 [ sort keys %formats ];
205 }
206 };
207 }
208  
209 # Give each kebab-case operation a snake_case name a caller can write as a
210 # bareword, never replacing a method the class already has.
211 0 ▶sub _alias_operations {
Subroutine _alias_operations: not called CC=6 SCAR=37.4
212 0 my ( $class, $routes ) = @_;
213  
214 0 state %aliased;
215 0 ▶ return if $aliased{$class}++;
Branch: 1 branch
BranchTrueFalseTotal
if $aliased{$class}++ 0 0 0
216  
217 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
218 0 foreach my $method ( $operation, "${operation}_p" ) {
219 0 ( my $alias = $method ) =~ tr/-/_/;
220 0 ▶ monkey_patch( $class, $alias => $class->can($method) ) unless $class->can($alias);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($alias) 0 0 0
221 }
222 }
223  
224 0 return;
225 }
226  
227 1 1;
228  
229 __END__
↑ Summary