File Coverage

blib/lib/cPanel/APIClient/TransportBase/HTTPBase.pm
Criterion Covered Total %
statement 30 31 96.7
branch 3 6 50.0
condition n/a
subroutine 7 7 100.0
pod 0 1 0.0
total 40 45 88.8


line stmt bran cond sub pod time code
1             package cPanel::APIClient::TransportBase::HTTPBase;
2              
3             # Copyright 2020 cPanel, L. L. C.
4             # All rights reserved.
5             # http://cpanel.net
6             #
7             # This is free software; you can redistribute it and/or modify it under the
8             # same terms as Perl itself. See L.
9              
10 2     2   939 use strict;
  2         5  
  2         66  
11 2     2   11 use warnings;
  2         4  
  2         54  
12              
13 2     2   10 use parent qw( cPanel::APIClient::Transport );
  2         5  
  2         28  
14              
15             my @_REQUIRED = ('hostname');
16              
17             sub new {
18 7     7 0 27 my ( $class, $authn, %opts ) = @_;
19              
20 7 50       62 if ( $authn->isa('cPanel::APIClient::Authn::Username') ) {
21 0         0 die("HTTP-based transport $class doesn’t work with Username authentication.");
22             }
23              
24 7         21 my @missing = grep { !defined $opts{$_} } @_REQUIRED;
  7         109  
25 7 50       26 die( __PACKAGE__ . ": Missing @missing" ) if @missing;
26              
27             return bless {
28             authn => $authn,
29 7         43 hostname => $opts{'hostname'},
30             }, $class;
31             }
32              
33             sub _get_url_base {
34 7     7   18 my ( $self, $service_obj ) = @_;
35              
36 7         21 my $url = "https://" . $self->{'hostname'};
37 7 50       27 if ( my $port = $service_obj->get_https_port() ) {
38 7         24 $url .= ":$port";
39             }
40              
41 7         16 return $url;
42             }
43              
44             sub _needs_session {
45 14     14   53 my ($self) = @_;
46              
47 14         63 return $self->{'authn'}->needs_session();
48             }
49              
50             sub _assemble_request_pieces {
51 7     7   18 my ( $self, $service_obj, $request_obj ) = @_;
52              
53 7         34 my $method = $request_obj->get_http_method();
54              
55             my @headers = (
56 7         26 $self->{'authn'}->get_http_headers_for_service($service_obj),
57             $request_obj->get_http_headers(),
58             );
59              
60 7         28 my $payload = $request_obj->get_http_payload();
61              
62 7         32 my $url = $self->_get_url_base($service_obj);
63 7         44 $url .= $self->{'authn'}->get_url_path_prefix();
64 7         26 $url .= $request_obj->get_http_url_path();
65              
66 7         35 return ( $method, $url, \@headers, $payload );
67             }
68              
69             1;