blib/lib/Langertha/Role/OpenAPI.pm

filter: Statementstmt 69.7% 53 / 76 Branchbran 25.0% 6 / 24 Conditioncond 10.0% 1 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 86.6% 13 / 15 Podpod 100% 3 / 3 totaltotal 56.7% 76 / 134 CC 19 SCAR 42.9
CC
19
Cov
55%
_build_openapi_operations
182.0
_build_openapi
6.0
get_operation
4.5
CRAP 73.1
« bin/langertha_image ↑ Summary blib/lib/Langertha/Output/Tools.pm »
1 package Langertha::Role::OpenAPI;
2 # ABSTRACT: Role for APIs with OpenAPI definition
3 155 our $VERSION = '0.503';
4 155 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 155 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 155 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 155 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 155 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 155 ▶use OpenAPI::Modern;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 155 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 155 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 155 ▶use YAML::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 155 requires qw(
16 openapi_file
17 generate_http_request
18 url
19 json
20 );
21  
22 155 has openapi_operations => (
23 is => 'ro',
24 isa => 'HashRef',
25 lazy_build => 1,
26 );
27 0 ▶sub _build_openapi_operations {
Subroutine _build_openapi_operations: not called CC=13 SCAR=52.0
28 0 my ( $self ) = @_;
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'} || {}
l!l
00
MC/DC0%0/1
$$data{'paths'} || {}
$$data{'paths'}
Truth table0%0/2
A
A= $$data{'paths'}
Aresult
11
00
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
BranchTrueFalseTotal
unless ref $$paths{$path}{$method} eq "HASH" 0 0 0
37 0 my $op = $paths->{$path}{$method};
38 0 ▶ my $operation_id = $op->{operationId} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $operation_id = $$op{"operationId"} or next 0 0 0
39 0 my $ct;
40 0 ▶ if ($op->{requestBody} && $op->{requestBody}{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$op{"requestBody"} and $$op{"requestBody"}{"content"}) 0 0 0
Condition0%0/3
$$op{"requestBody"} and $$op{"requestBody"}{"content"}
!ll&&!rl&&r
000
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"}
ABresult
0X0
100
111
41 0 ▶ $ct = 'application/json' if $op->{requestBody}{content}{'application/json'};
Branch: 1 branch
BranchTrueFalseTotal
if $$op{"requestBody"}{"content"}{"application/json"} 0 0 0
42 0 ▶ $ct //= 'multipart/form-data' if $op->{requestBody}{content}{'multipart/form-data'};
Branch: 1 branch
BranchTrueFalseTotal
if $$op{"requestBody"}{"content"}{"multipart/form-data"} 0 0 0
Condition0%0/2
$ct //= "multipart/form-data"
l!l
00
MC/DC0%0/1
$ct //= "multipart/form-data"
$ct
Truth table0%0/2
A
A= $ct
Aresult
11
00
43 }
44 0 ▶ $operations{$operation_id} = {
Branch: 1 branch
BranchTrueFalseTotal
defined $ct ? : 0 0 0
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
BranchTrueFalseTotal
if $$data{"servers"} 0 0 0
52 0 return { server_url => $server_url, operations => \%operations };
53 }
54  
55  
56 155 has openapi => (
57 is => 'ro',
58 lazy_build => 1,
59 );
60 0 ▶sub _build_openapi {
Subroutine _build_openapi: not called CC=2 SCAR=17.9
61 0 my ( $self ) = @_;
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
BranchTrueFalseTotal
unless $format eq "yaml" 0 0 0
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 155 has supported_operations => (
73 is => 'ro',
74 isa => 'ArrayRef[Str]',
75 lazy_build => 1,
76 );
77 583 ▶sub _build_supported_operations {
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
78 583 my ( $self ) = @_;
79 583 return [];
80 }
81  
82  
83 2212 ▶sub can_operation {
Subroutine can_operation: called CC=2 SCAR=6.9
Pod documented
84 2212 my ( $self, $operation_id ) = @_;
85 2212 ▶ return 1 unless scalar @{$self->supported_operations} > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->supported_operations;} > 0 881 1331 2212
86 1331 my %so = map { $_, 1 } @{$self->supported_operations};
87 1331 return $so{$operation_id};
88 }
89  
90  
91 2192 ▶sub get_operation {
Subroutine get_operation: called CC=4 SCAR=15.0
Pod documented
92 2192 my ( $self, $operation_id ) = @_;
93 2192 ▶ croak "".(ref $self)." runs in compatibility mode and is unable to perform this OpenAPI operation"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can_operation($operation_id) 0 2192 2192
94 unless ($self->can_operation($operation_id));
95 2192 my $ops = $self->openapi_operations;
96 2192 ▶ my $op = $ops->{operations}{$operation_id}
Branch: 1 branch
BranchTrueFalseTotal
my $op = $$ops{"operations"}{$operation_id} or croak('' . ref($self) . ": operationId '${operation_id}' not found in spec") 2192 0 2192
97 or croak "".(ref $self).": operationId '$operation_id' not found in spec";
98 2192 ▶ my $url = $self->url || $ops->{server_url};
Condition33%1/3
$self->url || $$ops{'server_url'}
l!l&&r!l&&!r
219200
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'}
ABresult
1X1
011
000
99 2192 return ( $op->{method}, $url.$op->{path}, $op->{content_type} );
100 }
101  
102  
103 2192 ▶sub generate_request {
Subroutine generate_request: called CC=2 SCAR=6.9
Pod documented
104 2192 my ( $self, $operation_id, $response_call, %args ) = @_;
105 2192 my ( $method, $url, $content_type ) = $self->get_operation($operation_id);
106 2192 $log->debugf("[%s] %s %s (%s)", ref $self, $method, $url, $operation_id);
107 2192 ▶ $args{content_type} = $content_type if defined $content_type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $content_type 2187 5 2192
108 2192 return $self->generate_http_request( $method, $url, $response_call, %args );
109 }
110  
111  
112  
113 155 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.503
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<$operation_id> 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($operation_id);
160  
161 Looks up an operation by C<$operation_id> 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($operation_id, $response_call, %args);
168  
169 Generates an HTTP request for the named OpenAPI C<$operation_id>. 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
« bin/langertha_image ↑ Summary blib/lib/Langertha/Output/Tools.pm »