| 1 | package OpenAPI::PerlGenerator::Template::Mojo 0.02; | ||
| 2 | 2 | ▶ | use 5.020; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | |||
| 4 | =head1 NAME | ||
| 5 | |||
| 6 | OpenAPI::PerlGenerator::Template::Mojo - Mojolicious templates for OpenAPI clients | ||
| 7 | |||
| 8 | =head1 SYNOPSIS | ||
| 9 | |||
| 10 | # override the 'foo' template | ||
| 11 | $OpenAPI::PerlGenerator::Template::Mojo::template{'foo'} = <<__MY_TEMPLATE__; | ||
| 12 | bar | ||
| 13 | __MY_TEMPLATE__ | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | 2 | our %template; | |
| 18 | |||
| 19 | 2 | $template{required_parameters} = <<'__REQUIRED_PARAMETERS__'; | |
| 20 | %# Check that we received all required parameters: | ||
| 21 | % if( my $p = $elt->{parameters}) { | ||
| 22 | % my @required = grep { $_->{required} } $elt->{parameters}->@*; | ||
| 23 | % if( @required ) { | ||
| 24 | % for my $p (@required) { | ||
| 25 | croak "Missing required parameter '<%= $p->{name} %>'" | ||
| 26 | unless exists $options{ '<%= $p->{name} %>' }; | ||
| 27 | % } | ||
| 28 | |||
| 29 | % } | ||
| 30 | % } # parameter-required | ||
| 31 | __REQUIRED_PARAMETERS__ | ||
| 32 | |||
| 33 | 2 | $template{path_parameters} = <<'__PATH_PARAMETERS__'; | |
| 34 | my $template = URI::Template->new( '<%= $method->{path} %>' ); | ||
| 35 | my $path = $template->process( | ||
| 36 | % for my $p ($params->@*) { | ||
| 37 | % if( $p->{required} ) { | ||
| 38 | '<%= $p->{name} %>' => delete $options{'<%= $p->{name} %>'}, | ||
| 39 | % } else { | ||
| 40 | maybe '<%= $p->{name} %>' => delete $options{'<%= $p->{name} %>'}, | ||
| 41 | % } | ||
| 42 | % } | ||
| 43 | ); | ||
| 44 | __PATH_PARAMETERS__ | ||
| 45 | |||
| 46 | 2 | $template{generate_request_body} = <<'__REQUEST_BODY__'; | |
| 47 | % for my $ct (sort keys $content->%*) { | ||
| 48 | % if( exists $content->{$ct}->{schema}) { | ||
| 49 | % if( $content->{$ct}->{schema}->{type} eq 'string' ) { | ||
| 50 | my $body = delete $options{ body } // ''; | ||
| 51 | % } else { | ||
| 52 | my $request = <%= $prefix %>::<%= $content->{$ct}->{schema}->{name} %>->new( \%options ); | ||
| 53 | % } | ||
| 54 | % } elsif( $ct eq 'multipart/form-data' ) { | ||
| 55 | % # nothing to do | ||
| 56 | % } else { | ||
| 57 | # don't know how to handle content type <%= $ct %>... | ||
| 58 | % } | ||
| 59 | % } | ||
| 60 | __REQUEST_BODY__ | ||
| 61 | |||
| 62 | 2 | $template{inflated_response} = <<'__INFLATED_RESPONSE__'; | |
| 63 | % if( $type->{name} ) { | ||
| 64 | <%= $prefix %>::<%= $type->{name} %>->new(<%= $argname %>), | ||
| 65 | % } elsif( $type->{type} and $type->{type} eq 'array') { | ||
| 66 | %# use Data::Dumper; warn Dumper $type; | ||
| 67 | [ map { <%= include('inflated_response', { type => $type->{items}, prefix => $prefix, argname => '$_' }) %> } $payload->@* ], | ||
| 68 | % } else { | ||
| 69 | <%= $argname %> | ||
| 70 | % } | ||
| 71 | __INFLATED_RESPONSE__ | ||
| 72 | |||
| 73 | 2 | $template{streaming_response} = <<'__STREAMING_RESPONSE__'; | |
| 74 | use Future::Queue; | ||
| 75 | my $res = Future::Queue->new( prototype => 'Future::Mojo' ); | ||
| 76 | our @store; # we should use ->retain() instead | ||
| 77 | push @store, $r1->then( sub( $tx ) { | ||
| 78 | my $resp = $tx->res; | ||
| 79 | # Should we validate using OpenAPI::Modern here?! | ||
| 80 | %# Should this be its own subroutine instead?! | ||
| 81 | % for my $code (sort keys $elt->{responses}->%*) { # response code s | ||
| 82 | % my $info = $elt->{responses}->{ $code }; | ||
| 83 | %# XXX if streaming, we need to handle a non-streaming error response! | ||
| 84 | <%= elsif_chain($name) %>( $resp->code <%= openapi_http_code_match( $code ) %> ) { | ||
| 85 | % if( $info->{description} =~ /\S/ ) { | ||
| 86 | # <%= single_line( $info->{description} ) %> | ||
| 87 | % } | ||
| 88 | % # Check the content type | ||
| 89 | % # Will we always have a content type?! | ||
| 90 | % if( keys $info->{content}->%* ) { | ||
| 91 | % for my $ct (sort keys $info->{content}->%*) { | ||
| 92 | my $ct = $resp->headers->content_type; | ||
| 93 | return unless $ct; | ||
| 94 | $ct =~ s/;\s+.*//; | ||
| 95 | if( $ct eq '<%= $ct %>' ) { | ||
| 96 | # we only handle ndjson currently | ||
| 97 | my $handled_offset = 0; | ||
| 98 | $resp->on(progress => sub($msg,@) { | ||
| 99 | my $fresh = substr( $msg->body, $handled_offset ); | ||
| 100 | my $body = $msg->body; | ||
| 101 | $body =~ s/[^\r\n]+\z//; # Strip any unfinished line | ||
| 102 | $handled_offset = length $body; | ||
| 103 | my @lines = split /\n/, $fresh; | ||
| 104 | for (@lines) { | ||
| 105 | my $payload = decode_json( $_ ); | ||
| 106 | $res->push( | ||
| 107 | % my $type = $info->{content}->{$ct}->{schema}; | ||
| 108 | <%= include('inflated_response', { type => $type, prefix => $prefix, argname => '$payload' } ) %> | ||
| 109 | ); | ||
| 110 | }; | ||
| 111 | if( $msg->{state} eq 'finished' ) { | ||
| 112 | $res->finish(); | ||
| 113 | } | ||
| 114 | }); | ||
| 115 | } | ||
| 116 | % } | ||
| 117 | % } else { # we don't know how to handle this, so pass $res # known content types? | ||
| 118 | %# XXX should we always use ->done or should we use ->fail for 4xx and 5xx ?! | ||
| 119 | return Future::Mojo->done($resp); | ||
| 120 | % } | ||
| 121 | % } | ||
| 122 | } else { | ||
| 123 | # An unknown/unhandled response, likely an error | ||
| 124 | return Future::Mojo->fail($resp); | ||
| 125 | } | ||
| 126 | }); | ||
| 127 | |||
| 128 | my $_tx; | ||
| 129 | $tx->res->once( progress => sub($msg, @) { | ||
| 130 | $r1->resolve( $tx ); | ||
| 131 | undef $_tx; | ||
| 132 | undef $r1; | ||
| 133 | }); | ||
| 134 | $_tx = $self->ua->start_p($tx); | ||
| 135 | __STREAMING_RESPONSE__ | ||
| 136 | |||
| 137 | 2 | $template{synchronous_response} = <<'__SYNCHRONOUS_RESPONSE__'; | |
| 138 | my $res = $r1->then( sub( $tx ) { | ||
| 139 | my $resp = $tx->res; | ||
| 140 | # Should we validate using OpenAPI::Modern here?! | ||
| 141 | %# Should this be its own subroutine instead?! | ||
| 142 | % my $first_code = 1; | ||
| 143 | % for my $code (sort keys $elt->{responses}->%*) { # response code s | ||
| 144 | % my $info = $elt->{responses}->{ $code }; | ||
| 145 | <%= elsif_chain($name) %>( $resp->code <%= openapi_http_code_match( $code ) %> ) { | ||
| 146 | % if( $info->{description} =~ /\S/ ) { | ||
| 147 | # <%= single_line( $info->{description} ) %> | ||
| 148 | % } | ||
| 149 | % # Check the content type | ||
| 150 | % # Will we always have a content type?! | ||
| 151 | % if( keys $info->{content}->%* ) { | ||
| 152 | % for my $ct (sort keys $info->{content}->%*) { | ||
| 153 | my $ct = $resp->headers->content_type; | ||
| 154 | $ct =~ s/;\s+.*//; | ||
| 155 | if( $ct eq '<%= $ct %>' ) { | ||
| 156 | %# These handlers for content types should come from templates? Or maybe | ||
| 157 | %# from a subroutine?! | ||
| 158 | % if( $ct eq 'application/json' ) { | ||
| 159 | my $payload = $resp->json(); | ||
| 160 | % } elsif( $ct eq 'application/x-ndjson' ) { | ||
| 161 | # code missing to hack up ndjson into hashes for a non-streaming response | ||
| 162 | my $payload = $resp->body(); | ||
| 163 | % } else { | ||
| 164 | my $payload = $resp->body(); | ||
| 165 | % } | ||
| 166 | return Future::Mojo->done( | ||
| 167 | % my $type = $info->{content}->{$ct}->{schema}; | ||
| 168 | <%= include('inflated_response', { type => $type, prefix => $prefix, argname => '$payload' } ) %> | ||
| 169 | ); | ||
| 170 | } | ||
| 171 | % } | ||
| 172 | % } else { # we don't know how to handle this, so pass $res # known content types? | ||
| 173 | return Future::Mojo->done($resp); | ||
| 174 | % } | ||
| 175 | % } | ||
| 176 | } else { | ||
| 177 | # An unknown/unhandled response, likely an error | ||
| 178 | return Future::Mojo->fail($resp); | ||
| 179 | } | ||
| 180 | }); | ||
| 181 | |||
| 182 | # Start our transaction | ||
| 183 | $tx = $self->ua->start_p($tx)->then(sub($tx) { | ||
| 184 | $r1->resolve( $tx ); | ||
| 185 | undef $r1; | ||
| 186 | })->catch(sub($err) { | ||
| 187 | $r1->fail( $err => $tx ); | ||
| 188 | undef $r1; | ||
| 189 | }); | ||
| 190 | __SYNCHRONOUS_RESPONSE__ | ||
| 191 | |||
| 192 | 2 | $template{object} = <<'__OBJECT__'; | |
| 193 | % my @subclasses; | ||
| 194 | % my @included_types; | ||
| 195 | % if( exists $elt->{allOf}) { | ||
| 196 | % for my $item ($elt->{allOf}->@*) { | ||
| 197 | % if( $item->{name} ) { | ||
| 198 | % push @subclasses, $item; | ||
| 199 | % } else { | ||
| 200 | % push @included_types, $item; | ||
| 201 | % } | ||
| 202 | % } | ||
| 203 | % } else { | ||
| 204 | % push @included_types, $elt; | ||
| 205 | % } | ||
| 206 | % | ||
| 207 | package <%= $prefix %>::<%= $name %> 0.01; | ||
| 208 | # DO NOT EDIT! This is an autogenerated file. | ||
| 209 | use 5.020; | ||
| 210 | use Moo 2; | ||
| 211 | use experimental 'signatures'; | ||
| 212 | use Types::Standard qw(Str Bool Num Int Object ArrayRef); | ||
| 213 | use MooX::TypeTiny; | ||
| 214 | |||
| 215 | =head1 NAME | ||
| 216 | |||
| 217 | <%= $prefix %>::<%= $name %> - | ||
| 218 | |||
| 219 | =head1 SYNOPSIS | ||
| 220 | |||
| 221 | my $obj = <%= $prefix %>::<%= $name %>->new(); | ||
| 222 | ... | ||
| 223 | |||
| 224 | =cut | ||
| 225 | |||
| 226 | sub as_hash( $self ) { | ||
| 227 | return { $self->%* } | ||
| 228 | } | ||
| 229 | |||
| 230 | % if( @subclasses ) { | ||
| 231 | % for my $item (@subclasses) { | ||
| 232 | extends '<%= $prefix %>::<%= $item->{name} %>'; | ||
| 233 | % } | ||
| 234 | |||
| 235 | % } | ||
| 236 | =head1 PROPERTIES | ||
| 237 | |||
| 238 | % for my $t (@included_types) { | ||
| 239 | % for my $prop (sort keys $t->{properties}->%*) { | ||
| 240 | % my $p = $t->{properties}->{$prop}; | ||
| 241 | =head2 C<< <%= property_name( $prop ) %> >> | ||
| 242 | |||
| 243 | % if( $p->{description} and $p->{description} =~ /\S/ ) { | ||
| 244 | <%= $p->{description} =~ s/\s*$//r %> | ||
| 245 | |||
| 246 | % } | ||
| 247 | =cut | ||
| 248 | |||
| 249 | has '<%= property_name( $prop ) %>' => ( | ||
| 250 | is => 'ro', | ||
| 251 | % my $type = map_type( $p ); | ||
| 252 | % if( $type ) { | ||
| 253 | isa => <%= $type %>, | ||
| 254 | % }; | ||
| 255 | % if( grep {$_ eq $prop} $elt->{required}->@*) { | ||
| 256 | required => 1, | ||
| 257 | % } | ||
| 258 | ); | ||
| 259 | |||
| 260 | % } | ||
| 261 | % } | ||
| 262 | |||
| 263 | 1; | ||
| 264 | __OBJECT__ | ||
| 265 | |||
| 266 | 2 | $template{return_types} = <<'__RETURN_TYPES__'; | |
| 267 | % for my $code (sort keys $elt->{responses}->%*) { | ||
| 268 | % my $info = $elt->{responses}->{ $code }; | ||
| 269 | % if( my $content = $info->{content} ) { | ||
| 270 | % for my $ct (sort keys $content->%*) { | ||
| 271 | % if( $content->{$ct}->{schema}) { | ||
| 272 | % my $descriptor = 'a'; | ||
| 273 | % my $class; | ||
| 274 | % if( $content->{$ct}->{schema}->{type} and $content->{$ct}->{schema}->{type} eq 'array' ) { | ||
| 275 | % $descriptor = 'an array of'; | ||
| 276 | % $class = join "::", $prefix, $content->{$ct}->{schema}->{items}->{name}; | ||
| 277 | % } elsif( $content->{$ct}->{schema}->{name}) { | ||
| 278 | % $class = join "::", $prefix, $content->{$ct}->{schema}->{name}; | ||
| 279 | % } else { | ||
| 280 | % $class = $content->{$ct}->{schema}->{type}; | ||
| 281 | % } | ||
| 282 | Returns <%= $descriptor %> L<< <%= $class %> >>. | ||
| 283 | % } | ||
| 284 | % } | ||
| 285 | % } | ||
| 286 | % } | ||
| 287 | __RETURN_TYPES__ | ||
| 288 | |||
| 289 | 2 | $template{ build_request } = <<'__BUILD_REQUEST__'; | |
| 290 | sub _build_<%= $method->{name} %>_request( $self, %options ) { | ||
| 291 | <%= include( 'required_parameters', { elt => $elt }); =%> | ||
| 292 | %# | ||
| 293 | my $method = '<%= uc $method->{http_method} %>'; | ||
| 294 | %# Output the path parameters | ||
| 295 | % if( my $params = delete $parameters->{ path }) { | ||
| 296 | <%= include( 'path_parameters', { method => $method, params => $params }); =%> | ||
| 297 | % } else { | ||
| 298 | my $path = '<%= $method->{path} %>'; | ||
| 299 | % } # path parameters | ||
| 300 | my $url = Mojo::URL->new( $self->server . $path ); | ||
| 301 | |||
| 302 | %#------ | ||
| 303 | %# Generate the (URL) parameters | ||
| 304 | %# This must happen before the remaining options are passed into an object | ||
| 305 | %# Output the query parameters | ||
| 306 | % if( my $params = delete $parameters->{ query }) { | ||
| 307 | $url->query->merge( | ||
| 308 | % for my $p ($params->@*) { | ||
| 309 | % if( $p->{required} ) { | ||
| 310 | '<%= $p->{name} %>' => delete $options{'<%= $p->{name} %>'}, | ||
| 311 | % } else { | ||
| 312 | maybe '<%= $p->{name} %>' => delete $options{'<%= $p->{name} %>'}, | ||
| 313 | % } | ||
| 314 | % } | ||
| 315 | ); | ||
| 316 | |||
| 317 | % }; | ||
| 318 | %#------ | ||
| 319 | %# Generate the header parameters | ||
| 320 | % my $custom_headers; | ||
| 321 | % if( my $params = delete $parameters->{ header }) { # header parameters | ||
| 322 | % $custom_headers = []; | ||
| 323 | % for my $p ($params->@*) { | ||
| 324 | % if( $p->{required} ) { | ||
| 325 | % push @$custom_headers, qq{ '$p->{name}' => delete \$options{'$p->{name}'}}; | ||
| 326 | % } else { | ||
| 327 | % push @$custom_headers, qq{maybe '$p->{name}' => delete \$options{'$p->{name}'}}; | ||
| 328 | % } | ||
| 329 | % } | ||
| 330 | % }; # header parameters | ||
| 331 | %#------ | ||
| 332 | %# Output any parameter locations we didn't handle as comment | ||
| 333 | % for (sort keys $parameters->%* ) { | ||
| 334 | % for my $p ($parameters->{$_}->@*) { | ||
| 335 | # unhandled <%= $p->{in} %> parameter <%= $p->{name} %>; | ||
| 336 | % } | ||
| 337 | % } # parameter-in | ||
| 338 | % | ||
| 339 | % my $is_json; | ||
| 340 | % my $content_type; | ||
| 341 | % my $has_body = exists $elt->{requestBody}; | ||
| 342 | % if( $has_body ) { | ||
| 343 | %# We assume we will only ever have one content type for the request we send: | ||
| 344 | % ($content_type) = sort keys $elt->{requestBody}->{content}->%*; | ||
| 345 | % $is_json = $ct && $ct eq 'application/json'; | ||
| 346 | <%= include('generate_request_body', { | ||
| 347 | content => $elt->{requestBody}->{content}, | ||
| 348 | prefix => $prefix, | ||
| 349 | is_json => $is_json, | ||
| 350 | } ); =%> | ||
| 351 | % } | ||
| 352 | % | ||
| 353 | my $tx = $self->ua->build_tx( | ||
| 354 | $method => $url, | ||
| 355 | { | ||
| 356 | % my $known_response_types = join ",", openapi_response_content_types($elt); | ||
| 357 | % if( $known_response_types ) { | ||
| 358 | 'Accept' => '<%= $known_response_types %>', | ||
| 359 | % } | ||
| 360 | % if( $content_type ) { | ||
| 361 | "Content-Type" => '<%= $content_type %>', | ||
| 362 | % } | ||
| 363 | % if( $custom_headers ) { | ||
| 364 | % for my $h (@$custom_headers) { | ||
| 365 | <%= $h %> | ||
| 366 | % } | ||
| 367 | % } | ||
| 368 | %#------ | ||
| 369 | %# Generate the header parameters | ||
| 370 | % if( my $params = delete $parameters->{ header }) { # header parameters | ||
| 371 | % for my $p ($params->@*) { | ||
| 372 | % if( $p->{required} ) { | ||
| 373 | '<%= $p->{name} %>' => delete $options{'<%= $p->{name} %>'}, | ||
| 374 | % } else { | ||
| 375 | maybe '<%= $p->{name} %>' => delete $options{'<%= $p->{name} %>'}, | ||
| 376 | % } | ||
| 377 | % } | ||
| 378 | % }; # header parameters | ||
| 379 | } | ||
| 380 | % if( ! $has_body ) { | ||
| 381 | %# nothing to do | ||
| 382 | % } elsif( $is_json ) { | ||
| 383 | => json => $request->as_hash, | ||
| 384 | % } elsif( $content_type and $content_type eq 'multipart/form-data' ) { | ||
| 385 | => form => $request->as_hash, | ||
| 386 | % } elsif( $content_type and $content_type eq 'application/octet-stream' ) { | ||
| 387 | => $body, | ||
| 388 | % } else { | ||
| 389 | # XXX Need to fill the body | ||
| 390 | # => $body, | ||
| 391 | % } | ||
| 392 | ); | ||
| 393 | |||
| 394 | return $tx | ||
| 395 | } | ||
| 396 | __BUILD_REQUEST__ | ||
| 397 | |||
| 398 | 2 | $template{client_implementation} = <<'__CLIENT_IMPLEMENTATION__'; | |
| 399 | package <%= $prefix %>::<%= $name %> 0.01; | ||
| 400 | # DO NOT EDIT! This is an autogenerated file. | ||
| 401 | use 5.020; | ||
| 402 | use Moo 2; | ||
| 403 | use experimental 'signatures'; | ||
| 404 | use PerlX::Maybe; | ||
| 405 | use Carp 'croak'; | ||
| 406 | |||
| 407 | # These should go into a ::Role | ||
| 408 | use YAML::PP; | ||
| 409 | use Mojo::UserAgent; | ||
| 410 | use Mojo::URL; | ||
| 411 | use Mojo::JSON 'encode_json', 'decode_json'; | ||
| 412 | use OpenAPI::Modern; | ||
| 413 | |||
| 414 | use Future::Mojo; | ||
| 415 | |||
| 416 | % my @submodules = openapi_submodules($schema); | ||
| 417 | % while (my($submodule,$info) = splice( @submodules, 0, 2 )) { | ||
| 418 | % if( $info->{type} and $info->{type} eq 'object' ) { | ||
| 419 | use <%= $prefix %>::<%= $submodule %>; | ||
| 420 | % } | ||
| 421 | % } | ||
| 422 | |||
| 423 | =head1 SYNOPSIS | ||
| 424 | |||
| 425 | =head1 PROPERTIES | ||
| 426 | |||
| 427 | =head2 B<< openapi >> | ||
| 428 | |||
| 429 | =head2 B<< ua >> | ||
| 430 | |||
| 431 | =head2 B<< server >> | ||
| 432 | |||
| 433 | =cut | ||
| 434 | |||
| 435 | # XXX this should be more configurable, and potentially you don't want validation?! | ||
| 436 | has 'schema' => ( | ||
| 437 | is => 'lazy', | ||
| 438 | default => sub { | ||
| 439 | YAML::PP->new( boolean => 'JSON::PP' )->load_file( 'ollama/ollama-curated.yaml' ); | ||
| 440 | }, | ||
| 441 | ); | ||
| 442 | |||
| 443 | has 'openapi' => ( | ||
| 444 | is => 'lazy', | ||
| 445 | default => sub { OpenAPI::Modern->new( openapi_schema => $_[0]->schema, openapi_uri => '/api' )}, | ||
| 446 | ); | ||
| 447 | |||
| 448 | # The HTTP stuff should go into a ::Role I guess | ||
| 449 | has 'ua' => ( | ||
| 450 | is => 'lazy', | ||
| 451 | default => sub { Mojo::UserAgent->new }, | ||
| 452 | ); | ||
| 453 | |||
| 454 | has 'server' => ( | ||
| 455 | is => 'lazy', | ||
| 456 | default => sub { 'http://localhost:11434/api' }, # XXX pull from OpenAPI file instead | ||
| 457 | ); | ||
| 458 | |||
| 459 | =head1 METHODS | ||
| 460 | |||
| 461 | % for my $method ($methods->@*) { | ||
| 462 | % my $elt = $method->{elt}; | ||
| 463 | % my $is_streaming = exists $elt->{responses}->{200} | ||
| 464 | % && $elt->{responses}->{200}->{content} | ||
| 465 | % && [keys $elt->{responses}->{200}->{content}->%*]->[0] eq 'application/x-ndjson' | ||
| 466 | % ; | ||
| 467 | % | ||
| 468 | %# Sort the parameters according to where they go | ||
| 469 | % my %parameters; | ||
| 470 | % if( my $p = $elt->{parameters}) { | ||
| 471 | % for my $p ($elt->{parameters}->@*) { | ||
| 472 | % $parameters{ $p->{in} } //= []; | ||
| 473 | % push $parameters{ $p->{in} }->@*, $p; | ||
| 474 | % } | ||
| 475 | % } | ||
| 476 | % | ||
| 477 | =head2 C<< <%= $method->{name} %> >> | ||
| 478 | |||
| 479 | %# Generate the example invocation | ||
| 480 | % if( $is_streaming ) { | ||
| 481 | use Future::Utils 'repeat'; | ||
| 482 | my $responses = $client-><%= $method->{name} %>(); | ||
| 483 | repeat { | ||
| 484 | my ($res) = $responses->shift; | ||
| 485 | if( $res ) { | ||
| 486 | my $str = $res->get; | ||
| 487 | say $str; | ||
| 488 | } | ||
| 489 | |||
| 490 | Future::Mojo->done( defined $res ); | ||
| 491 | } until => sub($done) { $done->get }; | ||
| 492 | % } else { | ||
| 493 | my $res = $client-><%= $method->{name} %>()->get; | ||
| 494 | % } | ||
| 495 | |||
| 496 | % if( $elt->{summary} and $elt->{summary} =~ /\S/ ) { | ||
| 497 | <%= markdown_to_pod( $elt->{summary} =~ s/\s*$//r ) %> | ||
| 498 | |||
| 499 | %} | ||
| 500 | %# List/add the invocation parameters | ||
| 501 | % my $parameters = $elt->{parameters}; | ||
| 502 | % if( $parameters ) { # parameters | ||
| 503 | =head3 Parameters | ||
| 504 | |||
| 505 | =over 4 | ||
| 506 | |||
| 507 | % for my $p ($parameters->@* ) { | ||
| 508 | =item B<< <%= $p->{name} %> >> | ||
| 509 | |||
| 510 | % if( $p->{description} =~ /\S/ ) { | ||
| 511 | <%= markdown_to_pod( $p->{description} =~ s/\s*$//r ) %> | ||
| 512 | |||
| 513 | % } | ||
| 514 | % if( $p->{default}) { | ||
| 515 | Defaults to C<< <%= $p->{default} =%> >> | ||
| 516 | |||
| 517 | % } | ||
| 518 | % } | ||
| 519 | =back | ||
| 520 | |||
| 521 | % } # parameters | ||
| 522 | %# | ||
| 523 | %# Add the body/schema parameters: | ||
| 524 | % (my $ct) = exists $elt->{requestBody} ? keys $elt->{requestBody}->{content}->%* : (); | ||
| 525 | % my $type; | ||
| 526 | % if( $ct ) { | ||
| 527 | % $type = $ct && $elt->{requestBody}->{content}->{$ct}->{schema}; | ||
| 528 | % }; | ||
| 529 | % if( $type ) { | ||
| 530 | % my @properties = (sort keys $type->{properties}->%*); | ||
| 531 | % if( @properties ) { | ||
| 532 | |||
| 533 | =head3 Options | ||
| 534 | |||
| 535 | =over 4 | ||
| 536 | |||
| 537 | % for my $prop (@properties) { | ||
| 538 | % my $p = $type->{properties}->{$prop}; | ||
| 539 | =item C<< <%= property_name( $prop ) %> >> | ||
| 540 | |||
| 541 | % if( $p->{description} ) { | ||
| 542 | <%= markdown_to_pod( $p->{description} =~ s/\s*$//r ) %> | ||
| 543 | |||
| 544 | % } | ||
| 545 | % } | ||
| 546 | =back | ||
| 547 | % } | ||
| 548 | % } | ||
| 549 | |||
| 550 | %=include('return_types', { prefix => $prefix, elt => $elt }); | ||
| 551 | =cut | ||
| 552 | |||
| 553 | <%= include( 'build_request', { method => $method, parameters => \%parameters, elt => $elt, ct => $ct, prefix => $prefix, } ); %> | ||
| 554 | |||
| 555 | sub <%= $method->{name} %>( $self, %options ) { | ||
| 556 | my $tx = $self->_build_<%= $method->{name} %>_request(%options); | ||
| 557 | |||
| 558 | # validate our request while developing | ||
| 559 | my $results = $self->openapi->validate_request($tx->req); | ||
| 560 | if( $results->{error}) { | ||
| 561 | say $results; | ||
| 562 | say $tx->req->to_string; | ||
| 563 | }; | ||
| 564 | |||
| 565 | %# We want to handle both here, streaming (ndjson) and plain responses | ||
| 566 | %# Plain responses are easy, but for streamed, we want to register an ->on('progress') | ||
| 567 | %# handler instead of the plain part completely. In the ->on('progress') part, | ||
| 568 | %# we still run the handler, so maybe that is the same ?! | ||
| 569 | |||
| 570 | my $r1 = Future::Mojo->new(); | ||
| 571 | % if( $is_streaming ) { | ||
| 572 | <%= include('streaming_response', { | ||
| 573 | name => $method->{name}, | ||
| 574 | elt => $elt, | ||
| 575 | prefix => $prefix, | ||
| 576 | }); =%> | ||
| 577 | % } else { | ||
| 578 | <%= include('synchronous_response', { | ||
| 579 | name => $method->{name}, | ||
| 580 | elt => $elt, | ||
| 581 | prefix => $prefix, | ||
| 582 | }); | ||
| 583 | =%> | ||
| 584 | % } | ||
| 585 | |||
| 586 | return $res | ||
| 587 | } | ||
| 588 | |||
| 589 | % } | ||
| 590 | |||
| 591 | 1; | ||
| 592 | __CLIENT_IMPLEMENTATION__ | ||
| 593 | |||
| 594 | 2 | $template{client} = <<'__CLIENT__'; | |
| 595 | package <%= $prefix %>::<%= $name %> 0.01; | ||
| 596 | use 5.020; | ||
| 597 | use Moo 2; | ||
| 598 | use experimental 'signatures'; | ||
| 599 | |||
| 600 | extends '<%= $prefix %>::<%= $name %>::Impl'; | ||
| 601 | |||
| 602 | =head1 NAME | ||
| 603 | |||
| 604 | <%= $prefix %>::<%= $name %> - Client for <%= $prefix %> | ||
| 605 | |||
| 606 | =head1 SYNOPSIS | ||
| 607 | |||
| 608 | use 5.020; | ||
| 609 | use <%= $prefix %>::<%= $name %>; | ||
| 610 | |||
| 611 | my $client = <%= $prefix %>::<%= $name %>->new( | ||
| 612 | server => '<%= $schema->{servers}->[0]->{url} // "https://example.com/" %>', | ||
| 613 | ); | ||
| 614 | my $res = $client->someMethod()->get; | ||
| 615 | say $res; | ||
| 616 | |||
| 617 | =head1 METHODS | ||
| 618 | |||
| 619 | % for my $method ($methods->@*) { | ||
| 620 | % my $elt = $method->{elt}; | ||
| 621 | =head2 C<< <%= $method->{name} %> >> | ||
| 622 | |||
| 623 | my $res = $client-><%= $method->{name} %>()->get; | ||
| 624 | |||
| 625 | % if( $elt->{summary} and $elt->{summary} =~ /\S/ ) { | ||
| 626 | <%= $elt->{summary} =~ s/\s*$//r; %> | ||
| 627 | |||
| 628 | %} | ||
| 629 | %=include('return_types', { prefix => $prefix, elt => $elt }); | ||
| 630 | =cut | ||
| 631 | |||
| 632 | % } | ||
| 633 | 1; | ||
| 634 | __CLIENT__ | ||
| 635 | |||
| 636 | 2 | 1; | |
| 637 | |||
| 638 | __END__ | ||