blib/lib/Linode/API.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 220 CC 12 SCAR 22.3
CC
12
Cov
0%
_fix_api_version
20.0
_fix_closed_all_of
20.0
_fix_json_headers
12.0
CRAP 9.3
1 package Linode::API;
2 $Linode::API::VERSION = '0.002';
3 #ABSTRACT: A client for the Linode API, generated from Linode's own OpenAPI specification
4  
5 use 5.016;
6  
7 use strict;
8 use warnings FATAL => 'all';
9  
10 use re '/aa';
11  
12 use parent qw{OpenAPI::Client};
13  
14 use Carp qw{croak};
15 use Cpanel::JSON::XS qw{decode_json};
16 use File::ShareDir qw{dist_file};
17 use File::Slurper qw{read_binary};
18 use Mojo::Util qw{monkey_patch};
19  
20  
21 sub new {
22 my ( $class, %options ) = @_;
23  
24 my $token = delete $options{token};
25 my $api_version = delete $options{api_version} // 'v4';
26 my $specification = delete $options{specification} // dist_file( 'Linode-API', 'openapi.json' );
27  
28 my $self = $class->SUPER::new( _specification( $specification, $api_version ), %options );
29 _alias_operations( ref $self, $self->validator->routes );
30 $self->on( after_build_tx => \&_unescape_slashes );
31 if ( defined $token ) {
32 $self->on( after_build_tx => sub { $_[1]->req->headers->authorization("Bearer $token") } );
33 }
34  
35 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 sub _unescape_slashes {
41 my ( undef, $tx ) = @_;
42  
43 my $path = $tx->req->url->path;
44 $path->parts( [ map { split q{/}, $_ } @{ $path->parts } ] );
45 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 sub _specification {
51 my ( $file, $api_version ) = @_;
52  
53 state %specifications;
54 return $specifications{"$api_version\0$file"} //= do {
55 my $spec = decode_json( read_binary($file) );
56 _fix_api_version( $spec, $api_version );
57 _fix_json_headers($spec);
58 _fix_closed_all_of($spec);
59 $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 sub _fix_api_version {
67 my ( $spec, $api_version ) = @_;
68  
69 my ( $paths, %offered ) = ( $spec->{paths} );
70 foreach my $path ( keys %$paths ) {
71 my $item = delete $paths->{$path};
72  
73 my ($parameter) = grep { ( $_->{name} // q{} ) eq 'apiVersion' } @{ $item->{parameters} // [] };
74 my @versions = @{ $parameter->{schema}{enum} // [] };
75 $offered{$_} = 1 for @versions;
76  
77 if (@versions) {
78 my $version = ( grep { $_ eq $api_version } @versions ) ? $api_version : $versions[0];
79 $item->{parameters} = [ grep { $_ != $parameter } @{ $item->{parameters} } ];
80 $path =~ s{\{apiVersion\}}{$version}g;
81 }
82 $paths->{$path} = $item;
83 }
84  
85 croak "api_version '$api_version' is not in the specification, which offers [" . join( ', ', sort keys %offered ) . ']' unless $offered{$api_version};
86  
87 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 sub _fix_json_headers {
93 my ($spec) = @_;
94  
95 foreach my $item ( values %{ $spec->{paths} } ) {
96 my @operations = grep { ref $_ eq 'HASH' } values %$item;
97 foreach my $parameter ( map { @{ $_->{parameters} // [] } } $item, @operations ) {
98 next unless ( $parameter->{in} // q{} ) eq 'header';
99 next if ( $parameter->{schema}{type} // q{} ) eq 'string';
100 $parameter->{schema} = { type => 'string', description => 'JSON' };
101 }
102 }
103  
104 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 sub _fix_closed_all_of {
110 my ($node) = @_;
111  
112 if ( ref $node eq 'ARRAY' ) {
113 _fix_closed_all_of($_) foreach @$node;
114 }
115 elsif ( ref $node eq 'HASH' ) {
116 if ( ref $node->{allOf} eq 'ARRAY' && @{ $node->{allOf} } > 1 ) {
117 foreach my $member ( grep { ref $_ eq 'HASH' && _is_closed($_) } @{ $node->{allOf} } ) {
118 delete $member->{additionalProperties};
119 }
120 }
121 _fix_closed_all_of($_) foreach values %$node;
122 }
123  
124 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 sub _is_closed {
129 my ($schema) = @_;
130 my $additional = $schema->{additionalProperties};
131 return defined $additional && ref $additional ne 'HASH' && !$additional;
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 sub _alias_operations {
137 my ( $class, $routes ) = @_;
138  
139 state %aliased;
140 return if $aliased{$class}++;
141  
142 foreach my $operation ( grep { defined && index( $_, q{-} ) >= 0 } map { $_->{operation_id} } $routes->each ) {
143 foreach my $method ( $operation, "${operation}_p" ) {
144 ( my $alias = $method ) =~ tr/-/_/;
145 monkey_patch( $class, $alias => $class->can($method) ) unless $class->can($alias);
146 }
147 }
148  
149 return;
150 }
151  
152 1;
153  
154 __END__