Client/Specification.pm

filter: Statementstmt 29.1% 35 / 120 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 52.6% 10 / 19 Podpod 100% 4 / 4 totaltotal 19.3% 49 / 253 CC 46 SCAR 42.4
CC
46
Cov
17%
_entry
132.0
fetch
110.0
__ANON__
90.0
CRAP 69.5
↑ Summary Client.pm »
1 package SolusVM::Client::Specification;
2 2 $SolusVM::Client::Specification::VERSION = '0.001';
3 2 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use re '/aa';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Path qw{make_path};
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Slurper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use File::Slurper::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use HTTP::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Time::Piece ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # ABSTRACT: The SolusVM 2 API, distilled out of its OpenAPI document
16  
17  
18 2 our $SPEC_URL = 'https://installer.dev.solusvm.com/admin-docs.json';
19 2 our $SPEC_FILE = 'solusvm2.json';
20  
21 # The methods of an OpenAPI path item that are operations. Everything else in
22 # there -- parameters, summary, servers -- describes the path, not a call.
23 2 my %IS_OPERATION = map { $_ => 1 } qw{get put post delete patch head options trace};
24  
25  
26 0 ▶sub load (%options) {
Subroutine load: not called CC=4 SCAR=30.0
Pod documented
27 0 state %cache;
28  
29 0 ▶ my $key = $options{file} // q{};
Condition0%0/2
$options{"file"} // ""
l!l
00
MC/DC0%0/1
$options{"file"} // ""
$options{"file"}
Truth table0%0/2
A
A= $options{"file"}
Aresult
11
00
30 0 ▶ return $cache{$key} if $cache{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $cache{$key} 0 0 0
31  
32 0 ▶ my $json = defined $options{file} ? File::Slurper::read_binary( $options{file} ) : _baked();
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'file'} ? : 0 0 0
33 0 my $entries = Cpanel::JSON::XS->new->utf8->decode($json);
34  
35 0 my %spec;
36 0 @spec{ map { $_->{name} } @{$entries} } = @{$entries};
37  
38 0 $cache{$key} = \%spec;
39 0 return \%spec;
40 }
41  
42  
43 0 ▶sub distil ($document) {
Subroutine distil: not called CC=7 SCAR=40.3
Pod documented
44 0 my $paths = $document->{paths};
45 0 ▶ die "That is not an OpenAPI document: it has no paths.\n" unless ref $paths eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $paths eq "HASH" 0 0 0
46  
47 0 my $resolve = _resolver($document);
48  
49 0 my @entries;
50 0 foreach my $uri ( sort keys %{$paths} ) {
51 0 foreach my $method ( sort keys %{ $paths->{$uri} } ) {
52 0 ▶ next unless $IS_OPERATION{$method};
Branch: 1 branch
BranchTrueFalseTotal
unless $IS_OPERATION{$method} 0 0 0
53 0 push @entries, _entry( $uri, $method, $paths->{$uri}{$method}, $resolve );
54 }
55 }
56  
57 0 my %seen;
58 0 foreach my $entry (@entries) {
59 0 my $already = $seen{ $entry->{name} };
60 0 ▶ die "$entry->{operation_id} and $already->{operation_id} would both be called $entry->{name}.\n" if $already;
Branch: 1 branch
BranchTrueFalseTotal
if $already 0 0 0
61 0 $seen{ $entry->{name} } = $entry;
62 }
63  
64 0 return \@entries;
65 }
66  
67 # Path parameters come out of the URI rather than out of the operation's
68 # parameter list, because the document does not always declare them: GET
69 # /servers/{id} names no parameters at all. The URI is the one place a
70 # placeholder is guaranteed to appear, since appearing is what it is for.
71 0 ▶sub _entry ( $uri, $method, $operation, $resolve ) {
Subroutine _entry: not called CC=11 SCAR=48.8
72 0 my $id = $operation->{operationId};
73 0 ▶ die "$method $uri has no operationId, so there is nothing to name it after.\n" unless defined $id && length $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and length $id 0 0 0
Condition0%0/3
defined $id and length $id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $id and length $id
defined $id length $id
Truth table0%0/3
A and B
A= defined $idB= length $id
ABresult
0X0
100
111
74  
75 0 my @path_params = $uri =~ m/[{](\w+)[}]/g;
76 0 ▶ my @query_params = map { $_->{name} } grep { ( $_->{in} // q{} ) eq 'query' } @{ $operation->{parameters} // [] };
Condition0%0/4
$_->{"in"} // ""
l!l
00
$operation->{"parameters"} // []
l!l
00
MC/DC0%0/1
$_->{"in"} // ""
$_->{"in"}
MC/DC0%0/1
$operation->{"parameters"} // []
$operation->{"parameters"}
Truth table0%0/2
A
A= $_->{"in"}
Aresult
11
00
Truth table0%0/2
A
A= $operation->{"parameters"}
Aresult
11
00
77  
78 0 my $schema = $resolve->( $operation->{requestBody} )->{content}{'application/json'}{schema};
79 0 $schema = $resolve->($schema);
80  
81 return {
82 name => operation_name( $id, uc $method, $uri ),
83 operation_id => $id,
84 method => uc $method,
85 uri => $uri,
86 path_params => \@path_params,
87 query_params => \@query_params,
88 0 ▶ body_params => [ sort keys %{ $schema->{properties} // {} } ],
Condition0%0/2
$schema->{"properties"} // {}
l!l
00
MC/DC0%0/1
$schema->{"properties"} // {}
$schema->{"properties"}
Truth table0%0/2
A
A= $schema->{"properties"}
Aresult
11
00
89 0 ▶ required => [ sort @{ $schema->{required} // [] } ],
Branch: 1 branch
BranchTrueFalseTotal
$operation->{'requestBody'} ? : 0 0 0
Condition0%0/8
$schema->{"required"} // []
l!l
00
+($operation->{"tags"} // [])->[0] // ""
l!l
00
$operation->{"tags"} // []
l!l
00
$operation->{"summary"} // ""
l!l
00
MC/DC0%0/1
$schema->{"required"} // []
$schema->{"required"}
MC/DC0%0/1
+($operation->{"tags"} // [])->[0] // ""
$operation->{"tags"}
MC/DC0%0/1
$operation->{"summary"} // ""
$operation->{"summary"}
Truth table0%0/2
A
A= $schema->{"required"}
Aresult
11
00
Truth table0%0/2
A
A= $operation->{"tags"}
Aresult
11
00
Truth table0%0/2
A
A= $operation->{"summary"}
Aresult
11
00
90 has_body => ( $operation->{requestBody} ? 1 : 0 ),
91 tag => ( $operation->{tags} // [] )->[0] // q{},
92 0 summary => $operation->{summary} // q{},
93 };
94 }
95  
96  
97 0 ▶sub operation_name ( $operation_id, $method = undef, $uri = undef ) {
Subroutine operation_name: not called CC=8 SCAR=42.8
Pod documented
98 0 my $name = lc $operation_id;
99 0 $name =~ s/[^a-z0-9]+/_/g;
100 0 $name =~ s/^_+|_+$//g;
101  
102 0 ▶ return $name unless _is_digest($operation_id) || $name !~ m/^[a-z]/;
Branch: 1 branch
BranchTrueFalseTotal
unless _is_digest($operation_id) or not $name =~ /^[a-z]/aa 0 0 0
Condition0%0/3
_is_digest($operation_id) or not $name =~ /^[a-z]/aa
l!l&&r!l&&!r
000
MC/DC0%0/2
_is_digest($operation_id) or not $name =~ /^[a-z]/aa
_is_digest($operation_id) not $name =~ /^[a-z]/aa
Truth table0%0/3
A or not(B)
A= _is_digest($operation_id)B= not $name =~ /^[a-z]/aa
ABresult
1X1
011
000
103 0 ▶ die "$operation_id is no kind of name, and there is no verb and path here to build one from.\n" unless defined $method && defined $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $method and defined $uri 0 0 0
Condition0%0/3
defined $method and defined $uri
!ll&&!rl&&r
000
MC/DC0%0/2
defined $method and defined $uri
defined $method defined $uri
Truth table0%0/3
A and B
A= defined $methodB= defined $uri
ABresult
0X0
100
111
104  
105 0 ▶ my @segments = grep { length && index( $_, '{' ) != 0 } split '/', $uri;
Condition0%0/3
length $_ && index($_, '{') != 0
!ll&&!rl&&r
000
MC/DC0%0/2
length $_ && index($_, '{') != 0
length $_ index($_, '{') != 0
Truth table0%0/3
A && B
A= length $_B= index($_, '{') != 0
ABresult
0X0
100
111
106 0 return join '_', lc $method, @segments;
107 }
108  
109 # What SolusVM leaves behind when whatever generates their document fails to
110 # name an operation. Matching the shape rather than listing the thirteen means
111 # the fourteenth is handled too.
112 0 ▶sub _is_digest ($operation_id) {
Subroutine _is_digest: not called CC=2 SCAR=17.9
113 0 ▶ return $operation_id =~ m/^[0-9a-f]{32}$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$operation_id =~ /^[0-9a-f]{32}$/aa ? : 0 0 0
114 }
115  
116  
117 0 ▶sub fetch (%options) {
Subroutine fetch: not called CC=10 SCAR=47.0
Pod documented
118 0 my $dir = $options{dir};
119 0 ▶ die "fetch needs a directory to write the spec into.\n" unless defined $dir && length $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir and length $dir 0 0 0
Condition0%0/3
defined $dir and length $dir
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dir and length $dir
defined $dir length $dir
Truth table0%0/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
120  
121 0 ▶ my $url = $options{url} // $SPEC_URL;
Condition0%0/3
$options{"url"} // $SolusVM::Client::Specification::SPEC_URL
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"url"} // $SolusVM::Client::Specification::SPEC_URL
$options{"url"} $SolusVM::Client::Specification::SPEC_URL
Truth table0%0/3
A // B
A= $options{"url"}B= $SolusVM::Client::Specification::SPEC_URL
ABresult
1X1
011
000
122 0 my $file = File::Spec->catfile( $dir, $SPEC_FILE );
123 0 ▶ my $ua = $options{ua} // HTTP::Tiny->new( timeout => 60 );
Condition0%0/3
$options{"ua"} // "HTTP::Tiny"->new("timeout", 60)
l!l&&r!l&&!r
000
MC/DC0%0/2
$options{"ua"} // "HTTP::Tiny"->new("timeout", 60)
$options{"ua"} "HTTP::Tiny"->new("timeout", 60)
Truth table0%0/3
A // B
A= $options{"ua"}B= "HTTP::Tiny"->new("timeout", 60)
ABresult
1X1
011
000
124  
125 0 my %headers;
126 0 ▶ if ( $options{once} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"once"}) 0 0 0
127 0 my @stat = stat $file;
128 0 ▶ $headers{'If-Modified-Since'} = Time::Piece->gmtime( $stat[9] )->strftime('%a, %d %b %Y %H:%M:%S GMT') if @stat;
Branch: 1 branch
BranchTrueFalseTotal
if @stat 0 0 0
129 }
130  
131 0 my $response = $ua->get( $url, { headers => \%headers } );
132 0 ▶ return undef if $response->{status} == 304;
Branch: 1 branch
BranchTrueFalseTotal
if $response->{"status"} == 304 0 0 0
133 0 ▶ die "Could not fetch the SolusVM API spec from $url: $response->{status} $response->{reason}\n" unless $response->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $response->{"success"} 0 0 0
134  
135 0 my $entries = distil( Cpanel::JSON::XS->new->utf8->decode( $response->{content} ) );
136  
137 0 ▶ make_path($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
138 0 File::Slurper::Temp::write_binary( $file, Cpanel::JSON::XS->new->utf8->canonical->encode($entries) );
139  
140 0 return $file;
141 }
142  
143 # OpenAPI lets any node be a reference to one somewhere else in the document,
144 # and SolusVM uses that for the bodies that several operations share. Following
145 # one is the difference between knowing what a call takes and knowing only that
146 # it takes something.
147 0 ▶sub _resolver ($document) {
Subroutine _resolver: not called CC=1 SCAR=6.9
148 0 my $follow;
149 0 ▶ $follow = sub ( $node, $depth = 0 ) {
Subroutine __ANON__: not called CC=9 SCAR=45.0
150 0 ▶ return {} unless ref $node eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "HASH" 0 0 0
151 0 ▶ return $node unless defined $node->{'$ref'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $node->{"\$ref"} 0 0 0
152 0 ▶ die "A \$ref in this document points at itself, or close enough: $node->{'$ref'}\n" if $depth > 10;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 10 0 0 0
153  
154 0 my $target = $document;
155 0 foreach my $step ( split '/', $node->{'$ref'} ) {
156 0 ▶ next if $step eq '#' || !length $step;
Branch: 1 branch
BranchTrueFalseTotal
if $step eq "#" or not length $step 0 0 0
Condition0%0/3
$step eq "#" or not length $step
l!l&&r!l&&!r
000
MC/DC0%0/2
$step eq "#" or not length $step
$step eq "#" not length $step
Truth table0%0/3
A or not(B)
A= $step eq "#"B= not length $step
ABresult
1X1
011
000
157 0 $target = $target->{$step};
158 0 ▶ return {} unless ref $target eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $target eq "HASH" 0 0 0
159 }
160  
161 0 return $follow->( $target, $depth + 1 );
162 0 };
163  
164 0 return $follow;
165 }
166  
167 # Slurped once and kept, because there is only one pass through a filehandle.
168 0 ▶sub _baked {
Subroutine _baked: not called CC=2 SCAR=17.9
169 0 state $baked;
170 0 ▶ $baked //= do { local $/ = undef; <DATA> };
Condition0%0/3
$baked //= do { local $/ = undef; readline DATA }
l!l&&r!l&&!r
000
MC/DC0%0/2
$baked //= do { local $/ = undef; readline DATA }
$baked do { local $/ = undef; readline DATA }
Truth table0%0/3
A //= B
A= $bakedB= do { local $/ = undef; readline DATA }
ABresult
1X1
011
000
171 0 return $baked;
172 }
173  
174 2 1;
175  
176 =pod
177  
178 =encoding UTF-8
179  
180 =head1 NAME
181  
182 SolusVM::Client::Specification - The SolusVM 2 API, distilled out of its OpenAPI document
183  
184 =head1 VERSION
185  
186 version 0.001
187  
188 =head1 SYNOPSIS
189  
190 my $spec = SolusVM::Client::Specification::load();
191  
192 print $spec->{get_list_of_servers}{method}, "\n"; # GET
193 print $spec->{get_list_of_servers}{uri}, "\n"; # /servers
194  
195 =head1 DESCRIPTION
196  
197 SolusVM publishes an OpenAPI 3.0 document describing the 309 operations of its
198 version 2 API. This module carries a distillation of it -- everything
199 L<SolusVM::Client> needs in order to turn an operation into an HTTP request, and
200 nothing else -- so that a client can be built out of the vendor's own description
201 of the API rather than out of a hand-written list that goes stale.
202  
203 The distillation is baked into this file, so nothing reaches the network at
204 runtime. L</fetch> is how it gets regenerated when SolusVM ships new endpoints.
205  
206 =head1 FUNCTIONS
207  
208 =head2 load
209  
210 Returns the spec as a hashref keyed on operation name, each value a hashref of:
211  
212 =over 4
213  
214 =item * C<name> -- the perl method name, as L</operation_name> derived it
215  
216 =item * C<operation_id> -- the C<operationId> it came from, to find the operation in the published reference
217  
218 =item * C<method> -- the HTTP verb, upper case
219  
220 =item * C<uri> -- the path below the API prefix, C<{id}> placeholders and all
221  
222 =item * C<path_params> -- the placeholder names in C<uri>, in the order they appear
223  
224 =item * C<query_params> -- the query parameter names the operation declares
225  
226 =item * C<body_params> -- the body property names it declares, and C<required> the subset it insists on
227  
228 =item * C<has_body> -- whether the operation takes a request body
229  
230 =item * C<tag>, C<summary> -- the grouping and the one-line description, for L<SolusVM::Client/catalog>
231  
232 =back
233  
234 Takes an optional C<< file => $path >> naming a distilled spec written by
235 L</fetch>, for running against an API newer than this release.
236  
237 The result is memoised for the life of the process, keyed on that path. A spec
238 file that changes underneath a running program is not a case worth serving; call
239 it again with a different path to get a different spec.
240  
241 =head2 distil
242  
243 Takes the decoded OpenAPI document and returns the arrayref of entries described
244 in L</load>. Kept apart from L</fetch> so that what the client believes about an
245 API can be tested against a document without fetching one.
246  
247 =head2 operation_name
248  
249 Turns an C<operationId> into a perl method name: lower cased, with every run of
250 characters that cannot appear in an identifier collapsed to a single underscore.
251  
252 The result is clumsy -- C<create_a_new_server>, C<get_an_existing_api_token> --
253 and deliberately so. It derives in both directions, so a name in your code can
254 be found in SolusVM's published reference by eye, and an operation added to a
255 future spec arrives named without anybody having to decide anything.
256  
257 Thirteen operations have no name to derive one from. SolusVM's document gives
258 them a 32 character hex digest where the C<operationId> should be -- the whole of
259 C<License>, C<Search>, C<Usage> and C<Server Batch Actions> -- and one of those
260 digests begins with a digit, which perl will not accept as a method name at all.
261 For those the name is built out of the verb and the path instead, so
262 C<POST /servers/start> becomes C<post_servers_start>. Pass C<$method> and C<$uri>
263 so that it can be.
264  
265 =head2 fetch
266  
267 Downloads the OpenAPI document, distils it, and writes the result as JSON into
268 C<< dir => $dir >> as F<solusvm2.json>. Returns the path it wrote, or undef when
269 there was nothing to do.
270  
271 Options are C<dir> (required), C<url> (defaulting to
272 C<$SolusVM::Client::Specification::SPEC_URL>), C<once> to skip the download when
273 the file is already there and the server reports nothing newer, and C<ua> to hand
274 it your own L<HTTP::Tiny>.
275  
276 This is on no runtime path. It exists to regenerate the baked-in spec, and to
277 let somebody run against an API newer than this release without waiting for one:
278  
279 perl -MSolusVM::Client::Specification -e 'SolusVM::Client::Specification::fetch( dir => "." )'
280  
281 The URL it defaults to is the one the published reference at
282 L<https://docs.solusvm.com/en/solusvm2/api-reference/api/> loads its document
283 from, and it lives on a vendor development host. Expect to need C<url> one day.
284  
285 =head1 SEE ALSO
286  
287 Please see those modules/websites for more information related to this module.
288  
289 =over 4
290  
291 =item *
292  
293 L<SolusVM::Client|SolusVM::Client>
294  
295 =back
296  
297 =head1 BUGS
298  
299 Please report any bugs or feature requests on the bugtracker website
300 L<https://github.com/Troglodyne-Internet-Widgets/perl-solusvm-client/issues>
301  
302 When submitting a bug or request, please include a test-file or a
303 patch to an existing test-file that illustrates the bug or desired
304 feature.
305  
306 =head1 AUTHORS
307  
308 Current Maintainers:
309  
310 =over 4
311  
312 =item *
313  
314 George S. Baugh <george@troglodyne.net>
315  
316 =back
317  
318 =head1 COPYRIGHT AND LICENSE
319  
320 Copyright (c) 2026 Troglodyne LLC
321  
322  
323 Permission is hereby granted, free of charge, to any person obtaining a copy
324 of this software and associated documentation files (the "Software"), to deal
325 in the Software without restriction, including without limitation the rights
326 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
327 copies of the Software, and to permit persons to whom the Software is
328 furnished to do so, subject to the following conditions:
329 The above copyright notice and this permission notice shall be included in all
330 copies or substantial portions of the Software.
331 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
332 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
333 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
334 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
335 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
336 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
337 SOFTWARE.
338  
339 =cut
340  
341 __DATA__
↑ Summary Client.pm »