HetznerCloud/Schema.pm

filter: Statementstmt 67.5% 77 / 114 Branchbran 28.5% 12 / 42 Conditioncond 14.2% 1 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.2% 13 / 16 Podpod 100% 1 / 1 totaltotal 56.5% 104 / 184 CC 30 SCAR 38.1
CC
30
Cov
55%
_get_params
83.5
_get_subnmame
56.0
validate
6.1
CRAP 45.1
↑ Summary HetznerCloud/APIBase.pm »
1 package VM::HetznerCloud::Schema;
2  
3 # ABSTRACT: OpenAPI schema for the cloud API
4  
5 4 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Mojo::Base -strict, -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use JSON::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use JSON::Validator::Formats;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use List::Util qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Mojo::JSON qw(decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Mojo::Loader qw(data_section);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Mojo::Util qw(camelize);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 4 ▶use constant IV_SIZE => eval 'require Config;$Config::Config{ivsize}'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 our $VERSION = '0.0.3'; # VERSION
22  
23 7 ▶sub validate ( $class, $operation, $params = {} ) {
Subroutine validate: called CC=6 SCAR=18.1
Pod documented
24 7 my ($spec, $validator) = _get_params($operation);
25  
26 my %check_params = map {
27 7 my $camelized = camelize $_;
28 6 $camelized =~ s{Id$}{ID};
29  
30 $spec->{param_names}->{$camelized} ?
31 ($camelized => $params->{$_}) :
32 6 ▶ ($_ => $params->{$_});
Branch: 1 branch
BranchTrueFalseTotal
$spec->{'param_names'}{$camelized} ? : 0 6 6
33 } keys $params->%*;
34  
35 7 my @errors = $validator->validate(
36 \%check_params,
37 );
38  
39 7 ▶ if ( @errors ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 7 7
40 0 return ( undef, @errors );
41 }
42  
43 7 my %request_params;
44  
45 7 for my $param_name ( sort keys $spec->{param_names}->%* ) {
46 19 my $key = $spec->{param_names}->{$param_name};
47  
48 19 ▶ next if !$check_params{$param_name};
Branch: 1 branch
BranchTrueFalseTotal
unless $check_params{$param_name} 15 4 19
49  
50 4 $request_params{$key}->{$param_name} = $check_params{$param_name};
51 }
52  
53 7 return \%request_params;
54 }
55  
56 0 ▶sub _get_subnmame ( $parts, $method ) {
Subroutine _get_subnmame: not called CC=7 SCAR=40.3
57 0 ▶ my $all = ( $parts->@* && $parts->[-1] =~ m{\{} ) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
@$parts && $parts->[-1] =~ /\{/u ? : 0 0 0
Condition0%0/3
@$parts and $parts->[-1] =~ /\{/u
!ll&&!rl&&r
000
MC/DC0%0/2
@$parts and $parts->[-1] =~ /\{/u
@$parts $parts->[-1] =~ /\{/u
Truth table0%0/3
A and B
A= @$partsB= $parts->[-1] =~ /\{/u
ABresult
0X0
100
111
58 0 my @static_parts = grep { $_ !~ m/\{/ } $parts->@*;
59  
60 0 ▶ my $suffix = @static_parts ? ( join '_', '', @static_parts ) : '';
Branch: 1 branch
BranchTrueFalseTotal
@static_parts ? : 0 0 0
61  
62 0 ▶ if ( $all ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all) 0 0 0
63 0 ▶ return 'list' . $suffix if $method eq 'get';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "get" 0 0 0
64 0 ▶ return 'create' . $suffix if $method eq 'post';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "post" 0 0 0
65 }
66  
67 0 return $method . $suffix;
68 }
69  
70 7 ▶sub _get_params ($operation) {
Subroutine _get_params: called CC=19 SCAR=44.2
71 7 state %operation_params;
72  
73 7 my $op_data = $operation_params{$operation};
74 7 ▶ return $op_data->@* if $op_data;
Branch: 1 branch
BranchTrueFalseTotal
if $op_data 5 2 7
75  
76 2 my $api_spec = data_section(__PACKAGE__, 'openapi.json');
77 2 $api_spec = encode_utf8( $api_spec );
78  
79 2 my $data = decode_json( $api_spec );
80  
81 2 ▶ my ($path, $method) = split '#', $operation // '1#1';
Condition50%1/2
$operation // "1#1"
l!l
20
MC/DC0%0/1
$operation // "1#1"
$operation
Truth table50%1/2
A
A= $operation
Aresult
11
00
82 2 my $op = $data->{$path}->{$method};
83  
84 2 ▶ return if !$op;
Branch: 1 branch
BranchTrueFalseTotal
unless $op 0 2 2
85  
86 2 my $params = $op->{parameters};
87  
88 2 my %properties;
89 my @required;
90 2 my %param_names;
91  
92 PARAM:
93 2 for my $param ( $params->@* ) {
94 5 ▶ next PARAM if $param->{name} eq 'Auth-API-Token';
Branch: 1 branch
BranchTrueFalseTotal
if $param->{"name"} eq "Auth-API-Token" 0 5 5
95  
96 5 my $name = $param->{name};
97  
98 5 ▶ if ( $param->{required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"required"}) 1 4 5
99 1 push @required, $name;
100 }
101  
102 5 $param_names{$name} = $param->{in};
103 5 $properties{$name} = $param;
104 }
105  
106 2 my ($content_type, $body_required);
107 2 ▶ if ( $op->{requestBody} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->{"requestBody"}) 0 2 2
108 0 my $body = $op->{requestBody}->{content};
109 0 $body_required = $op->{requestBody}->{required};
110  
111 0 ($content_type) = sort keys $body->%*;
112  
113 0 ▶ if ( $content_type eq 'application/json' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content_type eq 'application/json') { } 0 0 0
elsif ($content_type eq 'text/plain') { } 0 0 0
114 0 my $schema = $body->{$content_type}->{schema};
115 0 ▶ my $prop = $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
116 0 for my $property ( keys $prop->%* ) {
117 0 $properties{$property} = delete $prop->{$property};
118 0 $param_names{$property} = 'body';
119 }
120 }
121 elsif ( $content_type eq 'text/plain' ) {
122 0 $properties{text} = { type => "string" };
123 0 $param_names{text} = 'body';
124 }
125 }
126  
127 2 my $spec = {
128 type => 'object',
129 required => \@required,
130 body_required => $body_required,
131 properties => \%properties,
132 param_names => \%param_names,
133 content_type => $content_type,
134 };
135  
136 2 my $validator = JSON::Validator->new->schema($spec);
137  
138 0 ▶ $validator->formats->{uint32} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
139 0 my ($sub) = JSON::Validator::Formats->can('_match_number');
140  
141 0 $sub->( unint32 => $_[0], 'L' );
142 2 };
143  
144 0 ▶ $validator->formats->{uint64} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 0 my ($sub) = JSON::Validator::Formats->can('_match_number');
146  
147 0 $sub->( unint32 => $_[0], IV_SIZE >= 8 ? 'Q' : '' );
148 2 };
149  
150 2 ▶ if ( $spec->{properties}->{'$ref'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"properties"}{"\$ref"}) 0 2 2
151 0 my @ref = $spec->{properties}->{'$ref'};
152 0 ▶ while ( my $ref = shift @ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = shift @ref) 0 0 0
153 0 $ref =~ s{^#}{};
154  
155 0 my $data = $validator->get( $ref );
156 0 ▶ if ( $data->{properties} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"properties"}) 0 0 0
157 0 for my $property ( keys $data->{properties}->%* ) {
158 0 ▶ next if $data->{properties}->{$property}->{readOnly};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"properties"}{$property}{"readOnly"} 0 0 0
159  
160 0 $spec->{param_names}->{$property} = 'body';
161 }
162 }
163  
164 0 ▶ if ( $data->{allOf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"allOf"}) 0 0 0
165 0 push @ref, map{ $_->{'$ref'} } $data->{allOf}->@*;
166 0 ▶ if ( $data->{required} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->{"required"}) 0 0 0
167 0 push $spec->{required}->@*, $data->{required}->@*;
168 }
169 }
170 }
171 }
172  
173 2 $spec->{required}->@* = uniq $spec->{required}->@*;
174  
175 2 $operation_params{$operation} = [ $spec, $validator ];
176 2 return $operation_params{$operation}->@*;
177 }
178  
179 4 1;
180  
181 =pod
182  
183 =encoding UTF-8
184  
185 =head1 NAME
186  
187 VM::HetznerCloud::Schema - OpenAPI schema for the cloud API
188  
189 =head1 VERSION
190  
191 version 0.0.3
192  
193 =head1 SYNOPSIS
194  
195 =head1 METHODS
196  
197 =head2 validate
198  
199 =head1 AUTHOR
200  
201 Renee Baecker <reneeb@cpan.org>
202  
203 =head1 COPYRIGHT AND LICENSE
204  
205 This software is Copyright (c) 2018 by Renee Baecker.
206  
207 This is free software, licensed under:
208  
209 The Artistic License 2.0 (GPL Compatible)
210  
211 =cut
212  
213 __DATA__
↑ Summary HetznerCloud/APIBase.pm »