Connector/Proxy/HTTP.pm

filter: Statementstmt 68.1% 79 / 116 Branchbran 34.3% 11 / 32 Conditioncond 61.5% 8 / 13 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 85.7% 12 / 14 Podpod 100% 3 / 3 totaltotal 62.9% 117 / 186 CC 23 SCAR 27.0
CC
23
Cov
61%
set
56.0
_sanitize_path
8.0
get
6.3
CRAP 14.9
« Connector/Proxy/Net/LDAP/DN.pm ↑ Summary Connector/Multi.pm »
1 # Proxy class to get/set data using HTTP POST
2 #
3 package Connector::Proxy::HTTP;
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 extends 'Connector::Proxy';
14 1 with qw(
15 Connector::Role::SSLUserAgent
16 Connector::Role::LocalPath
17 );
18  
19 1 has content => (
20 is => 'rw',
21 isa => 'Str',
22 );
23  
24 1 has header => (
25 is => 'ro',
26 isa => 'HashRef',
27 );
28  
29 1 has content_type => (
30 is => 'rw',
31 isa => 'Str',
32 );
33  
34 1 has http_method => (
35 is => 'rw',
36 isa => 'Str',
37 default => 'PUT',
38 );
39  
40 1 has http_auth => (
41 is => 'ro',
42 isa => 'HashRef',
43 );
44  
45 1 has query_param => (
46 is => 'rw',
47 isa => 'HashRef',
48 predicate => 'has_query_param',
49 );
50  
51 1 has undef_on_404 => (
52 is => 'ro',
53 isa => 'Bool',
54 default => 0,
55 );
56  
57 1 has chomp_result => (
58 is => 'ro',
59 isa => 'Bool',
60 default => 0,
61 );
62  
63  
64 5 ▶sub get {
Subroutine get: called CC=4 SCAR=18.4
Pod documented
65 5 my $self = shift;
66 5 my $args = shift;
67  
68 5 my $url = $self->_sanitize_path( $args );
69 5 $self->log()->debug('Make LWP call to ' . $url );
70  
71 5 my $req = HTTP::Request->new('GET' => $url);
72 5 $self->_init_request( $req );
73  
74 5 my $response = $self->agent()->request($req);
75  
76 5 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 5 5
77 0 ▶ if ( $response->code == 404 && $self->undef_on_404()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 404 and $self->undef_on_404) 0 0 0
Condition0%0/3
$response->code == 404 and $self->undef_on_404
!ll&&!rl&&r
000
MC/DC0%0/2
$response->code == 404 and $self->undef_on_404
$response->code == 404 $self->undef_on_404
Truth table0%0/3
A and B
A= $response->code == 404B= $self->undef_on_404
ABresult
0X0
100
111
78 0 $self->log()->warn("Resource not found");
79 0 return $self->_node_not_exists();
80 }
81 0 $self->log()->error($response->status_line);
82 0 die "Unable to retrieve data from server";
83 }
84  
85 5 return $self->_parse_result($response);
86 }
87  
88 0 ▶sub set {
Subroutine set: not called CC=7 SCAR=40.3
Pod documented
89  
90 0 my $self = shift;
91 0 my $args = shift;
92 0 my $data = shift;
93 # build url
94 0 my $url = $self->_sanitize_path( $args, $data );
95 # create content from template
96 0 my $content;
97 0 ▶ if ($self->content()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content) { } 0 0 0
98 0 $self->log()->debug('Process template for content ' . $self->content());
99 0 my $template = Template->new({});
100  
101 0 ▶ $data = { DATA => $data } if (ref $data eq '');
Branch: 1 branch
BranchTrueFalseTotal
if ref $data eq "" 0 0 0
102  
103 0 ▶ $template->process( \$self->content(), $data, \$content) || die "Error processing content template.";
Branch: 1 branch
BranchTrueFalseTotal
$template->process(\$self->content, $data, \$content) or die "Error processing content template." 0 0 0
104 } else {
105 0 ▶ if (ref $data ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data ne "") 0 0 0
106 0 die "You need to define a content template if data is not a scalar";
107 }
108 0 $content = $data;
109 }
110  
111 # create request
112 0 my $req = HTTP::Request->new($self->http_method() => $url);
113 0 $self->_init_request( $req );
114  
115 # set generated content
116 0 $req->content($content);
117  
118 0 my $response = $self->agent()->request($req);
119 # error handling
120 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
121 0 $self->log()->error($response->status_line);
122 0 $self->log()->error($response->decoded_content);
123 0 die "Unable to upload data to server";
124 }
125  
126 0 $self->log()->debug("Set responded with: " . $response->status_line);
127 0 ▶ $self->log()->trace($response->decoded_content) if ($self->log()->is_trace());
Branch: 1 branch
BranchTrueFalseTotal
if $self->log->is_trace 0 0 0
128  
129 0 return 1;
130 }
131  
132 0 ▶sub get_meta {
Subroutine get_meta: not called CC=2 SCAR=17.9
Pod documented
133 0 my $self = shift;
134  
135 # If we have no path, we tell the caller that we are a connector
136 0 my @path = $self->_build_path_with_prefix( shift );
137 0 ▶ if (scalar @path == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @path == 0) 0 0 0
138 0 return { TYPE => "connector" };
139 }
140  
141 0 return {TYPE => "scalar" };
142 }
143  
144  
145 5 ▶sub _init_request {
Subroutine _init_request: called CC=5 SCAR=17.8
146  
147 5 my $self = shift;
148 5 my $req = shift;
149  
150 # use basic auth if supplied
151 5 my $auth = $self->http_auth();
152 5 ▶ if ($auth){
Branch: 1 branch
BranchTrueFalseTotal
if ($auth) 0 5 5
153 0 $req->authorization_basic($auth->{user}, $auth->{pass});
154 }
155  
156 # set content_type if supplied (does not make much sense with GET)
157 5 ▶ if ($self->content_type()){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->content_type) 0 5 5
158 0 $req->content_type($self->content_type());
159 }
160  
161 # extra headers
162 5 my $header = $self->header();
163 5 foreach my $key (%{$header}) {
164 0 $req->header($key, $header->{$key} );
165 }
166  
167 5 ▶ $self->log()->trace(Dumper $req) if ($self->log()->is_trace());
Branch: 1 branch
BranchTrueFalseTotal
if $self->log->is_trace 0 5 5
168  
169 }
170  
171 5 ▶sub _sanitize_path {
Subroutine _sanitize_path: called CC=8 SCAR=20.8
172  
173 5 my $self = shift;
174 5 my $inargs = shift;
175 5 ▶ my $data = shift || {};
Condition50%1/2
shift() || {}
l!l
05
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
176  
177 5 my @args = $self->_build_path_with_prefix( $inargs );
178  
179 5 my $file;
180 5 ▶ if ($self->file() || $self->path()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file or $self->path) { } 3 2 5
Condition100%3/3
$self->file or $self->path
l!l&&r!l&&!r
212
MC/DC100%2/2
$self->file or $self->path
$self->file $self->path
Truth table100%3/3
A or B
A= $self->fileB= $self->path
ABresult
1X1
011
000
181 3 $file = $self->_render_local_path( \@args, $data );
182 } else {
183 2 $file = join("/", @args);
184 }
185  
186 5 my $filename = $self->LOCATION();
187 5 ▶ if (defined $file && $file ne "") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and $file ne "") 4 1 5
Condition66%2/3
defined $file and $file ne ""
!ll&&!rl&&r
014
MC/DC50%1/2
defined $file and $file ne ""
defined $file $file ne ""
Truth table66%2/3
A and B
A= defined $fileB= $file ne ""
ABresult
0X0
100
111
188 4 $filename .= '/'.$file;
189 }
190  
191 5 $self->log()->debug('Filename evaluated to ' . $filename);
192  
193 5 ▶ if ($self->has_query_param()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_query_param) 1 4 5
194 1 my $param = $self->query_param;
195 my @chunks = map {
196 1 ▶ $_ .'='. uri_escape($param->{$_}//'')
Condition100%2/2
$param->{$_} // ""
l!l
11
MC/DC100%1/1
$param->{$_} // ""
$param->{$_}
Truth table100%2/2
A
A= $param->{$_}
Aresult
11
00
197 } keys %$param;
198 1 my $qsa = join('&', @chunks);
199 1 $self->log()->debug('Attach extra query params ' . $qsa);
200 1 $filename .= '?'.$qsa;
201 }
202  
203 5 return $filename;
204 }
205  
206 5 ▶sub _parse_result {
Subroutine _parse_result: called CC=2 SCAR=7.0
207  
208 5 my $self = shift;
209 5 my $response = shift;
210  
211 5 my $res = $response->decoded_content;
212 5 ▶ chomp $res if ($self->chomp_result());
Branch: 1 branch
BranchTrueFalseTotal
if $self->chomp_result 0 5 5
213 5 return $res;
214 }
215  
216  
217 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
218 1 __PACKAGE__->meta->make_immutable;
219  
220 1 1;
221  
222 __END__
« Connector/Proxy/Net/LDAP/DN.pm ↑ Summary Connector/Multi.pm »