| 1 | package Kubernetes::REST::HTTPTinyIO; | ||||||||||||||||||||
| 2 | 4 | our $VERSION = '1.113'; | |||||||||||||||||||
| 3 | # ABSTRACT: HTTP client using HTTP::Tiny | ||||||||||||||||||||
| 4 | 4 | ▶ | use Moo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 4 | ▶ | use HTTP::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use IO::Socket::SSL; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 4 | ▶ | use Kubernetes::REST::HTTPResponse; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 4 | ▶ | use Types::Standard qw/Bool/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 4 | with 'Kubernetes::REST::Role::IO'; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 4 | has ssl_verify_server => (is => 'ro', isa => Bool, default => 1); | |||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 4 | has ssl_cert_file => (is => 'ro'); | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 4 | has ssl_cert_pem => (is => 'ro'); | |||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 4 | has ssl_key_file => (is => 'ro'); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | 4 | has ssl_key_pem => (is => 'ro'); | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 4 | has ssl_ca_file => (is => 'ro'); | |||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 4 | has ssl_ca_pem => (is => 'ro'); | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 17 | ▶ | has timeout => (is => 'ro', default => sub { 310 }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 4 | ▶ | has ua => (is => 'ro', lazy => 1, default => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=25.7
| |||||||||||||||||||||
| 38 | 4 | my $self = shift; | |||||||||||||||||||
| 39 | 4 | require IO::Socket::SSL::Utils; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 4 | my %options; | |||||||||||||||||||
| 42 | 4 | ▶ | $options{ SSL_verify_mode } = $self->ssl_verify_server ? SSL_VERIFY_PEER : SSL_VERIFY_NONE; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 4 | ▶ | if (defined $self->ssl_cert_pem) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 45 | 0 | $options{ SSL_cert } = [ IO::Socket::SSL::Utils::PEM_string2cert($self->ssl_cert_pem) ]; | |||||||||||||||||||
| 46 | } elsif (defined $self->ssl_cert_file) { | ||||||||||||||||||||
| 47 | 0 | $options{ SSL_cert_file } = $self->ssl_cert_file; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 4 | ▶ | if (defined $self->ssl_key_pem) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 51 | 0 | $options{ SSL_key } = IO::Socket::SSL::Utils::PEM_string2key($self->ssl_key_pem); | |||||||||||||||||||
| 52 | } elsif (defined $self->ssl_key_file) { | ||||||||||||||||||||
| 53 | 0 | $options{ SSL_key_file } = $self->ssl_key_file; | |||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | 4 | ▶ | if (defined $self->ssl_ca_pem) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 57 | 0 | $options{ SSL_ca } = [ IO::Socket::SSL::Utils::PEM_string2cert($self->ssl_ca_pem) ]; | |||||||||||||||||||
| 58 | } elsif (defined $self->ssl_ca_file) { | ||||||||||||||||||||
| 59 | 1 | $options{ SSL_ca_file } = $self->ssl_ca_file; | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 4 | ▶ | return HTTP::Tiny->new( | ||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$Kubernetes::REST::VERSION // "dev"
MC/DC100%1/1
$Kubernetes::REST::VERSION // "dev"
$Kubernetes::REST::VERSION
Truth table100%2/2
A
A= $Kubernetes::REST::VERSION
| |||||||||||||||||||||
| 63 | agent => 'Kubernetes::REST Perl Client ' . ($Kubernetes::REST::VERSION // 'dev'), | ||||||||||||||||||||
| 64 | timeout => $self->timeout, | ||||||||||||||||||||
| 65 | verify_SSL => $self->ssl_verify_server ? 1 : 0, | ||||||||||||||||||||
| 66 | SSL_options => \%options, | ||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||
| 68 | 4 | }); | |||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 13 | ▶ | sub call { | ||||||||||||||||||
|
Subroutine
call: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 72 | 13 | my ($self, $req) = @_; | |||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 13 | ▶ | my $res = $self->ua->request( | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | $req->method, | ||||||||||||||||||||
| 77 | $req->url, | ||||||||||||||||||||
| 78 | { | ||||||||||||||||||||
| 79 | headers => $req->headers, | ||||||||||||||||||||
| 80 | (defined $req->content) ? (content => $req->content) : (), | ||||||||||||||||||||
| 81 | } | ||||||||||||||||||||
| 82 | ); | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | return Kubernetes::REST::HTTPResponse->new( | ||||||||||||||||||||
| 85 | status => $res->{ status }, | ||||||||||||||||||||
| 86 | (defined $res->{ content })?( content => $res->{ content } ) : (), | ||||||||||||||||||||
| 87 | 13 | ▶ | headers => $self->_headers($res->{ headers }), | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 88 | ); | ||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 8 | ▶ | sub call_streaming { | ||||||||||||||||||
|
Subroutine
call_streaming: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 92 | 8 | my ($self, $req, $data_callback) = @_; | |||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | # HTTP::Tiny turns a die in data_callback into a 599; keep the original | ||||||||||||||||||||
| 96 | # exception and rethrow it unchanged after the request. | ||||||||||||||||||||
| 97 | 8 | my ( $failed, $error ); | |||||||||||||||||||
| 98 | my $res = $self->ua->request( | ||||||||||||||||||||
| 99 | $req->method, | ||||||||||||||||||||
| 100 | $req->url, | ||||||||||||||||||||
| 101 | { | ||||||||||||||||||||
| 102 | headers => $req->headers, | ||||||||||||||||||||
| 103 | 6 | ▶ | data_callback => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 104 | 6 | ▶ | return if $failed; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 105 | 6 | ▶ | unless ( eval { $data_callback->(@_); 1 } ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 2 | ( $failed, $error ) = ( 1, $@ ); | |||||||||||||||||||
| 107 | 2 | die $error; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | }, | ||||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | 8 | ); | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 8 | ▶ | die $error if $failed; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | return Kubernetes::REST::HTTPResponse->new( | ||||||||||||||||||||
| 116 | status => $res->{ status }, | ||||||||||||||||||||
| 117 | (defined $res->{ content })?( content => $res->{ content } ) : (), | ||||||||||||||||||||
| 118 | 6 | ▶ | headers => $self->_headers($res->{ headers }), | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | # HTTP::Tiny's headers - lowercased names, a string for a header that came | ||||||||||||||||||||
| 123 | # once and an arrayref for a repeated one - as an arrayref of values each, in | ||||||||||||||||||||
| 124 | # the order they came (karr k79). A response without headers has none. | ||||||||||||||||||||
| 125 | 19 | ▶ | sub _headers { | ||||||||||||||||||
|
Subroutine
_headers: called CC=4 SCAR=13.9
| |||||||||||||||||||||
| 126 | 19 | my ($self, $headers) = @_; | |||||||||||||||||||
| 127 | 19 | ▶ | return {} unless ref $headers eq 'HASH'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 128 | 9 | my %headers; | |||||||||||||||||||
| 129 | 9 | for my $name (keys %$headers) { | |||||||||||||||||||
| 130 | 17 | my $value = $headers->{$name}; | |||||||||||||||||||
| 131 | 17 | ▶ | $headers{ lc $name } = [ ref $value eq 'ARRAY' ? @$value : $value ]; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | } | ||||||||||||||||||||
| 133 | 9 | return \%headers; | |||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 4 | 1; | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||