Mojolicious/Command/openapi.pm

filter: Statementstmt 88.6% 70 / 79 Branchbran 73.6% 28 / 38 Conditioncond 66.6% 16 / 24 MC/DCmcdc 46.1% 6 / 13 Subroutinesub 70.0% 14 / 20 Podpod 100% 1 / 1 totaltotal 77.1% 135 / 175 CC 33 SCAR 23.6
CC
33
Cov
81%
run
22.4
_say
12.0
_info
6.2
CRAP 10.6
↑ Summary OpenAPI/Client.pm »
1 package Mojolicious::Command::openapi;
2 3 ▶use Mojo::Base 'Mojolicious::Command';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use OpenAPI::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Mojo::JSON qw(encode_json decode_json j);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Mojo::Util qw(encode getopt);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use constant YAML => eval 'require YAML::XS;1';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use constant REPLACE => $ENV{JSON_VALIDATOR_REPLACE} // 1;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"JSON_VALIDATOR_REPLACE"} // 1
l!l
03
MC/DC0%0/1
$ENV{"JSON_VALIDATOR_REPLACE"} // 1
$ENV{"JSON_VALIDATOR_REPLACE"}
Truth table50%1/2
A
A= $ENV{"JSON_VALIDATOR_REPLACE"}
Aresult
11
00
10  
11 0 ▶sub _say { length && say encode('UTF-8', $_) for @_ }
Subroutine _say: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
length $_ and say encode("UTF-8", $_) 0 0 0
12 0 ▶sub _warn { warn @_ }
Subroutine _warn: not called CC=1 SCAR=6.9
13  
14 3 has description => 'Perform Open API requests';
15 1 ▶has usage => sub { shift->extract_usage . "\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 3 has _client => undef;
18  
19 10 ▶sub run {
Subroutine run: called CC=19 SCAR=31.1
Pod documented
20 10 my ($self, @args) = @_;
21 10 my %ua;
22  
23 getopt \@args,
24 0 ▶ 'i|inactivity-timeout=i' => sub { $ua{inactivity_timeout} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 'I|information' => \my $info,
26 0 ▶ 'o|connect-timeout=i' => sub { $ua{connect_timeout} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 'p|parameter=s' => \my %parameters,
28 'c|content=s' => \my $content,
29 0 ▶ 'S|response-size=i' => sub { $ua{max_response_size} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 10 'v|verbose' => \my $verbose;
31  
32 # Read body from STDIN
33 10 vec(my $r, fileno(STDIN), 1) = 1;
34 10 ▶ $content //= !-t STDIN && select($r, undef, undef, 0) ? join '', <STDIN> : undef;
Branch: 1 branch
BranchTrueFalseTotal
!-t(STDIN) && select($r, undef, undef, 0) ? : 8 0 8
Condition60%3/5
$content //= !-t(STDIN) && select($r, undef, undef, 0) ? join("", readline STDIN) : undef
l!l
28
not -t STDIN and select $r, undef, undef, 0
!ll&&!rl&&r
008
MC/DC100%1/1
$content //= !-t(STDIN) && select($r, undef, undef, 0) ? join("", readline STDIN) : undef
$content
Truth table100%2/2
A
A= $content
Aresult
11
00
35  
36 10 my @client_args = (shift @args);
37 10 my $op = shift @args;
38 10 ▶ my $selector = shift @args // '';
Condition100%2/2
shift @args // ""
l!l
19
MC/DC100%1/1
shift @args // ""
shift @args
Truth table100%2/2
A
A= shift @args
Aresult
11
00
39  
40 10 ▶ die $self->usage unless $client_args[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $client_args[0] 1 9 10
41  
42 9 ▶ if ($client_args[0] =~ m!^/! and !-e $client_args[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client_args[0] =~ m[^/]u and not -e $client_args[0]) 3 6 9
Condition66%2/3
$client_args[0] =~ m[^/]u and not -e $client_args[0]
!ll&&!rl&&r
603
MC/DC50%1/2
$client_args[0] =~ m[^/]u and not -e $client_args[0]
$client_args[0] =~ m[^/]u not -e $client_args[0]
Truth table66%2/3
A and not(B)
A= $client_args[0] =~ m[^/]uB= not -e $client_args[0]
ABresult
0X0
100
111
43 3 $client_args[0] = Mojo::URL->new($client_args[0]);
44 3 push @client_args, app => $self->app;
45 }
46  
47 9 $self->_client(OpenAPI::Client->new(@client_args));
48 9 ▶ return $self->_info($op) if $info;
Branch: 1 branch
BranchTrueFalseTotal
if $info 2 7 9
49 7 ▶ return $self->_list unless $op;
Branch: 1 branch
BranchTrueFalseTotal
unless $op 2 5 7
50 5 ▶ die qq(Unknown operationId "$op".\n) unless $self->_client->can($op);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_client->can($op) 0 5 5
51  
52 5 ▶ $self->_client->ua->proxy->detect unless $ENV{OPENAPI_NO_PROXY};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"OPENAPI_NO_PROXY"} 5 0 5
53 5 $self->_client->ua->$_($ua{$_}) for keys %ua;
54 $self->_client->ua->on(
55 2 ▶ start => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
56 2 my ($ua, $tx) = @_;
57 2 weaken $tx;
58 0 ▶ $tx->res->content->on(body => sub { _warn _header($tx->req), _header($tx->res) }) if $verbose;
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
59 }
60 5 );
61  
62 5 ▶ my $tx = $self->_client->call($op => \%parameters, $content ? (json => decode_json $content) : ());
Branch: 1 branch
BranchTrueFalseTotal
$content ? : 2 3 5
63 5 ▶ if ($tx->error and $tx->error->{message} eq 'Invalid input') {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->error and $tx->error->{"message"} eq "Invalid input") 3 2 5
Condition66%2/3
$tx->error and $tx->error->{"message"} eq "Invalid input"
!ll&&!rl&&r
203
MC/DC50%1/2
$tx->error and $tx->error->{"message"} eq "Invalid input"
$tx->error $tx->error->{"message"} eq "Invalid input"
Truth table66%2/3
A and B
A= $tx->errorB= $tx->error->{"message"} eq "Invalid input"
ABresult
0X0
100
111
64 3 ▶ _warn _header($tx->req), _header($tx->res) if $verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 1 2 3
65 }
66  
67 5 ▶ return _json($tx->res->json, $selector) if !length $selector || $selector =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
if not length $selector or $selector =~ m[^/]u 5 0 5
Condition66%2/3
not length $selector or $selector =~ m[^/]u
l!l&&r!l&&!r
410
MC/DC0%0/2
not length $selector or $selector =~ m[^/]u
not length $selector $selector =~ m[^/]u
Truth table66%2/3
not(A) or B
A= not length $selectorB= $selector =~ m[^/]u
ABresult
1X1
011
000
68 0 return _say $tx->res->dom->find($selector)->each;
69 }
70  
71 2 ▶sub _header { $_[0]->build_start_line, $_[0]->headers->to_string, "\n\n" }
Subroutine _header: called CC=1 SCAR=0.0
72  
73 2 ▶sub _info {
Subroutine _info: called CC=6 SCAR=18.2
74 2 my ($self, $op) = @_;
75 2 local $YAML::XS::Boolean = 'JSON::PP';
76  
77 2 ▶ unless ($op) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op) 0 2 2
78 0 my $op_spec = $self->_client->validator->bundle->data;
79 0 return _say YAML ? YAML::XS::Dump($op_spec) : Mojo::Util::dumper($op_spec);
80 }
81  
82 2 my ($schema, $op_spec) = ($self->_client->validator);
83 2 for my $route ($schema->routes->each) {
84 2 ▶ next if !$route->{operation_id} or $route->{operation_id} ne $op;
Branch: 1 branch
BranchTrueFalseTotal
if not $route->{"operation_id"} or $route->{"operation_id"} ne $op 1 1 2
Condition66%2/3
not $route->{"operation_id"} or $route->{"operation_id"} ne $op
l!l&&r!l&&!r
011
MC/DC50%1/2
not $route->{"operation_id"} or $route->{"operation_id"} ne $op
not $route->{"operation_id"} $route->{"operation_id"} ne $op
Truth table66%2/3
not(A) or B
A= not $route->{"operation_id"}B= $route->{"operation_id"} ne $op
ABresult
1X1
011
000
85 1 $op_spec = $schema->get(['paths', @$route{qw(path method)}]);
86 }
87  
88 2 ▶ return _warn qq(Could not find the given operationId "$op".\n) unless $op_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless $op_spec 1 1 2
89 1 local $YAML::XS::Boolean = 'JSON::PP';
90 1 ▶ return _say YAML ? YAML::XS::Dump($op_spec) : Mojo::Util::dumper($op_spec);
Subroutine BEGIN: called CC=1 SCAR=0.0
91 }
92  
93 5 ▶sub _json {
Subroutine _json: called CC=4 SCAR=14.2
94 5 ▶ return unless defined(my $data = Mojo::JSON::Pointer->new(shift)->get(shift));
Branch: 1 branch
BranchTrueFalseTotal
unless defined(my $data = "Mojo::JSON::Pointer"->new(shift())->get(shift())) 0 5 5
95 5 ▶ return _say $data unless ref $data eq 'HASH' || ref $data eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" or ref $data eq "ARRAY" 1 4 5
Condition66%2/3
ref $data eq "HASH" or ref $data eq "ARRAY"
l!l&&r!l&&!r
401
MC/DC50%1/2
ref $data eq "HASH" or ref $data eq "ARRAY"
ref $data eq "HASH" ref $data eq "ARRAY"
Truth table66%2/3
A or B
A= ref $data eq "HASH"B= ref $data eq "ARRAY"
ABresult
1X1
011
000
96 4 _say Mojo::Util::decode('UTF-8', encode_json $data);
97 }
98  
99 2 ▶sub _list {
Subroutine _list: called CC=3 SCAR=11.1
100 2 my $self = shift;
101 2 _warn "--- Operations for @{[$self->_client->base_url]}\n";
102 2 ▶ $_->{operation_id} && _say $_->{operation_id} for $self->_client->validator->routes->each;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"operation_id"} and _say($_->{"operation_id"}) 2 0 2
103 }
104  
105 3 1;
106  
107 =encoding utf8
108  
109 =head1 NAME
110  
111 Mojolicious::Command::openapi - Perform Open API requests
112  
113 =head1 SYNOPSIS
114  
115 Usage: APPLICATION openapi SPECIFICATION OPERATION "{ARGUMENTS}" [SELECTOR|JSON-POINTER]
116  
117 # Fetch /api from myapp.pl and list available operationId
118 ./myapp.pl openapi /api
119  
120 # Dump the whole specification or for an operationId
121 ./myapp.pl openapi /api -I
122 ./myapp.pl openapi /api -I addPet
123  
124 # Run an operation against a local application
125 ./myapp.pl openapi /api listPets /pets/0
126  
127 # Run an operation against a local application, with body parameter
128 ./myapp.pl openapi /api addPet -c '{"name":"pluto"}'
129 echo '{"name":"pluto"} | ./myapp.pl openapi /api addPet
130  
131 # Run an operation with parameters
132 mojo openapi spec.json listPets -p limit=10 -p type=dog
133  
134 # Run against local or online specifications
135 mojo openapi /path/to/spec.json listPets
136 mojo openapi http://service.example.com/api.json listPets
137  
138 Options:
139 -h, --help Show this summary of available options
140 -c, --content <content> JSON content, with body parameter data
141 -i, --inactivity-timeout <seconds> Inactivity timeout, defaults to the
142 value of MOJO_INACTIVITY_TIMEOUT or 20
143 -I, --information [operationId] Dump the specification about a given
144 operationId or the whole spec.
145 YAML::XS is preferred if available.
146 -o, --connect-timeout <seconds> Connect timeout, defaults to the value
147 of MOJO_CONNECT_TIMEOUT or 10
148 -p, --parameter <name=value> Specify multiple header, path, or
149 query parameter
150 -S, --response-size <size> Maximum response size in bytes,
151 defaults to 2147483648 (2GB)
152 -v, --verbose Print request and response headers to
153 STDERR
154  
155 =head1 DESCRIPTION
156  
157 L<Mojolicious::Command::openapi> is a command line interface for
158 L<OpenAPI::Client>.
159  
160 Not that this implementation is currently EXPERIMENTAL! Feedback is
161 appreciated.
162  
163 =head1 ATTRIBUTES
164  
165 =head2 description
166  
167 $str = $command->description;
168  
169 =head2 usage
170  
171 $str = $command->usage;
172  
173 =head1 METHODS
174  
175 =head2 run
176  
177 $command->run(@ARGV);
178  
179 Run this command.
180  
181 =head1 SEE ALSO
182  
183 L<OpenAPI::Client>.
184  
185 =cut
↑ Summary OpenAPI/Client.pm »