Langertha/Role/HTTP.pm

filter: Statementstmt 83.5% 56 / 67 Branchbran 69.2% 18 / 26 Conditioncond 90.9% 10 / 11 MC/DCmcdc 85.7% 6 / 7 Subroutinesub 78.5% 11 / 14 Podpod 100% 5 / 5 totaltotal 81.5% 106 / 130 CC 18 SCAR 20.4
CC
18
Cov
81%
generate_http_request
13.0
execute_streaming_request
12.0
_build_user_agent
6.0
CRAP 7.7
« Langertha/Plugin/Langfuse.pm ↑ Summary Langertha/Engine/AKI.pm »
1 package Langertha::Role::HTTP;
2 # ABSTRACT: Role for HTTP APIs
3 34 our $VERSION = '0.502';
4 34 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 34 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 34 ▶use Log::Any qw( $log );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 34 ▶use Langertha::Request::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 34 requires qw(
15 json
16 );
17  
18 34 has url => (
19 is => 'ro',
20 isa => 'Str',
21 predicate => 'has_url',
22 );
23  
24  
25 sub generate_json_body {
26 69 ▶ my ( $self, %args ) = @_;
Subroutine generate_json_body: called CC=1 SCAR=0.0
Pod documented
27 69 return $self->json->encode({ %args });
28 }
29  
30  
31 34 our $boundary = 'XyXLaXyXngXyXerXyXthXyXaXyX';
32  
33 sub generate_multipart_body {
34 1 ▶ my ( $self, $req, %args ) = @_;
Subroutine generate_multipart_body: called CC=1 SCAR=0.0
Pod documented
35 1 my @formdata = map { $_, $args{$_} } sort { $a cmp $b } keys %args;
36 1 return HTTP::Request::Common::form_data(\@formdata, $boundary, $req);
37 }
38  
39  
40 sub generate_http_request {
41 98 ▶ my ( $self, $method, $url, $response_call, %args ) = @_;
Subroutine generate_http_request: called CC=13 SCAR=25.7
Pod documented
42 98 my $uri = URI->new($url);
43 98 ▶ my $content_type = (delete $args{content_type}||"");
Condition100%2/2
delete $args{'content_type'} || ''
l!l
5741
MC/DC100%1/1
delete $args{'content_type'} || ''
delete $args{'content_type'}
Truth table100%2/2
A
A= delete $args{'content_type'}
Aresult
11
00
44 98 my $userinfo = $uri->userinfo;
45 98 ▶ $uri->userinfo(undef) if $userinfo;
Branch: 1 branch
BranchTrueFalseTotal
if $userinfo 9 89 98
46 98 ▶ my $headers = [
Branch: 1 branch
BranchTrueFalseTotal
$content_type eq 'multipart/form-data' ? : 1 97 98
47 ( 'Content-Type',
48 $content_type eq 'multipart/form-data'
49 ? 'multipart/form-data; boundary="'.$boundary.'"'
50 : 'application/json; charset=utf-8' )
51 ];
52 98 ▶ my $request = Langertha::Request::HTTP->new(
Branch: 2 branches
BranchTrueFalseTotal
scalar %args > 0 ? : 70 28 98
!$content_type || $content_type eq 'application/json' ? : 69 1 70
Condition100%3/3
not $content_type or $content_type eq "application/json"
l!l&&r!l&&!r
13561
MC/DC100%2/2
not $content_type or $content_type eq "application/json"
not $content_type $content_type eq "application/json"
Truth table100%3/3
not(A) or B
A= not $content_typeB= $content_type eq "application/json"
ABresult
1X1
011
000
53 http => [ uc($method), $uri, $headers, ( scalar %args > 0 ?
54 ( !$content_type or $content_type eq 'application/json' )
55 ? $self->generate_json_body(%args)
56 : ()
57 : ()
58 ) ],
59 request_source => $self,
60 response_call => $response_call,
61 );
62 98 ▶ if ($content_type and $content_type eq 'multipart/form-data') {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_type and $content_type eq "multipart/form-data") 1 97 98
Condition100%3/3
$content_type and $content_type eq "multipart/form-data"
!ll&&!rl&&r
41561
MC/DC100%2/2
$content_type and $content_type eq "multipart/form-data"
$content_type $content_type eq "multipart/form-data"
Truth table100%3/3
A and B
A= $content_typeB= $content_type eq "multipart/form-data"
ABresult
0X0
100
111
63 1 $request->content($self->generate_multipart_body($request, %args));
64 }
65 98 ▶ if ($userinfo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($userinfo) 9 89 98
66 9 my ( $user, $pass ) = split(/:/, $userinfo);
67 9 ▶ if ($user and $pass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user and $pass) 8 1 9
Condition66%2/3
$user and $pass
!ll&&!rl&&r
018
MC/DC50%1/2
$user and $pass
$user $pass
Truth table66%2/3
A and B
A= $userB= $pass
ABresult
0X0
100
111
68 8 $request->authorization_basic($user, $pass);
69 }
70 }
71 98 ▶ $self->update_request($request) if $self->can('update_request');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("update_request") 84 14 98
72 98 return $request;
73 }
74  
75  
76 sub parse_response {
77 56 ▶ my ( $self, $response ) = @_;
Subroutine parse_response: called CC=3 SCAR=12.3
Pod documented
78 56 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 56 56
79 0 $log->errorf("[%s] HTTP %s", ref $self, $response->status_line);
80 0 croak "".(ref $self)." request failed: ".($response->status_line);
81 }
82 56 ▶ $self->_update_rate_limit($response) if $self->can('_update_rate_limit');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_update_rate_limit") 56 0 56
83 56 $log->tracef("[%s] Response: %s", ref $self, $response->decoded_content);
84 56 return $self->json->decode($response->content);
85 }
86  
87  
88 34 has user_agent_timeout => (
89 isa => 'Int',
90 is => 'ro',
91 predicate => 'has_user_agent_timeout',
92 );
93  
94  
95 34 has user_agent_agent => (
96 isa => 'Str',
97 is => 'ro',
98 lazy_build => 1,
99 );
100 sub _build_user_agent_agent {
101 0 ▶ my ( $self ) = @_;
Subroutine _build_user_agent_agent: not called CC=1 SCAR=6.9
102 0 return "".(ref $self)."";
103 }
104  
105  
106 34 has user_agent => (
107 isa => 'LWP::UserAgent',
108 is => 'ro',
109 lazy_build => 1,
110 );
111 sub _build_user_agent {
112 0 ▶ my ( $self ) = @_;
Subroutine _build_user_agent: not called CC=2 SCAR=17.9
113 0 ▶ return LWP::UserAgent->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->has_user_agent_timeout ? : 0 0 0
114 agent => $self->user_agent_agent,
115 $self->has_user_agent_timeout ? ( timeout => $self->user_agent_timeout ) : (),
116 );
117 }
118  
119  
120 sub execute_streaming_request {
121 0 ▶ my ($self, $request, $chunk_callback) = @_;
Subroutine execute_streaming_request: not called CC=3 SCAR=24.8
Pod documented
122  
123 0 ▶ croak "execute_streaming_request requires Langertha::Role::Streaming"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->does("Langertha::Role::Streaming") 0 0 0
124 unless $self->does('Langertha::Role::Streaming');
125  
126 0 my $response = $self->user_agent->request($request);
127  
128 0 ▶ croak "".(ref $self)." streaming request failed: ".($response->status_line)
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
129 unless $response->is_success;
130  
131 0 return $self->process_stream_data($response->content, $chunk_callback);
132 }
133  
134  
135  
136 34 1;
137  
138 __END__
139  
140 =pod
141  
142 =encoding UTF-8
143  
144 =head1 NAME
145  
146 Langertha::Role::HTTP - Role for HTTP APIs
147  
148 =head1 VERSION
149  
150 version 0.502
151  
152 =head2 url
153  
154 Base URL for API requests. Optional — many engines hard-code their default URL
155 internally and only require this attribute to be set when pointing at a custom
156 or self-hosted endpoint.
157  
158 =head2 generate_json_body
159  
160 my $body = $engine->generate_json_body(%args);
161  
162 Encodes C<%args> as a JSON string using the engine's L<Langertha::Role::JSON/json>
163 instance. Used internally when building C<application/json> request bodies.
164  
165 =head2 generate_multipart_body
166  
167 my $body = $engine->generate_multipart_body($request, %args);
168  
169 Encodes C<%args> as a C<multipart/form-data> body and attaches it to C<$request>.
170 Used internally when the OpenAPI spec specifies C<multipart/form-data> content type
171 (e.g. for audio upload endpoints).
172  
173 =head2 generate_http_request
174  
175 my $request = $engine->generate_http_request(
176 $method, $url, $response_call, %args
177 );
178  
179 Low-level HTTP request builder. Creates a L<Langertha::Request::HTTP> object
180 with the appropriate headers and body encoding (JSON or multipart). Calls the
181 engine's C<update_request> hook if it exists, allowing engines to inject
182 authentication headers. If the URL contains C<user:password> userinfo, HTTP
183 Basic authentication is set automatically.
184  
185 =head2 parse_response
186  
187 my $data = $engine->parse_response($http_response);
188  
189 Decodes a successful L<HTTP::Response> body as JSON and returns the data
190 structure. Croaks with the HTTP status line on failure. If the engine
191 supports rate limiting, extracts rate limit headers via
192 C<_update_rate_limit> before decoding the body.
193  
194 =head2 user_agent_timeout
195  
196 Optional timeout in seconds for the L<LWP::UserAgent>. When not set, the
197 default L<LWP::UserAgent> timeout applies.
198  
199 =head2 user_agent_agent
200  
201 The C<User-Agent> string sent with HTTP requests. Defaults to the engine's
202 class name.
203  
204 =head2 user_agent
205  
206 The L<LWP::UserAgent> instance used for synchronous HTTP requests. Built lazily
207 with C<user_agent_agent> and C<user_agent_timeout>.
208  
209 =head2 execute_streaming_request
210  
211 my $chunks = $engine->execute_streaming_request($request, $chunk_callback);
212 my $chunks = $engine->execute_streaming_request($request);
213  
214 Executes a streaming HTTP request synchronously using L<LWP::UserAgent> and
215 delegates stream parsing to L<Langertha::Role::Streaming/process_stream_data>.
216 Requires the engine to also compose L<Langertha::Role::Streaming>. Returns an
217 ArrayRef of L<Langertha::Stream::Chunk> objects. If C<$chunk_callback> is
218 provided it is called with each chunk as it is parsed.
219  
220 =head1 SEE ALSO
221  
222 =over
223  
224 =item * L<Langertha::Role::JSON> - JSON encoding/decoding (required by this role)
225  
226 =item * L<Langertha::Role::Streaming> - Stream processing
227  
228 =item * L<Langertha::Role::OpenAPI> - OpenAPI request generation
229  
230 =item * L<Langertha::Request::HTTP> - HTTP request object created by this role
231  
232 =back
233  
234 =head1 SUPPORT
235  
236 =head2 Issues
237  
238 Please report bugs and feature requests on GitHub at
239 L<https://github.com/Getty/langertha/issues>.
240  
241 =head2 IRC
242  
243 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
244  
245 =head1 CONTRIBUTING
246  
247 Contributions are welcome! Please fork the repository and submit a pull request.
248  
249 =head1 AUTHOR
250  
251 Torsten Raudssus <getty@cpan.org>
252  
253 =head1 COPYRIGHT AND LICENSE
254  
255 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
256  
257 This is free software; you can redistribute it and/or modify it under
258 the same terms as the Perl 5 programming language system itself.
259  
260 =cut
« Langertha/Plugin/Langfuse.pm ↑ Summary Langertha/Engine/AKI.pm »