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   897 use strict;
  2         7  
  2         55  
11 2     2   11 use warnings;
  2         4  
  2         52  
12              
13 2     2   9 use parent qw( cPanel::APIClient::Transport );
  2         4  
  2         37  
14              
15             my @_REQUIRED = ('hostname');
16              
17             sub new {
18 7     7 0 29 my ( $class, $authn, %opts ) = @_;
19              
20 7 50       66 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         124  
25 7 50       41 die( __PACKAGE__ . ": Missing @missing" ) if @missing;
26              
27             return bless {
28             authn => $authn,
29 7         52 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       28 if ( my $port = $service_obj->get_https_port() ) {
38 7         25 $url .= ":$port";
39             }
40              
41 7         19 return $url;
42             }
43              
44             sub _needs_session {
45 14     14   35 my ($self) = @_;
46              
47 14         55 return $self->{'authn'}->needs_session();
48             }
49              
50             sub _assemble_request_pieces {
51 7     7   19 my ( $self, $service_obj, $request_obj ) = @_;
52              
53 7         34 my $method = $request_obj->get_http_method();
54              
55             my @headers = (
56 7         31 $self->{'authn'}->get_http_headers_for_service($service_obj),
57             $request_obj->get_http_headers(),
58             );
59              
60 7         31 my $payload = $request_obj->get_http_payload();
61              
62 7         34 my $url = $self->_get_url_base($service_obj);
63 7         40 $url .= $self->{'authn'}->get_url_path_prefix();
64 7         26 $url .= $request_obj->get_http_url_path();
65              
66 7         37 return ( $method, $url, \@headers, $payload );
67             }
68              
69             1;