blib/lib/REST/Client.pm

filter: Statementstmt 77.8% 162 / 208 Branchbran 42.0% 21 / 50 Conditioncond 37.5% 6 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 85.4% 41 / 48 Podpod 100% 15 / 15 totaltotal 70.6% 245 / 347 CC 35 SCAR 31.5
CC
35
Cov
69%
request
84.8
_prepareURL
6.6
addHeader
6.0
CRAP 23.4
↑ Summary
1 package REST::Client;
2 #ABSTRACT: A simple client for interacting with RESTful http/https resources
3  
4 1 our $VERSION = '281';
5  
6  
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use 5.008_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 3 ▶sub new {
Subroutine new: called CC=4 SCAR=14.3
Pod documented
21 3 my $class = shift;
22 3 my $config;
23  
24 3 $class->_buildAccessors();
25  
26 3 ▶ if(ref $_[0] eq 'HASH'){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 2 1 3
elsif (scalar @_ and scalar @_ % 2 == 0) { } 0 1 1
27 2 $config = shift;
28 ▶ }elsif(scalar @_ && scalar @_ % 2 == 0){
Condition33%1/3
scalar @_ and scalar @_ % 2 == 0
!ll&&!rl&&r
100
MC/DC0%0/2
scalar @_ and scalar @_ % 2 == 0
scalar @_ scalar @_ % 2 == 0
Truth table33%1/3
A and B
A= scalar @_B= scalar @_ % 2 == 0
ABresult
0X0
100
111
29 0 $config = {@_};
30 }else{
31 1 $config = {};
32 }
33  
34 3 my $self = bless({}, $class);
35 3 $self->{'_config'} = $config;
36  
37 3 $self->_buildUseragent();
38  
39 3 return $self;
40 }
41  
42  
43 0 ▶sub addHeader {
Subroutine addHeader: not called CC=2 SCAR=17.9
Pod documented
44 0 my $self = shift;
45 0 my $header = shift;
46 0 my $value = shift;
47
48 0 ▶ my $headers = $self->{'_headers'} || {};
Condition0%0/2
$self->{'_headers'} || {}
l!l
00
MC/DC0%0/1
$self->{'_headers'} || {}
$self->{'_headers'}
Truth table0%0/2
A
A= $self->{'_headers'}
Aresult
11
00
49 0 $headers->{$header} = $value;
50 0 $self->{'_headers'} = $headers;
51 0 return;
52 }
53  
54  
55 0 ▶sub buildQuery {
Subroutine buildQuery: not called CC=1 SCAR=6.9
Pod documented
56 0 my $self = shift;
57  
58 0 my $uri = URI->new();
59 0 $uri->query_form(@_);
60 0 return $uri->as_string();
61 }
62  
63  
64  
65  
66 7 ▶sub GET {
Subroutine GET: called CC=1 SCAR=0.0
Pod documented
67 7 my $self = shift;
68 7 my $url = shift;
69 7 my $headers = shift;
70 7 return $self->request('GET', $url, undef, $headers);
71 }
72  
73  
74 2 ▶sub PUT {
Subroutine PUT: called CC=1 SCAR=0.0
Pod documented
75 2 my $self = shift;
76 2 return $self->request('PUT', @_);
77 }
78  
79  
80 2 ▶sub PATCH {
Subroutine PATCH: called CC=1 SCAR=0.0
Pod documented
81 2 my $self = shift;
82 2 return $self->request('PATCH', @_);
83 }
84  
85  
86 2 ▶sub POST {
Subroutine POST: called CC=1 SCAR=0.0
Pod documented
87 2 my $self = shift;
88 2 return $self->request('POST', @_);
89 }
90  
91  
92 2 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
93 2 my $self = shift;
94 2 my $url = shift;
95 2 my $headers = shift;
96 2 return $self->request('DELETE', $url, undef, $headers);
97 }
98  
99  
100 2 ▶sub OPTIONS {
Subroutine OPTIONS: called CC=1 SCAR=0.0
Pod documented
101 2 my $self = shift;
102 2 my $url = shift;
103 2 my $headers = shift;
104 2 return $self->request('OPTIONS', $url, undef, $headers);
105 }
106  
107  
108 2 ▶sub HEAD {
Subroutine HEAD: called CC=1 SCAR=0.0
Pod documented
109 2 my $self = shift;
110 2 my $url = shift;
111 2 my $headers = shift;
112 2 return $self->request('HEAD', $url, undef, $headers);
113 }
114  
115  
116 21 ▶sub request {
Subroutine request: called CC=21 SCAR=44.4
Pod documented
117 21 my $self = shift;
118 21 my $method = shift;
119 21 my $url = shift;
120 21 my $content = shift;
121 21 my $headers = shift;
122  
123 21 $self->{'_res'} = undef;
124 21 $self->_buildUseragent();
125  
126  
127 #error check
128 21 ▶ croak "REST::Client exception: Must provide a url to $method" unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 21 21
129 21 ▶ croak "REST::Client exception: headers must be presented as a hashref" if $headers && ref $headers ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $headers and ref $headers ne "HASH" 0 21 21
Condition66%2/3
$headers and ref $headers ne "HASH"
!ll&&!rl&&r
1920
MC/DC0%0/2
$headers and ref $headers ne "HASH"
$headers ref $headers ne "HASH"
Truth table66%2/3
A and B
A= $headersB= ref $headers ne "HASH"
ABresult
0X0
100
111
130  
131  
132 21 $url = $self->_prepareURL($url);
133  
134 21 my $ua = $self->getUseragent();
135 21 ▶ if(defined $self->getTimeout()){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->getTimeout) { } 0 21 21
136 0 $ua->timeout($self->getTimeout);
137 }else{
138 21 $ua->timeout(300);
139 }
140 21 my $req = HTTP::Request->new( $method => $url );
141  
142 #build headers
143 21 ▶ if(defined $content && length($content)){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content and length $content) { } 0 21 21
Condition66%2/3
defined $content and length $content
!ll&&!rl&&r
1920
MC/DC0%0/2
defined $content and length $content
defined $content length $content
Truth table66%2/3
A and B
A= defined $contentB= length $content
ABresult
0X0
100
111
144 0 $req->content($content);
145 0 $req->header('Content-Length', length($content));
146 }else{
147 21 $req->header('Content-Length', 0);
148 }
149  
150 21 ▶ my $custom_headers = $self->{'_headers'} || {};
Condition50%1/2
$self->{'_headers'} || {}
l!l
021
MC/DC0%0/1
$self->{'_headers'} || {}
$self->{'_headers'}
Truth table50%1/2
A
A= $self->{'_headers'}
Aresult
11
00
151 21 for my $header (keys %$custom_headers){
152 0 $req->header($header, $custom_headers->{$header});
153 }
154  
155 21 for my $header (keys %$headers){
156 0 $req->header($header, $headers->{$header});
157 }
158  
159  
160 #prime LWP with ssl certfile if we have values
161 21 ▶ if($self->getCert){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getCert) 0 21 21
162 0 ▶ carp "REST::Client exception: Certs defined but not using https" unless $url =~ /^https/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /^https/ 0 0 0
163 0 ▶ croak "REST::Client exception: Cannot read cert and key file" unless -f $self->getCert && -f $self->getKey;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->getCert and -f $self->getKey 0 0 0
Condition0%0/3
-f $self->getCert and -f $self->getKey
!ll&&!rl&&r
000
MC/DC0%0/2
-f $self->getCert and -f $self->getKey
-f $self->getCert -f $self->getKey
Truth table0%0/3
A and B
A= -f $self->getCertB= -f $self->getKey
ABresult
0X0
100
111
164  
165 0 $ua->ssl_opts(SSL_cert_file => $self->getCert);
166 0 $ua->ssl_opts(SSL_key_file => $self->getKey);
167 }
168
169 #prime LWP with CA file if we have one
170 21 ▶ if(my $ca = $self->getCa){
Branch: 1 branch
BranchTrueFalseTotal
if (my $ca = $self->getCa) 0 21 21
171 0 ▶ croak "REST::Client exception: Cannot read CA file" unless -f $ca;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $ca 0 0 0
172 0 $ua->ssl_opts(SSL_ca_file => $ca);
173 }
174  
175 #prime LWP with PKCS12 certificate if we have one
176 21 ▶ if($self->getPkcs12){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getPkcs12) 0 21 21
177 0 ▶ carp "REST::Client exception: PKCS12 cert defined but not using https" unless $url =~ /^https/;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ /^https/ 0 0 0
178 0 ▶ croak "REST::Client exception: Cannot read PKCS12 cert" unless -f $self->getPkcs12;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $self->getPkcs12 0 0 0
179  
180 0 $ENV{HTTPS_PKCS12_FILE} = $self->getPkcs12;
181 0 ▶ if($self->getPkcs12password){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getPkcs12password) 0 0 0
182 0 $ENV{HTTPS_PKCS12_PASSWORD} = $self->getPkcs12password;
183 }
184 }
185  
186 21 ▶ my $res = $self->getFollow ?
Branch: 1 branch
BranchTrueFalseTotal
$self->getFollow ? : 0 21 21
187 $ua->request( $req, $self->getContentFile ) :
188 $ua->simple_request( $req, $self->getContentFile );
189  
190 21 $self->{_res} = $res;
191  
192 21 return $self;
193 }
194  
195  
196 3 ▶sub responseCode {
Subroutine responseCode: called CC=1 SCAR=0.0
Pod documented
197 3 my $self = shift;
198 3 return $self->{_res}->code;
199 }
200  
201  
202 7 ▶sub responseContent {
Subroutine responseContent: called CC=1 SCAR=0.0
Pod documented
203 7 my $self = shift;
204 7 return $self->{_res}->content;
205 }
206  
207  
208 1 ▶sub responseHeaders {
Subroutine responseHeaders: called CC=1 SCAR=0.0
Pod documented
209 1 my $self = shift;
210 1 return $self->{_res}->headers()->header_field_names();
211 }
212  
213  
214  
215  
216 1 ▶sub responseHeader {
Subroutine responseHeader: called CC=2 SCAR=7.0
Pod documented
217 1 my $self = shift;
218 1 my $header = shift;
219 1 ▶ croak "REST::Client exception: no header provided to responseHeader" unless $header;
Branch: 1 branch
BranchTrueFalseTotal
unless $header 0 1 1
220 1 return $self->{_res}->header($header);
221 }
222  
223  
224 0 ▶sub responseXpath {
Subroutine responseXpath: not called CC=2 SCAR=17.9
Pod documented
225 0 my $self = shift;
226  
227 0 require XML::LibXML;
228  
229 0 my $xml= XML::LibXML->new();
230 0 $xml->load_ext_dtd(0);
231  
232 0 ▶ if($self->responseHeader('Content-type') =~ /html/){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->responseHeader('Content-type') =~ /html/) { } 0 0 0
233 0 return XML::LibXML::XPathContext->new($xml->parse_html_string( $self->responseContent() ));
234 }else{
235 0 return XML::LibXML::XPathContext->new($xml->parse_string( $self->responseContent() ));
236 }
237 }
238  
239 # Private methods
240  
241 21 ▶sub _prepareURL {
Subroutine _prepareURL: called CC=6 SCAR=18.8
242 21 my $self = shift;
243 21 my $url = shift;
244  
245 # Do not prepend default host to absolute URLs.
246 21 ▶ return $url if $url =~ /^https?:/;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /^https?:/ 0 21 21
247  
248 21 my $host = $self->getHost;
249 21 ▶ if($host){
Branch: 1 branch
BranchTrueFalseTotal
if ($host) 21 0 21
250 21 ▶ $url = '/'.$url unless $url =~ /^\//;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[^/] 0 21 21
251 21 $url = $host . $url;
252 }
253 21 ▶ unless($url =~ /^\w+:\/\//){
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ m[^\w+://]) 21 0 21
254 21 ▶ $url = ($self->getCert ? 'https://' : 'http://') . $url;
Branch: 1 branch
BranchTrueFalseTotal
$self->getCert ? : 0 21 21
255 }
256  
257 21 return $url;
258 }
259  
260 24 ▶sub _buildUseragent {
Subroutine _buildUseragent: called CC=2 SCAR=6.9
261 24 my $self = shift;
262  
263 24 ▶ return if $self->getUseragent();
Branch: 1 branch
BranchTrueFalseTotal
if $self->getUseragent 21 3 24
264  
265 3 my $ua = LWP::UserAgent->new;
266 3 $ua->agent("REST::Client/$VERSION");
267 3 $self->setUseragent($ua);
268  
269 3 return;
270 }
271  
272 3 ▶sub _buildAccessors {
Subroutine _buildAccessors: called CC=3 SCAR=11.1
273 3 my $self = shift;
274  
275 3 ▶ return if $self->can('setHost');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("setHost") 2 1 3
276  
277 1 my @attributes = qw(Host Key Cert Ca Timeout Follow Useragent Pkcs12 Pkcs12password ContentFile);
278  
279 1 for my $attribute (@attributes){
280 10 my $set_method = "
281 sub {
282 my \$self = shift;
283 \$self->{'_config'}{lc('$attribute')} = shift;
284 return \$self->{'_config'}{lc('$attribute')};
285 }";
286  
287 10 my $get_method = "
288 sub {
289 my \$self = shift;
290 return \$self->{'_config'}{lc('$attribute')};
291 }";
292  
293  
294 {
295 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
296 0 ▶ *{'REST::Client::set'.$attribute} = eval $set_method ;
Subroutine __ANON__: called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
Subroutine __ANON__: called CC=1 SCAR=0.2
297 0 ▶ *{'REST::Client::get'.$attribute} = eval $get_method ;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
298 }
299  
300 }
301  
302 1 return;
303 }
304  
305 1 1;
306  
307 __END__
↑ Summary