blib/lib/Kubernetes/REST/LWPIO.pm untested

filter: Statementstmt 0.0% 0 / 60 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 95 CC 2 SCAR 10.6
CC
2
Cov
0%
_response
6.0
call
2.0
call_streaming
2.0
CRAP 2.9
1 package Kubernetes::REST::LWPIO;
2 our $VERSION = '1.108';
3 # ABSTRACT: HTTP client using LWP::UserAgent
4 use Moo;
5 use LWP::UserAgent;
6 use Kubernetes::REST::HTTPResponse;
7 use Types::Standard qw/Bool/;
8  
9 with 'Kubernetes::REST::Role::IO';
10  
11  
12 has ssl_verify_server => (is => 'ro', isa => Bool, default => 1);
13  
14  
15 has ssl_cert_file => (is => 'ro');
16  
17  
18 has ssl_cert_pem => (is => 'ro');
19  
20  
21 has ssl_key_file => (is => 'ro');
22  
23  
24 has ssl_key_pem => (is => 'ro');
25  
26  
27 has ssl_ca_file => (is => 'ro');
28  
29  
30 has ssl_ca_pem => (is => 'ro');
31  
32  
33 has timeout => (is => 'ro', default => sub { 310 });
34  
35  
36 has ua => (is => 'ro', lazy => 1, default => sub {
37 my $self = shift;
38 require IO::Socket::SSL::Utils;
39  
40 my %ssl_opts;
41 $ssl_opts{ verify_hostname } = $self->ssl_verify_server;
42 $ssl_opts{ SSL_verify_mode } = $self->ssl_verify_server ? 1 : 0;
43  
44 # PEM data: convert to X509/EVP_PKEY objects via SSL_cert/SSL_key
45 # PEM file paths: use SSL_cert_file/SSL_key_file as normal
46 if (defined $self->ssl_cert_pem) {
47 $ssl_opts{ SSL_cert } = [ IO::Socket::SSL::Utils::PEM_string2cert($self->ssl_cert_pem) ];
48 } elsif (defined $self->ssl_cert_file) {
49 $ssl_opts{ SSL_cert_file } = $self->ssl_cert_file;
50 }
51  
52 if (defined $self->ssl_key_pem) {
53 $ssl_opts{ SSL_key } = IO::Socket::SSL::Utils::PEM_string2key($self->ssl_key_pem);
54 } elsif (defined $self->ssl_key_file) {
55 $ssl_opts{ SSL_key_file } = $self->ssl_key_file;
56 }
57  
58 if (defined $self->ssl_ca_pem) {
59 $ssl_opts{ SSL_ca } = [ IO::Socket::SSL::Utils::PEM_string2cert($self->ssl_ca_pem) ];
60 } elsif (defined $self->ssl_ca_file) {
61 $ssl_opts{ SSL_ca_file } = $self->ssl_ca_file;
62 }
63  
64 return LWP::UserAgent->new(
65 agent => 'Kubernetes::REST Perl Client ' . ($Kubernetes::REST::VERSION // 'dev'),
66 timeout => $self->timeout,
67 ssl_opts => \%ssl_opts,
68 );
69 });
70  
71  
72 sub call {
73 my ($self, $req) = @_;
74  
75  
76 my $http_req = HTTP::Request->new(
77 $req->method,
78 $req->url,
79 [ %{$req->headers} ],
80 $req->content,
81 );
82  
83 my $res = $self->ua->request($http_req);
84  
85 return $self->_response($res);
86 }
87  
88 sub call_streaming {
89 my ($self, $req, $data_callback) = @_;
90  
91  
92 my $http_req = HTTP::Request->new(
93 $req->method,
94 $req->url,
95 [ %{$req->headers} ],
96 );
97  
98 my $res = $self->ua->request($http_req, sub {
99 my ($chunk) = @_;
100 $data_callback->($chunk);
101 });
102  
103 return $self->_response($res);
104 }
105  
106 # charset => 'none' undoes Content-Encoding (gzip et al) but leaves the charset
107 # alone, so the body stays bytes - which is what Kubernetes::REST and IO::K8s
108 # expect, and what HTTPTinyIO hands back. Plain decoded_content() would decode
109 # UTF-8 here and IO::K8s would then decode the characters a second time.
110 sub _response {
111 my ($self, $res) = @_;
112  
113 my $content = $res->decoded_content(charset => 'none');
114  
115 return Kubernetes::REST::HTTPResponse->new(
116 status => $res->code,
117 (defined $content && length $content) ? ( content => $content ) : (),
118 );
119 }
120  
121 1;
122  
123 __END__
124  
125 =pod
126  
127 =encoding UTF-8
128  
129 =head1 NAME
130  
131 Kubernetes::REST::LWPIO - HTTP client using LWP::UserAgent
132  
133 =head1 VERSION
134  
135 version 1.108
136  
137 =head1 SYNOPSIS
138  
139 use Kubernetes::REST::LWPIO;
140  
141 my $io = Kubernetes::REST::LWPIO->new(
142 ssl_verify_server => 1,
143 ssl_ca_file => '/path/to/ca.crt',
144 );
145  
146 # Access the LWP::UserAgent for debugging (e.g. with LWP::ConsoleLogger)
147 use LWP::ConsoleLogger::Easy qw(debug_ua);
148 debug_ua($io->ua);
149  
150 =head1 DESCRIPTION
151  
152 HTTP client implementation using L<LWP::UserAgent> for making Kubernetes API requests. This is the default IO backend for L<Kubernetes::REST>.
153  
154 The C<ua> attribute is exposed so that debugging tools like L<LWP::ConsoleLogger> can be attached to inspect HTTP traffic.
155  
156 Response bodies are returned as bytes, via C<< decoded_content(charset => 'none') >>: C<Content-Encoding> is undone, the charset is left to L<Kubernetes::REST>. See L<Kubernetes::REST::Role::IO/Encoding contract>.
157  
158 This backend does not implement C<call_duplex>: L<Kubernetes::REST> methods that
159 need full-duplex transport (C<port_forward>, C<exec>, C<attach>) croak against
160 it by design. Use L<Net::Async::Kubernetes> for those.
161  
162 =head2 ssl_verify_server
163  
164 Boolean. Whether to verify the server's SSL certificate. Defaults to true.
165  
166 =head2 ssl_cert_file
167  
168 Path to a client certificate file (PEM) for TLS client-certificate authentication. Ignored when C<ssl_cert_pem> is set.
169  
170 =head2 ssl_cert_pem
171  
172 Client certificate as a PEM string, for TLS client-certificate authentication. Takes precedence over C<ssl_cert_file>.
173  
174 =head2 ssl_key_file
175  
176 Path to the private key file (PEM) matching C<ssl_cert_file> or C<ssl_cert_pem>. Ignored when C<ssl_key_pem> is set.
177  
178 =head2 ssl_key_pem
179  
180 Private key as a PEM string, matching C<ssl_cert_file> or C<ssl_cert_pem>. Takes precedence over C<ssl_key_file>.
181  
182 =head2 ssl_ca_file
183  
184 Path to a CA certificate file (PEM) used to verify the server's certificate. Ignored when C<ssl_ca_pem> is set.
185  
186 =head2 ssl_ca_pem
187  
188 CA certificate as a PEM string, used to verify the server's certificate. Takes precedence over C<ssl_ca_file>.
189  
190 =head2 timeout
191  
192 Timeout in seconds for HTTP requests. Defaults to 310 (slightly more than the Kubernetes default watch timeout of 300s).
193  
194 =head2 ua
195  
196 The underlying L<LWP::UserAgent> instance. Access this to attach middleware such as L<LWP::ConsoleLogger> for HTTP debugging.
197  
198 =head2 call
199  
200 my $response = $io->call($req);
201  
202 Execute an HTTP request. Receives a fully prepared L<Kubernetes::REST::HTTPRequest> (URL, headers, content all set). Returns a L<Kubernetes::REST::HTTPResponse>.
203  
204 =head2 call_streaming
205  
206 my $response = $io->call_streaming($req, sub { my ($chunk) = @_; ... });
207  
208 Execute an HTTP request with streaming response. The C<$data_callback> is called with each chunk of data as it arrives.
209  
210 Used internally by L<Kubernetes::REST/watch> and L<Kubernetes::REST/log> for the Watch and Pod Log APIs.
211  
212 =head1 SEE ALSO
213  
214 =over
215  
216 =item * L<Kubernetes::REST> - Main API client
217  
218 =item * L<Kubernetes::REST::Role::IO> - IO interface role
219  
220 =item * L<Kubernetes::REST::HTTPTinyIO> - Alternative HTTP::Tiny backend
221  
222 =item * L<LWP::ConsoleLogger> - HTTP debugging tool
223  
224 =back
225  
226 =head1 SUPPORT
227  
228 =head2 Issues
229  
230 Please report bugs and feature requests on GitHub at
231 L<https://github.com/pplu/kubernetes-rest/issues>.
232  
233 =head2 IRC
234  
235 Join C<#kubernetes> on C<irc.perl.org> or message Getty directly.
236  
237 =head1 CONTRIBUTING
238  
239 Contributions are welcome! Please fork the repository and submit a pull request.
240  
241 =head1 AUTHORS
242  
243 =over 4
244  
245 =item *
246  
247 Torsten Raudssus <getty@cpan.org>
248  
249 =item *
250  
251 Jose Luis Martinez Torres <jlmartin@cpan.org>
252  
253 =back
254  
255 =head1 COPYRIGHT AND LICENSE
256  
257 This software is Copyright (c) 2019-2026 by Jose Luis Martinez Torres <jlmartin@cpan.org>.
258  
259 This is free software, licensed under:
260  
261 The Apache License, Version 2.0, January 2004
262  
263 =cut