OpenAPI-PerlGenerator
view release on metacpan or search on metacpan
lib/OpenAPI/PerlGenerator/Template/Mojo.pm view on Meta::CPAN
% if( $type->{name} ) {
<%= $prefix %>::<%= $type->{name} %>->new(<%= $argname %>),
% } elsif( $type->{type} and $type->{type} eq 'array') {
%# use Data::Dumper; warn Dumper $type;
[ map { <%= include('inflated_response', { type => $type->{items}, prefix => $prefix, argname => '$_' }) %> } $payload->@* ],
% } else {
<%= $argname %>
% }
__INFLATED_RESPONSE__
$template{streaming_response} = <<'__STREAMING_RESPONSE__';
use Future::Queue;
my $res = Future::Queue->new( prototype => 'Future::Mojo' );
our @store; # we should use ->retain() instead
push @store, $r1->then( sub( $tx ) {
my $resp = $tx->res;
# Should we validate using OpenAPI::Modern here?!
%# Should this be its own subroutine instead?!
% for my $code (sort keys $elt->{responses}->%*) { # response code s
% my $info = $elt->{responses}->{ $code };
%# XXX if streaming, we need to handle a non-streaming error response!
<%= elsif_chain($name) %>( $resp->code <%= openapi_http_code_match( $code ) %> ) {
% if( $info->{description} =~ /\S/ ) {
# <%= single_line( $info->{description} ) %>
% }
% # Check the content type
% # Will we always have a content type?!
% if( keys $info->{content}->%* ) {
% for my $ct (sort keys $info->{content}->%*) {
my $ct = $resp->headers->content_type;
return unless $ct;
lib/OpenAPI/PerlGenerator/Template/Mojo.pm view on Meta::CPAN
% if( keys $info->{content}->%* ) {
% for my $ct (sort keys $info->{content}->%*) {
my $ct = $resp->headers->content_type;
$ct =~ s/;\s+.*//;
if( $ct eq '<%= $ct %>' ) {
%# These handlers for content types should come from templates? Or maybe
%# from a subroutine?!
% if( $ct eq 'application/json' ) {
my $payload = $resp->json();
% } elsif( $ct eq 'application/x-ndjson' ) {
# code missing to hack up ndjson into hashes for a non-streaming response
my $payload = $resp->body();
% } else {
my $payload = $resp->body();
% }
return Future::Mojo->done(
% my $type = $info->{content}->{$ct}->{schema};
<%= include('inflated_response', { type => $type, prefix => $prefix, argname => '$payload' } ) %>
);
}
% }
lib/OpenAPI/PerlGenerator/Template/Mojo.pm view on Meta::CPAN
has 'server' => (
is => 'lazy',
default => sub { 'http://localhost:11434/api' }, # XXX pull from OpenAPI file instead
);
=head1 METHODS
% for my $method ($methods->@*) {
% my $elt = $method->{elt};
% my $is_streaming = exists $elt->{responses}->{200}
% && $elt->{responses}->{200}->{content}
% && [keys $elt->{responses}->{200}->{content}->%*]->[0] eq 'application/x-ndjson'
% ;
%
%# Sort the parameters according to where they go
% my %parameters;
% if( my $p = $elt->{parameters}) {
% for my $p ($elt->{parameters}->@*) {
% $parameters{ $p->{in} } //= [];
% push $parameters{ $p->{in} }->@*, $p;
% }
% }
%
=head2 C<< <%= $method->{name} %> >>
%# Generate the example invocation
% if( $is_streaming ) {
use Future::Utils 'repeat';
my $responses = $client-><%= $method->{name} %>();
repeat {
my ($res) = $responses->shift;
if( $res ) {
my $str = $res->get;
say $str;
}
Future::Mojo->done( defined $res );
lib/OpenAPI/PerlGenerator/Template/Mojo.pm view on Meta::CPAN
sub <%= $method->{name} %>( $self, %options ) {
my $tx = $self->_build_<%= $method->{name} %>_request(%options);
# validate our request while developing
my $results = $self->openapi->validate_request($tx->req);
if( $results->{error}) {
say $results;
say $tx->req->to_string;
};
%# We want to handle both here, streaming (ndjson) and plain responses
%# Plain responses are easy, but for streamed, we want to register an ->on('progress')
%# handler instead of the plain part completely. In the ->on('progress') part,
%# we still run the handler, so maybe that is the same ?!
my $r1 = Future::Mojo->new();
% if( $is_streaming ) {
<%= include('streaming_response', {
name => $method->{name},
elt => $elt,
prefix => $prefix,
}); =%>
% } else {
<%= include('synchronous_response', {
name => $method->{name},
elt => $elt,
prefix => $prefix,
});
t/ollama/ollama-curated.yaml view on Meta::CPAN
content:
application/x-ndjson:
schema:
$ref: '#/components/schemas/GenerateCompletionResponse'
/chat:
post:
operationId: generateChatCompletion
tags:
- Chat
summary: Generate the next message in a chat with a provided model.
description: This is a streaming endpoint, so there will be a series of responses. The final response object will include statistics and additional data from the request.
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/GenerateChatCompletionRequest'
responses:
'200':
description: Successful operation.
content:
application/x-ndjson:
( run in 0.282 second using v1.01-cache-2.11-cpan-4d50c553e7e )