OpenAPI/Client.pm

filter: Statementstmt 91.8% 157 / 171 Branchbran 77.6% 59 / 76 Conditioncond 65.0% 13 / 20 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 93.7% 30 / 32 Podpod 100% 4 / 4 totaltotal 85.0% 267 / 314 CC 65 SCAR 16.3
CC
65
Cov
86%
_coerce_collection_format
10.1
_generate_class
9.2
new
7.0
CRAP 5.1
« Mojolicious/Command/openapi.pm ↑ Summary
1 package OpenAPI::Client;
2 11 ▶use Mojo::EventEmitter -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 9 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use JSON::Validator;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 ▶use constant DEBUG => $ENV{OPENAPI_CLIENT_DEBUG} || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'OPENAPI_CLIENT_DEBUG'} || 0
l!l
09
MC/DC0%0/1
$ENV{'OPENAPI_CLIENT_DEBUG'} || 0
$ENV{'OPENAPI_CLIENT_DEBUG'}
Truth table50%1/2
A
A= $ENV{'OPENAPI_CLIENT_DEBUG'}
Aresult
11
00
11  
12 9 our $VERSION = '1.08';
13  
14 has base_url => sub {
15 18 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
16 18 my $validator = $self->validator;
17 18 ▶ my $url = $validator->can('base_url') ? $validator->base_url->clone : Mojo::URL->new;
Branch: 1 branch
BranchTrueFalseTotal
$validator->can('base_url') ? : 16 2 18
18 18 ▶ $url->scheme('http') unless $url->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $url->scheme 4 14 18
19 18 ▶ $url->host('localhost') unless $url->host;
Branch: 1 branch
BranchTrueFalseTotal
unless $url->host 4 14 18
20 18 return $url;
21 9 };
22  
23 23 ▶has ua => sub { Mojo::UserAgent->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 sub call {
26 7 ▶ my ($self, $op) = (shift, shift);
Subroutine call: called CC=2 SCAR=6.9
Pod documented
27 7 ▶ my $code = $self->can($op) or Carp::croak('[OpenAPI::Client] No such operationId');
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can($op) or Carp::croak("[OpenAPI::Client] No such operationId") 1 6 7
28 6 return $self->$code(@_);
29 }
30  
31 sub call_p {
32 2 ▶ my ($self, $op) = (shift, shift);
Subroutine call_p: called CC=2 SCAR=6.9
Pod documented
33 2 ▶ my $code = $self->can("${op}_p") or return Mojo::Promise->reject('[OpenAPI::Client] No such operationId');
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can("${op}_p") or return "Mojo::Promise"->reject("[OpenAPI::Client] No such operationId") 1 1 2
34 1 return $self->$code(@_);
35 }
36  
37 sub new {
38 24 ▶ my ($parent, $specification) = (shift, shift);
Subroutine new: called CC=7 SCAR=19.5
Pod documented
39 24 ▶ my $attrs = @_ == 1 ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 24 24
40  
41 24 my $class = $parent->_url_to_class($specification);
42 24 ▶ $parent->_generate_class($class, $specification, $attrs) unless $class->isa($parent);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->isa($parent) 10 14 24
43  
44 24 my $self = $class->SUPER::new($attrs);
45 24 ▶ $self->base_url(Mojo::URL->new($self->{base_url})) if $self->{base_url} and !blessed $self->{base_url};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"base_url"} and not builtin::blessed($self->{"base_url"}) 1 23 24
Condition100%3/3
$self->{"base_url"} and not builtin::blessed($self->{"base_url"})
!ll&&!rl&&r
2211
MC/DC100%2/2
$self->{"base_url"} and not builtin::blessed($self->{"base_url"})
$self->{"base_url"} not builtin::blessed($self->{"base_url"})
Truth table100%3/3
A and not(B)
A= $self->{"base_url"}B= not builtin::blessed($self->{"base_url"})
ABresult
0X0
100
111
46 24 ▶ $self->ua->transactor->name('Mojo-OpenAPI (Perl)') unless $self->{ua};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ua"} 23 1 24
47  
48 24 ▶ if (my $app = delete $self->{app}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $app = delete $self->{"app"}) 8 16 24
49 8 $self->base_url->host(undef)->scheme(undef)->port(undef);
50 8 $self->ua->server->app($app);
51 }
52  
53 24 return $self;
54 }
55  
56 0 ▶sub validator { Carp::confess("validator() is not defined for $_[0]") }
Subroutine validator: not called CC=1 SCAR=6.9
Pod documented
57  
58 sub _generate_class {
59 10 ▶ my ($parent, $class, $specification, $attrs) = @_;
Subroutine _generate_class: called CC=8 SCAR=22.2
60  
61 10 my $jv = JSON::Validator->new;
62 10 ▶ $jv->coerce($attrs->{coerce} // 'booleans,numbers,strings');
Condition50%1/2
$attrs->{"coerce"} // "booleans,numbers,strings"
l!l
010
MC/DC0%0/1
$attrs->{"coerce"} // "booleans,numbers,strings"
$attrs->{"coerce"}
Truth table50%1/2
A
A= $attrs->{"coerce"}
Aresult
11
00
63 10 ▶ $jv->store->ua->server->app($attrs->{app}) if $attrs->{app};
Branch: 1 branch
BranchTrueFalseTotal
if $attrs->{"app"} 5 5 10
64  
65 10 my $schema = $jv->schema($specification)->schema;
66 10 ▶ die "Invalid schema: $specification has the following errors:\n", join "\n", @{$schema->errors} if @{$schema->errors};
Branch: 1 branch
BranchTrueFalseTotal
if @{$schema->errors;} 0 10 10
67  
68 8 ▶ eval <<"HERE" or Carp::confess("package $class: $@");
Subroutine BEGIN: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
eval "package $class;\nuse Mojo::Base '${parent}';\n1;\n" or Carp::confess("package ${class}: $@") 0 10 10
69 package $class;
70 use Mojo::Base '$parent';
71 1;
72 HERE
73  
74 10 ▶ Mojo::Util::monkey_patch($class => validator => sub {$schema});
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine validator: called CC=1 SCAR=0.0
75 10 ▶ return unless $schema->can('routes'); # In case it is not an OpenAPI spec
Branch: 1 branch
BranchTrueFalseTotal
unless $schema->can("routes") 2 8 10
76  
77 8 for my $route ($schema->routes->each) {
78 13 my $operation_id = $route->{operation_id};
79  
80 13 ▶ unless ( $route->{operation_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($route->{"operation_id"}) 0 13 13
81 0 $operation_id = join '_', $route->{method}, $route->{path};
82 0 $operation_id =~ s|\{[^}]+\}|_|g;
83 0 $operation_id =~ s|[/-]|_|g;
84 0 $operation_id =~ s|__+|_|g;
85 }
86  
87 13 warn "[$class] Add method $operation_id() for $route->{method} $route->{path}\n" if DEBUG;
88 13 $class->_generate_method_bnb($operation_id => $route);
89 13 $class->_generate_method_p("${operation_id}_p" => $route);
90 }
91 }
92  
93 sub _generate_method_bnb {
94 13 ▶ my ($class, $method_name, $route) = @_;
Subroutine _generate_method_bnb: called CC=1 SCAR=0.1
95  
96 Mojo::Util::monkey_patch $class => $method_name => sub {
97 16 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Subroutine __ANON__: called CC=5 SCAR=16.7
Branch: 2 branches
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 1 15 16
ref $_[-1] eq 'CODE' ? : 1 0 1
98 16 my $self = shift;
99 16 my $tx = $self->_build_tx($route, @_);
100  
101 16 ▶ if ($tx->error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tx->error) 9 7 16
102 9 ▶ return $tx unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 8 1 9
103 1 ▶ Mojo::IOLoop->next_tick(sub { $self->$cb($tx) });
Subroutine __ANON__: called CC=1 SCAR=0.0
104 1 return $self;
105 }
106  
107 7 ▶ return $self->ua->start($tx) unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 7 0 7
108 0 ▶ $self->ua->start($tx, sub { $self->$cb($_[1]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 return $self;
110 13 };
111 }
112  
113 sub _generate_method_p {
114 13 ▶ my ($class, $method_name, $route) = @_;
Subroutine _generate_method_p: called CC=1 SCAR=0.5
115  
116 Mojo::Util::monkey_patch $class => $method_name => sub {
117 6 ▶ my $self = shift;
Subroutine __ANON__: called CC=5 SCAR=19.0
118 6 my $tx = $self->_build_tx($route, @_);
119  
120 6 ▶ return $self->ua->start_p($tx) unless my $err = $tx->error;
Branch: 2 branches
BranchTrueFalseTotal
unless my $err = $tx->error 5 1 6
unless my $err = $tx->error 0 1 1
121 1 ▶ return Mojo::Promise->new->reject($err->{message}) unless $err->{code};
Branch: 2 branches
BranchTrueFalseTotal
unless $err->{"code"} 0 1 1
unless $err->{"code"} 0 1 1
122 1 ▶ return Mojo::Promise->new->reject('WebSocket handshake failed') if $tx->req->is_handshake && !$tx->is_websocket;
Branch: 2 branches
BranchTrueFalseTotal
if $tx->req->is_handshake and not $tx->is_websocket 0 1 1
if $tx->req->is_handshake and not $tx->is_websocket 0 0 0
Condition33%1/3
$tx->req->is_handshake and not $tx->is_websocket
!ll&&!rl&&r
100
MC/DC0%0/2
$tx->req->is_handshake and not $tx->is_websocket
$tx->req->is_handshake not $tx->is_websocket
Truth table33%1/3
A and not(B)
A= $tx->req->is_handshakeB= not $tx->is_websocket
ABresult
0X0
100
111
123 1 return Mojo::Promise->new->resolve($tx);
124 13 };
125 }
126  
127 sub _build_tx {
128 23 ▶ my ($self, $route, $params, %content) = @_;
Subroutine _build_tx: called CC=4 SCAR=13.9
129 23 my $v = $self->validator;
130 23 my $url = $self->base_url->clone;
131 23 my ($tx, %headers);
132  
133 23 ▶ push @{$url->path}, map { local $_ = $_; s,\{([-\w]+)\},{$params->{$1}//''},ge; $_ } grep {length} split '/',
Condition100%2/2
$params->{$1} // ""
l!l
46
MC/DC100%1/1
$params->{$1} // ""
$params->{$1}
Truth table100%2/2
A
A= $params->{$1}
Aresult
11
00
134 23 $route->{path};
135  
136 my @errors = $self->validator->validate_request(
137 [@$route{qw(method path)}],
138 {
139 body => sub {
140 8 ▶ my ($name, $param) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
141  
142 8 ▶ if (exists $params->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{$name}) { } 4 4 8
143 4 $content{json} = $params->{$name};
144 }
145 else {
146 4 for ('body', sort keys %{$self->ua->transactor->generators}) {
147 14 ▶ next unless exists $content{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $content{$_} 12 2 14
148 2 $params->{$name} = $content{$_};
149 2 last;
150 }
151 }
152  
153 8 return {exists => $params->{$name}, value => $params->{$name}};
154 },
155 formData => sub {
156 8 ▶ my ($name, $param) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
157 8 my $value = _param_as_array($name => $params);
158 8 $content{form}{$name} = $params->{$name};
159 8 return {exists => !!@$value, value => $value};
160 },
161 header => sub {
162 2 ▶ my ($name, $param) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
163 2 my $value = _param_as_array($name => $params);
164 2 $headers{$name} = $value;
165 2 return {exists => !!@$value, value => $value};
166 },
167 path => sub {
168 10 ▶ my ($name, $param) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
169 10 return {exists => exists $params->{$name}, value => $params->{$name}};
170 },
171 query => sub {
172 18 ▶ my ($name, $param) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
173 18 my $value = _param_as_array($name => $params);
174 18 $url->query->param($name => _coerce_collection_format($value, $param));
175 18 return {exists => !!@$value, value => $value};
176 },
177 }
178 23 );
179  
180 23 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) { } 10 13 23
181 10 warn "[@{[ref $self]}] Validation for $route->{method} $url failed: @errors\n" if DEBUG;
182 10 $tx = Mojo::Transaction::HTTP->new;
183 10 $tx->req->method(uc $route->{method});
184 10 $tx->req->url($url);
185 10 $tx->res->headers->content_type('application/json');
186 10 $tx->res->body(Mojo::JSON::encode_json({errors => \@errors}));
187 10 $tx->res->code(400)->message($tx->res->default_message);
188 10 $tx->res->error({message => 'Invalid input', code => 400});
189 }
190 else {
191 13 warn "[@{[ref $self]}] Validation for $route->{method} $url was successful\n" if DEBUG;
192 13 ▶ $tx = $self->ua->build_tx($route->{method}, $url, \%headers, defined $content{body} ? $content{body} : %content);
Branch: 1 branch
BranchTrueFalseTotal
defined $content{'body'} ? : 0 13 13
193 }
194  
195 23 $tx->req->env->{operationId} = $route->{operation_id};
196 23 $self->emit(after_build_tx => $tx);
197  
198 23 return $tx;
199 }
200  
201 sub _coerce_collection_format {
202 18 ▶ my ($value, $param) = @_;
Subroutine _coerce_collection_format: called CC=9 SCAR=23.1
203 18 ▶ my $format = $param->{collectionFormat} || (+($param->{type} // '') eq 'array' ? 'csv' : '');
Branch: 1 branch
BranchTrueFalseTotal
($param->{'type'} // '') eq 'array' ? : 1 17 18
Condition60%3/5
$param->{'collectionFormat'} || (($param->{'type'} // '') eq 'array' ? 'csv' : '')
l!l&&r!l&&!r
0117
$param->{"type"} // ""
l!l
180
MC/DC0%0/2
$param->{'collectionFormat'} || (($param->{'type'} // '') eq 'array' ? 'csv' : '')
$param->{'collectionFormat'} $param->{"type"}
Truth table33%1/3
A || B
A= $param->{'collectionFormat'}B= $param->{"type"}
ABresult
1X1
011
000
204 18 ▶ return $value if !$format or $format eq 'multi';
Branch: 1 branch
BranchTrueFalseTotal
if not $format or $format eq "multi" 17 1 18
Condition66%2/3
not $format or $format eq "multi"
l!l&&r!l&&!r
1701
MC/DC50%1/2
not $format or $format eq "multi"
not $format $format eq "multi"
Truth table66%2/3
not(A) or B
A= not $formatB= $format eq "multi"
ABresult
1X1
011
000
205 1 ▶ return join "|", @$value if $format eq 'pipes';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "pipes" 0 1 1
206 1 ▶ return join " ", @$value if $format eq 'ssv';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "ssv" 0 1 1
207 1 ▶ return join "\t", @$value if $format eq 'tsv';
Branch: 1 branch
BranchTrueFalseTotal
if $format eq "tsv" 0 1 1
208 1 return join ",", @$value;
209 }
210  
211 sub _param_as_array {
212 28 ▶ my ($name, $params) = @_;
Subroutine _param_as_array: called CC=3 SCAR=11.0
213 28 ▶ return !exists $params->{$name} ? [] : ref $params->{$name} eq 'ARRAY' ? $params->{$name} : [$params->{$name}];
Branch: 2 branches
BranchTrueFalseTotal
!exists $params->{$name} ? : 17 11 28
ref $params->{$name} eq 'ARRAY' ? : 1 10 11
214 }
215  
216 sub _url_to_class {
217 24 ▶ my ($self, $package) = @_;
Subroutine _url_to_class: called CC=2 SCAR=7.0
218  
219 24 $package =~ s!^\w+?://!!;
220 24 $package =~ s!\W!_!g;
221 24 ▶ $package = Mojo::Util::md5_sum($package) if length $package > 110; # 110 is a bit random, but it cannot be too long
Branch: 1 branch
BranchTrueFalseTotal
if length $package > 110 0 24 24
222  
223 24 return "$self\::$package";
224 }
225  
226 9 1;
227  
228 =encoding utf8
229  
230 =head1 NAME
231  
232 OpenAPI::Client - A client for talking to an Open API powered server
233  
234 =head1 DESCRIPTION
235  
236 L<OpenAPI::Client> generates objects that can talk to Open API servers.
237 For each fresh OpenAPI contract given to the L</new> method, a custom subclass
238 is created from the Open API specification, with methods corresponding
239 to the operationIds. Parameters received by these methods are transformed
240 into HTTP requests. Input validation is performed, so invalid data won't be
241 sent to the server.
242  
243 Note that this implementation is currently EXPERIMENTAL, but unlikely to change!
244 Feedback is appreciated.
245  
246 =head1 SYNOPSIS
247  
248 =head2 Creating a client
249  
250 use OpenAPI::Client;
251 $client = OpenAPI::Client->new("file:///path/to/api.json");
252  
253 The specification given to L</new> must point to a valid OpenAPI document.
254 Several syntax variants are admitted -- see the L</new> method.
255  
256 =head2 Open API specification
257  
258 The OpenAPI document can be in OpenAPI version v2.x or v3.x, and it can be in either JSON or YAML
259 format. Example:
260  
261 openapi: 3.0.1
262 info:
263 title: Swagger Petstore
264 version: 1.0.0
265 servers:
266 - url: http://petstore.swagger.io/v1
267 paths:
268 /pets:
269 get:
270 operationId: listPets
271 ...
272  
273  
274 The url specified in the OpenAPI document can be altered at any time, if you need to send data to another
275 custom endpoint -- see the L</base_url> method.
276  
277  
278 =head2 Client
279  
280 The OpenAPI API specification will be used to generate a sub-class of
281 L<OpenAPI::Client> with additional methods corresponding to "operationId" inside of each path definition :
282  
283 # Blocking
284 $tx = $client->listPets;
285  
286 # Non-blocking
287 $client = $client->listPets(sub { my ($client, $tx) = @_; });
288  
289 # Promises
290 $promise = $client->listPets_p->then(sub { my $tx = shift });
291  
292 # With parameters
293 $tx = $client->listPets({limit => 10});
294  
295 See L<Mojo::Transaction> for more information about what you can do with the
296 C<$tx> object. Often you just want something like this:
297  
298 # Check for errors
299 die $tx->error->{message} if $tx->error;
300  
301 # Extract data from the JSON responses
302 say $tx->res->json->{pets}[0]{name};
303  
304 Check out L<Mojo::Transaction/error>, L<Mojo::Transaction/req> and
305 L<Mojo::Transaction/res> for some of the most used methods in that class.
306  
307 =head1 CUSTOMIZATION
308  
309 =head2 Custom server URL
310  
311 If you want to request another server than the one specified in the Open
312 API document, you can change the L</base_url>:
313  
314 # Pass on a Mojo::URL object to the constructor
315 $base_url = Mojo::URL->new("http://example.com");
316 $client1 = OpenAPI::Client->new("file:///path/to/api.json", base_url => $base_url);
317  
318 # A plain string will be converted to a Mojo::URL object
319 $client2 = OpenAPI::Client->new("file:///path/to/api.json", base_url => "http://example.com");
320  
321 # Change the base_url after the client has been created
322 $client3 = OpenAPI::Client->new("file:///path/to/api.json");
323 $client3->base_url->host("other.example.com");
324  
325 =head2 Custom content
326  
327 You can send XML or any format you like, but this requires you to add a new
328 "generator":
329  
330 use Your::XML::Library "to_xml";
331 $client->ua->transactor->add_generator(xml => sub {
332 my ($t, $tx, $data) = @_;
333 $tx->req->body(to_xml $data);
334 return $tx;
335 });
336  
337 $client->addHero({}, xml => {name => "Supergirl"});
338  
339 See L<Mojo::UserAgent::Transactor> for more details.
340  
341 =head1 EVENTS
342  
343 =head2 after_build_tx
344  
345 $client->on(after_build_tx => sub { my ($client, $tx) = @_ })
346  
347 This event is emitted after a L<Mojo::UserAgent::Transactor> object has been
348 built, just before it is passed on to the L</ua>. Note that all validation has
349 already been run, so altering the C<$tx> too much might cause an invalid
350 request on the server side.
351  
352 A special L<Mojo::Message::Request/env> variable will be set, to reference the
353 operationId:
354  
355 $tx->req->env->{operationId};
356  
357 Note that this usage of C<env()> is currently EXPERIMENTAL:
358  
359 =head1 ATTRIBUTES
360  
361 =head2 base_url
362  
363 $base_url = $client->base_url;
364  
365 Returns a L<Mojo::URL> object with the base URL to the API. The default value
366 comes from C<schemes>, C<basePath> and C<host> in the OpenAPI v2 specification
367 or from C<servers> in the OpenAPI v3 specification.
368  
369 =head2 ua
370  
371 $ua = $client->ua;
372  
373 Returns a L<Mojo::UserAgent> object which is used to execute requests.
374  
375 =head1 CLASS METHODS
376  
377 =head2 new
378  
379 $client = OpenAPI::Client->new($specification, \%attributes);
380 $client = OpenAPI::Client->new($specification, %attributes);
381  
382 Returns an object of a dynamic subclass of C<OpenAPI::Client>,
383 with methods generated from the OpenAPI specification located at C<$specification>.
384 Such subclasses are cached, so several invocations of C<new()> with the same
385 C<$specification> URL will result in several instances of the same subclass.
386  
387 The C<$specification> argument can accept various syntaxes -- see L<JSON::Validator/schema>.
388  
389 Extra C<%attributes> can be:
390  
391 =over 2
392  
393 =item app
394  
395 Can be used to run against a local L<Mojolicious> instance instead of issuing real
396 HTTP calls to a remote server.
397  
398 =item coerce
399  
400 See L<JSON::Validator/coerce>. Default to "booleans,numbers,strings".
401  
402 =back
403  
404  
405 =head1 INSTANCE METHODS
406  
407 =head2 call
408  
409 $tx = $client->call($operationId => \%params, %content);
410 $client = $client->call($operationId => \%params, %content, sub { my ($client, $tx) = @_; });
411  
412 Used to call an C<$operationId> that is not a proper Perl method name, such as
413 "list pets" instead of "listPets", or to check if an C<$operationId> is supported.
414 Unsupported operationIds throw an exception matching text "No such operationId".
415  
416 C<$operationId> is the name of the resource defined in the
417 L<OpenAPI specification|https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#operation-object>.
418  
419 C<$params> is optional, but must be a hash ref, where the keys should match a
420 named parameter in the L<OpenAPI specification|https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameter-object>.
421  
422 C<%content> is used for the body of the request, where the key needs to be
423 either "body" or a matching L<Mojo::UserAgent::Transactor/generators>. Example:
424  
425 $client->addHero({}, body => "Some data");
426 $client->addHero({}, json => {name => "Supergirl"});
427  
428 Like in L<Mojo::UserAgent>, an additional coderef argument can be supplied in last position.
429 In that case the call is asynchronous, and the coderef will be called as a continuation
430 callback, with two arguments C<$client> (the current openAPI client) and C<$tx>
431 (a L<Mojo::Transaction> object). See L<Mojolicious::Guides::Cookbook/Non-blocking> for details.
432  
433 =head2 call_p
434  
435 $promise = $client->call_p($operationId => $params, %content);
436 $promise->then(sub { my $tx = shift });
437  
438 As L</call> above, but returns a L<Mojo::Promise> object.
439  
440 =head2 validator
441  
442 $validator = $client->validator;
443 $validator = $class->validator;
444  
445 Returns the L<JSON::Validator::Schema> object associated with the current generated class.
446 Depending on the openAPI specification, this object will belong to the
447 L<OpenAPIv2|JSON::Validator::Schema::OpenAPIv2> or
448 L<OpenAPIv3|JSON::Validator::Schema::OpenAPIv3> subclass.
449 This object global to the class, so changing it will affect all instances returned by L</new>.
450  
451 =head1 COPYRIGHT AND LICENSE
452  
453 Copyright (C) 2017-2021, Jan Henning Thorsen
454  
455 This program is free software, you can redistribute it and/or modify it under
456 the terms of the Artistic License version 2.0.
457  
458 =head1 AUTHORS
459  
460 =head2 Project Founder
461  
462 Jan Henning Thorsen - C<jhthorsen@cpan.org>
463  
464 =head2 Contributors
465  
466 =over 2
467  
468  
469 =item * Clive Holloway <clhollow@estee.com>
470  
471 =item * Ed J <mohawk2@users.noreply.github.com>
472  
473 =item * Jan Henning Thorsen <jan.henning@thorsen.pm>
474  
475 =item * Jan Henning Thorsen <jhthorsen@cpan.org>
476  
477 =item * Mohammad S Anwar <mohammad.anwar@yahoo.com>
478  
479 =item * Reneeb <info@perl-services.de>
480  
481 =item * Roy Storey <kiwiroy@users.noreply.github.com>
482  
483 =item * Veesh Goldman <rabbiveesh@gmail.com>
484  
485 =item * Laurent Dami <dami@cpan.org>
486  
487 =back
488  
489 =cut
« Mojolicious/Command/openapi.pm ↑ Summary