| 1 | package Langertha::Role::OpenAPI; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Role for APIs with OpenAPI definition | ||||||||||||||||||||||||||||
| 3 | 31 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||
| 4 | 31 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 31 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 31 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 31 | ▶ | use JSON::PP (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 31 | ▶ | use Log::Any qw( $log ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 31 | ▶ | use OpenAPI::Modern; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 31 | ▶ | use Path::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 31 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 31 | ▶ | use YAML::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 31 | requires qw( | |||||||||||||||||||||||||||
| 16 | openapi_file | ||||||||||||||||||||||||||||
| 17 | generate_http_request | ||||||||||||||||||||||||||||
| 18 | url | ||||||||||||||||||||||||||||
| 19 | json | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 31 | has openapi_operations => ( | |||||||||||||||||||||||||||
| 23 | is => 'ro', | ||||||||||||||||||||||||||||
| 24 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 25 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||
| 27 | sub _build_openapi_operations { | ||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_openapi_operations: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||
| 29 | # Slow path: parse YAML + OpenAPI::Modern | ||||||||||||||||||||||||||||
| 30 | 0 | my $oam = $self->openapi; | |||||||||||||||||||||||||||
| 31 | 0 | my $data = $oam->openapi_document->get('/'); | |||||||||||||||||||||||||||
| 32 | 0 | my %operations; | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | my $paths = $data->{paths} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$$data{'paths'} || {}
MC/DC0%0/1
$$data{'paths'} || {}
$$data{'paths'}
Truth table0%0/2
A
A= $$data{'paths'}
| |||||||||||||||||||||||||||||
| 34 | 0 | for my $path (keys %$paths) { | |||||||||||||||||||||||||||
| 35 | 0 | for my $method (keys %{$paths->{$path}}) { | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | next unless ref $paths->{$path}{$method} eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | my $op = $paths->{$path}{$method}; | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $opId = $op->{operationId} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | my $ct; | |||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($op->{requestBody} && $op->{requestBody}{content}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$$op{"requestBody"} and $$op{"requestBody"}{"content"}
MC/DC0%0/2
$$op{"requestBody"} and $$op{"requestBody"}{"content"}
$$op{"requestBody"}
$$op{"requestBody"}{"content"}
Truth table0%0/3
A and B
A= $$op{"requestBody"}B= $$op{"requestBody"}{"content"}
| |||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $ct = 'application/json' if $op->{requestBody}{content}{'application/json'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $ct //= 'multipart/form-data' if $op->{requestBody}{content}{'multipart/form-data'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$ct //= "multipart/form-data"
MC/DC0%0/1
$ct //= "multipart/form-data"
$ct
Truth table0%0/2
A
A= $ct
| |||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $operations{$opId} = { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | method => uc($method), | ||||||||||||||||||||||||||||
| 46 | path => $path, | ||||||||||||||||||||||||||||
| 47 | defined $ct ? (content_type => $ct) : (), | ||||||||||||||||||||||||||||
| 48 | }; | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my $server_url = $data->{servers}[0]{url} if $data->{servers}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 0 | return { server_url => $server_url, operations => \%operations }; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 31 | has openapi => ( | |||||||||||||||||||||||||||
| 57 | is => 'ro', | ||||||||||||||||||||||||||||
| 58 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | sub _build_openapi { | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_openapi: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 62 | 0 | my ( $format, $file ) = $self->openapi_file; | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | croak "".(ref $self)." can only do format yaml for the OpenAPI spec currently" unless $format eq 'yaml'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | my $yaml = $file; | |||||||||||||||||||||||||||
| 65 | 0 | return OpenAPI::Modern->new( | |||||||||||||||||||||||||||
| 66 | openapi_uri => $yaml, | ||||||||||||||||||||||||||||
| 67 | openapi_schema => YAML::PP->new(boolean => 'JSON::PP')->load_string(path($yaml)->slurp_utf8), | ||||||||||||||||||||||||||||
| 68 | ); | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 31 | has supported_operations => ( | |||||||||||||||||||||||||||
| 73 | is => 'ro', | ||||||||||||||||||||||||||||
| 74 | isa => 'ArrayRef[Str]', | ||||||||||||||||||||||||||||
| 75 | lazy_build => 1, | ||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||
| 77 | sub _build_supported_operations { | ||||||||||||||||||||||||||||
| 78 | 26 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build_supported_operations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 79 | 26 | return []; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub can_operation { | ||||||||||||||||||||||||||||
| 84 | 69 | ▶ | my ( $self, $operationId ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
can_operation: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 69 | ▶ | return 1 unless scalar @{$self->supported_operations} > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 37 | my %so = map { $_, 1 } @{$self->supported_operations}; | |||||||||||||||||||||||||||
| 87 | 37 | return $so{$operationId}; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub get_operation { | ||||||||||||||||||||||||||||
| 92 | 59 | ▶ | my ( $self, $operationId ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_operation: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 59 | ▶ | croak "".(ref $self)." runs in compatibility mode and is unable to perform this OpenAPI operation" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | unless ($self->can_operation($operationId)); | ||||||||||||||||||||||||||||
| 95 | 59 | my $ops = $self->openapi_operations; | |||||||||||||||||||||||||||
| 96 | 59 | ▶ | my $op = $ops->{operations}{$operationId} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | or croak "".(ref $self).": operationId '$operationId' not found in spec"; | ||||||||||||||||||||||||||||
| 98 | 59 | ▶ | my $url = $self->url || $ops->{server_url}; | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->url || $$ops{'server_url'}
MC/DC0%0/2
$self->url || $$ops{'server_url'}
$self->url
$$ops{'server_url'}
Truth table33%1/3
A || B
A= $self->urlB= $$ops{'server_url'}
| |||||||||||||||||||||||||||||
| 99 | 59 | return ( $op->{method}, $url.$op->{path}, $op->{content_type} ); | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | sub generate_request { | ||||||||||||||||||||||||||||
| 104 | 59 | ▶ | my ( $self, $operationId, $response_call, %args ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
generate_request: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 105 | 59 | my ( $method, $url, $content_type ) = $self->get_operation($operationId); | |||||||||||||||||||||||||||
| 106 | 59 | $log->debugf("[%s] %s %s (%s)", ref $self, $method, $url, $operationId); | |||||||||||||||||||||||||||
| 107 | 59 | ▶ | $args{content_type} = $content_type if defined $content_type; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 59 | return $self->generate_http_request( $method, $url, $response_call, %args ); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 31 | 1; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | __END__ | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =pod | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | =head1 NAME | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | Langertha::Role::OpenAPI - Role for APIs with OpenAPI definition | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =head1 VERSION | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | version 0.502 | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 openapi_operations | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | HashRef of pre-computed OpenAPI operation data. Contains C<server_url> and | ||||||||||||||||||||||||||||
| 132 | an C<operations> sub-hash mapping each C<operationId> to its HTTP method, | ||||||||||||||||||||||||||||
| 133 | path, and content type. Built lazily; engines can override | ||||||||||||||||||||||||||||
| 134 | C<_build_openapi_operations> to provide pre-computed data and skip the | ||||||||||||||||||||||||||||
| 135 | expensive YAML parsing / OpenAPI::Modern construction. | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =head2 openapi | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | The L<OpenAPI::Modern> instance loaded from the engine's C<openapi_file>. Built | ||||||||||||||||||||||||||||
| 140 | lazily on first use. Only used as fallback when C<openapi_operations> is not | ||||||||||||||||||||||||||||
| 141 | overridden. Only YAML format OpenAPI specs are currently supported. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head2 supported_operations | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | ArrayRef of C<operationId> strings that this engine instance supports. When | ||||||||||||||||||||||||||||
| 146 | non-empty, only listed operations are permitted; all others croak. Defaults to | ||||||||||||||||||||||||||||
| 147 | an empty ArrayRef (all operations allowed). Used to restrict engines that run in | ||||||||||||||||||||||||||||
| 148 | a limited compatibility mode. | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 can_operation | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | if ($engine->can_operation('createChatCompletion')) { ... } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | Returns true if the given C<$operationId> is supported by this engine. Always | ||||||||||||||||||||||||||||
| 155 | returns true when C<supported_operations> is empty (unrestricted mode). | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head2 get_operation | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | my ($method, $url, $content_type) = $engine->get_operation($operationId); | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | Looks up an operation by C<$operationId> in the OpenAPI spec and returns the | ||||||||||||||||||||||||||||
| 162 | HTTP method, full URL, and content type as a three-element list. Croaks if the | ||||||||||||||||||||||||||||
| 163 | operation is not in C<supported_operations>. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head2 generate_request | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | my $request = $engine->generate_request($operationId, $response_call, %args); | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | Generates an HTTP request for the named OpenAPI C<$operationId>. Resolves the | ||||||||||||||||||||||||||||
| 170 | method, URL, and content type from the spec, then delegates to | ||||||||||||||||||||||||||||
| 171 | L<Langertha::Role::HTTP/generate_http_request>. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =over | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =item * L<Langertha::Role::HTTP> - HTTP request building (required by this role) | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =item * L<Langertha::Role::Models> - Model management (typically composed alongside this role) | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | =item * L<OpenAPI::Modern> - OpenAPI spec handling | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =back | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 Issues | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 190 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head2 IRC | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 209 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||