lib/Linode/API.pm

filter: Statementstmt 100% 112 / 112 Branchbran 95.4% 21 / 22 Conditioncond 74.2% 26 / 35 MC/DCmcdc 50.0% 10 / 20 Subroutinesub 100% 19 / 19 Podpod 100% 1 / 1 totaltotal 90.4% 189 / 209 CC 35 SCAR 17.5
CC
35
Cov
94%
_fix_closed_all_of
9.0
_fix_api_version
9.0
_fix_json_headers
8.0
CRAP 5.7
« blib/lib/Linode/API.pm ↑ Summary
1 package Linode::API;
2 2 $Linode::API::VERSION = '0.002';
3 #ABSTRACT: A client for the Linode API, generated from Linode's own OpenAPI specification
4  
5 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use parent qw{OpenAPI::Client};
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Carp qw{croak};
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Cpanel::JSON::XS qw{decode_json};
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use File::ShareDir qw{dist_file};
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use File::Slurper qw{read_binary};
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Mojo::Util qw{monkey_patch};
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20  
21 7 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
22 7 my ( $class, %options ) = @_;
23  
24 7 my $token = delete $options{token};
25 7 ▶ my $api_version = delete $options{api_version} // 'v4';
Condition100%2/2
delete $options{"api_version"} // "v4"
l!l
25
MC/DC100%1/1
delete $options{"api_version"} // "v4"
delete $options{"api_version"}
Truth table100%2/2
A
A= delete $options{"api_version"}
Aresult
11
00
26 7 ▶ my $specification = delete $options{specification} // dist_file( 'Linode-API', 'openapi.json' );
Condition33%1/3
delete $options{"specification"} // dist_file("Linode-API", "openapi.json")
l!l&&r!l&&!r
070
MC/DC0%0/2
delete $options{"specification"} // dist_file("Linode-API", "openapi.json")
delete $options{"specification"} dist_file("Linode-API", "openapi.json")
Truth table33%1/3
A // B
A= delete $options{"specification"}B= dist_file("Linode-API", "openapi.json")
ABresult
1X1
011
000
27  
28 7 my $self = $class->SUPER::new( _specification( $specification, $api_version ), %options );
29 6 _alias_operations( ref $self, $self->validator->routes );
30 6 $self->on( after_build_tx => \&_unescape_slashes );
31 6 ▶ if ( defined $token ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token) 3 3 6
32 3 ▶ $self->on( after_build_tx => sub { $_[1]->req->headers->authorization("Bearer $token") } );
Subroutine __ANON__: called CC=1 SCAR=0.0
33 }
34  
35 6 return $self;
36 }
37  
38 # An id Linode hands out can hold a slash, as private/123 does, and Linode wants
39 # it in the path as a slash, where OpenAPI::Client escapes it within one segment.
40 13 ▶sub _unescape_slashes {
Subroutine _unescape_slashes: called CC=1 SCAR=0.0
41 13 my ( undef, $tx ) = @_;
42  
43 13 my $path = $tx->req->url->path;
44 13 $path->parts( [ map { split q{/}, $_ } @{ $path->parts } ] );
45 13 return;
46 }
47  
48 # OpenAPI::Client names the class it generates after the hashref's address, so
49 # handing it the same hashref every time is what stops each client leaking a class.
50 10 ▶sub _specification {
Subroutine _specification: called CC=2 SCAR=6.9
51 10 my ( $file, $api_version ) = @_;
52  
53 10 state %specifications;
54 10 ▶ return $specifications{"$api_version\0$file"} //= do {
Condition66%2/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
730
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 table66%2/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
55 3 my $spec = decode_json( read_binary($file) );
56 3 _fix_api_version( $spec, $api_version );
57 2 _fix_json_headers($spec);
58 2 _fix_closed_all_of($spec);
59 2 $spec;
60 };
61 }
62  
63 # Write the version into each path, because OpenAPI::Client fills a path in
64 # before it validates, so a schema default for {apiVersion} never reaches the URL.
65 # A path that does not offer the version asked for gets the first one it does.
66 7 ▶sub _fix_api_version {
Subroutine _fix_api_version: called CC=9 SCAR=22.0
67 7 my ( $spec, $api_version ) = @_;
68  
69 7 my ( $paths, %offered ) = ( $spec->{paths} );
70 7 foreach my $path ( keys %$paths ) {
71 933 my $item = delete $paths->{$path};
72  
73 933 ▶ my ($parameter) = grep { ( $_->{name} // q{} ) eq 'apiVersion' } @{ $item->{parameters} // [] };
Condition75%3/4
$_->{"name"} // ""
l!l
16563
$item->{"parameters"} // []
l!l
9330
MC/DC100%1/1
$_->{"name"} // ""
$_->{"name"}
MC/DC0%0/1
$item->{"parameters"} // []
$item->{"parameters"}
Truth table100%2/2
A
A= $_->{"name"}
Aresult
11
00
Truth table50%1/2
A
A= $item->{"parameters"}
Aresult
11
00
74 933 ▶ my @versions = @{ $parameter->{schema}{enum} // [] };
Condition100%2/2
$parameter->{"schema"}{"enum"} // []
l!l
9303
MC/DC100%1/1
$parameter->{"schema"}{"enum"} // []
$parameter->{"schema"}{"enum"}
Truth table100%2/2
A
A= $parameter->{"schema"}{"enum"}
Aresult
11
00
75 933 $offered{$_} = 1 for @versions;
76  
77 933 ▶ if (@versions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@versions) 930 3 933
78 930 ▶ my $version = ( grep { $_ eq $api_version } @versions ) ? $api_version : $versions[0];
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $api_version;} @versions) ? : 592 338 930
79 930 $item->{parameters} = [ grep { $_ != $parameter } @{ $item->{parameters} } ];
80 930 $path =~ s{\{apiVersion\}}{$version}g;
81 }
82 933 $paths->{$path} = $item;
83 }
84  
85 7 ▶ 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} 3 4 7
86  
87 4 return $spec;
88 }
89  
90 # The specification describes X-Filter as the object its JSON encodes, and
91 # OpenAPI::Client would send that object as HASH(0x...), so a header is a string.
92 3 ▶sub _fix_json_headers {
Subroutine _fix_json_headers: called CC=8 SCAR=20.8
93 3 my ($spec) = @_;
94  
95 3 foreach my $item ( values %{ $spec->{paths} } ) {
96 617 my @operations = grep { ref $_ eq 'HASH' } values %$item;
97 617 ▶ foreach my $parameter ( map { @{ $_->{parameters} // [] } } $item, @operations ) {
Condition100%2/2
$_->{"parameters"} // []
l!l
7541378
MC/DC100%1/1
$_->{"parameters"} // []
$_->{"parameters"}
Truth table100%2/2
A
A= $_->{"parameters"}
Aresult
11
00
98 763 ▶ next unless ( $parameter->{in} // q{} ) eq 'header';
Branch: 1 branch
BranchTrueFalseTotal
unless ($parameter->{"in"} // "") eq "header" 751 12 763
Condition50%1/2
$parameter->{"in"} // ""
l!l
7630
MC/DC0%0/1
$parameter->{"in"} // ""
$parameter->{"in"}
Truth table50%1/2
A
A= $parameter->{"in"}
Aresult
11
00
99 12 ▶ next if ( $parameter->{schema}{type} // q{} ) eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if ($parameter->{"schema"}{"type"} // "") eq "string" 1 11 12
Condition100%2/2
$parameter->{"schema"}{"type"} // ""
l!l
39
MC/DC100%1/1
$parameter->{"schema"}{"type"} // ""
$parameter->{"schema"}{"type"}
Truth table100%2/2
A
A= $parameter->{"schema"}{"type"}
Aresult
11
00
100 11 $parameter->{schema} = { type => 'string', description => 'JSON' };
101 }
102 }
103  
104 3 return $spec;
105 }
106  
107 # A member of an allOf that says additionalProperties: false forbids what its
108 # siblings declare, so Linode's check of the body is the one that has to do.
109 222227 ▶sub _fix_closed_all_of {
Subroutine _fix_closed_all_of: called CC=9 SCAR=22.0
110 222227 my ($node) = @_;
111  
112 222227 ▶ if ( ref $node eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $node eq 'ARRAY') { } 11742 210485 222227
elsif (ref $node eq 'HASH') { } 58901 151584 210485
113 11742 _fix_closed_all_of($_) foreach @$node;
114 }
115 elsif ( ref $node eq 'HASH' ) {
116 58901 ▶ if ( ref $node->{allOf} eq 'ARRAY' && @{ $node->{allOf} } > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node->{"allOf"} eq "ARRAY" and @{$node->{"allOf"};} > 1) 89 58812 58901
Condition100%3/3
ref $node->{"allOf"} eq "ARRAY" and @{$node->{"allOf"};} > 1
!ll&&!rl&&r
587634989
MC/DC100%2/2
ref $node->{"allOf"} eq "ARRAY" and @{$node->{"allOf"};} > 1
ref $node->{"allOf"} eq "ARRAY" @{$node->{"allOf"};} > 1
Truth table100%3/3
A and B
A= ref $node->{"allOf"} eq "ARRAY"B= @{$node->{"allOf"};} > 1
ABresult
0X0
100
111
117 89 ▶ foreach my $member ( grep { ref $_ eq 'HASH' && _is_closed($_) } @{ $node->{allOf} } ) {
Condition66%2/3
ref $_ eq 'HASH' && _is_closed($_)
!ll&&!rl&&r
066115
MC/DC50%1/2
ref $_ eq 'HASH' && _is_closed($_)
ref $_ eq 'HASH' _is_closed($_)
Truth table66%2/3
A && B
A= ref $_ eq 'HASH'B= _is_closed($_)
ABresult
0X0
100
111
118 115 delete $member->{additionalProperties};
119 }
120 }
121 58901 _fix_closed_all_of($_) foreach values %$node;
122 }
123  
124 222227 return $node;
125 }
126  
127 # A JSON false is an object, so ask it what it is rather than whether it is a ref.
128 181 ▶sub _is_closed {
Subroutine _is_closed: called CC=3 SCAR=11.0
129 181 my ($schema) = @_;
130 181 my $additional = $schema->{additionalProperties};
131 181 ▶ return defined $additional && ref $additional ne 'HASH' && !$additional;
Condition83%5/6
defined $additional && ref $additional ne 'HASH' && !$additional
!ll&&!rl&&r
660115
defined $additional and ref $additional ne "HASH"
!ll&&!rl&&r
651115
MC/DC66%2/3
defined $additional && ref $additional ne 'HASH' && !$additional
defined $additional ref $additional ne "HASH" !$additional
Truth table75%3/4
A and B && not(C)
A= defined $additionalB= ref $additional ne "HASH"C= !$additional
ABCresult
0XX0
10X0
1100
1111
132 }
133  
134 # Give each kebab-case operation a snake_case name a caller can write as a
135 # bareword, never replacing a method the class already has.
136 6 ▶sub _alias_operations {
Subroutine _alias_operations: called CC=6 SCAR=18.2
137 6 my ( $class, $routes ) = @_;
138  
139 6 state %aliased;
140 6 ▶ return if $aliased{$class}++;
Branch: 1 branch
BranchTrueFalseTotal
if $aliased{$class}++ 4 2 6
141  
142 2 ▶ foreach my $operation ( grep { defined && index( $_, q{-} ) >= 0 } map { $_->{operation_id} } $routes->each ) {
Condition33%1/3
defined $_ && index($_, '-') != -1
!ll&&!rl&&r
00898
MC/DC0%0/2
defined $_ && index($_, '-') != -1
defined $_ index($_, '-') != -1
Truth table33%1/3
A && B
A= defined $_B= index($_, '-') != -1
ABresult
0X0
100
111
143 898 foreach my $method ( $operation, "${operation}_p" ) {
144 1796 ( my $alias = $method ) =~ tr/-/_/;
145 1796 ▶ monkey_patch( $class, $alias => $class->can($method) ) unless $class->can($alias);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->can($alias) 1796 0 1796
146 }
147 }
148  
149 2 return;
150 }
151  
152 2 1;
153  
154 __END__
« blib/lib/Linode/API.pm ↑ Summary