APIClient/Transport/HTTPSync.pm

filter: Statementstmt 100% 58 / 58 Branchbran 75.0% 9 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 89.8% 80 / 89 CC 8 SCAR 8.7
CC
8
Cov
93%
request
5.1
as_string
2.0
_get_session
2.0
CRAP 2.4
« APIClient/Service.pm ↑ Summary APIClient/Utils/HTTPSession.pm »
1 package cPanel::APIClient::Transport::HTTPSync;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =encoding utf-8
7  
8 =head1 NAME
9  
10 cPanel::APIClient::Transport::HTTPSync - Synchronous HTTP transport
11  
12 =head1 SYNOPSIS
13  
14 my $remote_cp = cPanel::APIClient->create(
15 service => 'cpanel',
16  
17 transport => [
18 'HTTPSync',
19 hostname => 'somewhere.out.there',
20  
21 # Not for production:
22 # tls_verification => 'off',
23 ],
24  
25 credentials => {
26 username => 'johnny',
27 password => '$3kr1t',
28 },
29 );
30  
31 my $resp = $remote_cp->call_uapi( 'Email', 'list_forwarders' );
32  
33 =head1 DESCRIPTION
34  
35 This transport mechanism implements access to cPanel & WHM’s APIs
36 via synchronous (i.e., blocking) HTTP.
37  
38 =head1 SEE ALSO
39  
40 L<cPanel::APIClient::Transport::NetCurlPromiser> and
41 L<cPanel::APIClient::Transport::Mojolicious> facilitate sending
42 multiple concurrent API requests.
43  
44 =cut
45  
46 #----------------------------------------------------------------------
47  
48 2 ▶use parent qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
49 cPanel::APIClient::TransportBase::HTTPBase
50 cPanel::APIClient::TransportBase::TLSVerificationBase
51 2 );
52  
53 2 ▶use HTTP::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 2 ▶use cPanel::APIClient::Utils::HTTPResponse ();
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 #----------------------------------------------------------------------
58  
59 8 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
60 8 my ( $class, $authn, %opts ) = @_;
61  
62 8 my $self = $class->SUPER::new( $authn, %opts );
63  
64 8 my @ht_args;
65  
66 8 my $verify_SSL = ('off' ne $self->_parse_tls_verification( \%opts ) );
67  
68 8 push @ht_args, ( verify_SSL => $verify_SSL );
69  
70 8 ▶ if ( $self->_needs_session() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_needs_session) 1 7 8
71 1 require HTTP::CookieJar;
72 1 push @ht_args, ( cookie_jar => HTTP::CookieJar->new() );
73 }
74  
75 8 $self->{'ua'} = HTTP::Tiny->new(@ht_args);
76  
77 8 return $self;
78 }
79  
80 1 ▶sub _get_session {
Subroutine _get_session: called CC=2 SCAR=7.0
81 1 my ( $self, $service_obj ) = @_;
82  
83 1 my ( $method, $url, $payload ) = $self->{'authn'}->get_login_request_pieces();
84 1 substr( $url, 0, 0, $self->_get_url_base($service_obj) );
85  
86 1 ▶ die "Bad method: $method" if 'POST' ne $method;
Branch: 1 branch
BranchTrueFalseTotal
if "POST" ne $method 0 1 1
87  
88 1 my $resp = $self->{'ua'}->post(
89 $url,
90 { content => $payload },
91 );
92  
93 1 my $resp_obj = cPanel::APIClient::Transport::HTTPSync::Response->new($resp);
94  
95 1 $self->{'authn'}->consume_session_response($resp_obj);
96  
97 1 return;
98 }
99  
100 8 ▶sub request {
Subroutine request: called CC=5 SCAR=16.2
Pod undocumented
101 8 my ( $self, $service_obj, $request_obj ) = @_;
102  
103 8 ▶ if ( $self->_needs_session() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_needs_session) 1 7 8
104 1 ▶ $self->{'_got_session'} ||= do {
Condition33%1/3
$self->{"_got_session"} ||= do { $self->_get_session($service_obj); 1 }
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"_got_session"} ||= do { $self->_get_session($service_obj); 1 }
$self->{"_got_session"} do { $self->_get_session($service_obj); 1 }
Truth table33%1/3
A ||= B
A= $self->{"_got_session"}B= do { $self->_get_session($service_obj); 1 }
ABresult
1X1
011
000
105 1 $self->_get_session($service_obj);
106 1 1;
107 };
108 }
109  
110 8 my ( $method, $url, $headers_ar, $payload ) = $self->_assemble_request_pieces( $service_obj, $request_obj );
111  
112 8 ▶ die "Bad method: $method" if 'POST' ne $method;
Branch: 1 branch
BranchTrueFalseTotal
if "POST" ne $method 0 8 8
113  
114 my $resp = $self->{'ua'}->post(
115 $url,
116 {
117 8 headers => { map { @$_ } @$headers_ar },
118 content => $payload,
119 },
120 );
121  
122 8 ▶ if ( $resp->{'status'} == 599 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{"status"} == 599) 2 6 8
123 2 die $request_obj->create_transport_error( $resp->{'content'} );
124 }
125  
126 6 my $resp_obj = cPanel::APIClient::Transport::HTTPSync::Response->new($resp);
127  
128 6 return $request_obj->parse_http_response( $resp_obj, $resp->{'content'} );
129 }
130  
131 #----------------------------------------------------------------------
132  
133 package cPanel::APIClient::Transport::HTTPSync::Response;
134  
135 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
136 7 my ( $class, $struct ) = @_;
137  
138 7 return bless $struct, $class;
139 }
140  
141 6 ▶sub code {
Subroutine code: called CC=1 SCAR=0.0
142 6 return $_[0]{'status'};
143 }
144  
145 1 ▶sub header {
Subroutine header: called CC=1 SCAR=0.0
146 1 my ( $self, $name ) = @_;
147  
148 1 return $self->{'headers'}{$name};
149 }
150  
151 1 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=7.0
152 1 my ($self) = @_;
153  
154 1 my $hdrs = $self->{'headers'};
155  
156 return join(
157 "\x0d\x0a",
158 3 join( q< >, grep { defined } @{$self}{ 'protocol', 'status', 'reason' } ),
159 1 ▶ ( map { "$_: " . ( defined $hdrs->{$_} ? $hdrs->{$_} : q<> ) } keys %$hdrs ),
Branch: 1 branch
BranchTrueFalseTotal
defined $hdrs->{$_} ? : 2 0 2
160 );
161 }
162  
163 =head1 LICENSE
164  
165 Copyright 2020 cPanel, L. L. C. All rights reserved. L<http://cpanel.net>
166  
167 This is free software; you can redistribute it and/or modify it under the
168 same terms as Perl itself. See L<perlartistic>.
169  
170 =cut
171  
172 2 1;
« APIClient/Service.pm ↑ Summary APIClient/Utils/HTTPSession.pm »