Client.pm

filter: Statementstmt 41.8% 54 / 129 Branchbran 12.5% 5 / 40 Conditioncond 17.6% 3 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 78.9% 15 / 19 Podpod 71.4% 5 / 7 totaltotal 36.9% 82 / 222 CC 28 SCAR 32.5
CC
28
Cov
33%
put_object
68.0
send_request
56.0
new
13.1
CRAP 25.7
↑ Summary Object.pm »
1 package Net::STF::Client;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Net::STF::Bucket;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Net::STF::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Furl::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Status ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 use Class::Accessor::Lite
12 1 ▶ rw => [ qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 furl
14 agent_name
15 repl_count
16 url
17 error
18 username
19 password
20 ) ]
21 1 ;
22  
23 1 our $VERSION = '1.01';
24  
25 1 ▶sub new {
Subroutine new: called CC=7 SCAR=25.7
Pod documented
26 1 my $class = shift;
27  
28 # Grr, allow hashrefs
29 1 ▶ if (@_ == 1 && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq "HASH") 0 1 1
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
30 0 @_ = %{$_[0]};
31 }
32  
33 # Only exists to provide back compat with non-published version.
34 # You shouldn't be using this
35 1 ▶ if ( @_ == 2 && ! ref $_[0] && ref $_[1] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and not ref $_[0] and ref $_[1] eq "HASH") 0 1 1
Condition33%2/6
@_ == 2 and not ref $_[0] and ref $_[1] eq "HASH"
!ll&&!rl&&r
100
@_ == 2 and not ref $_[0]
!ll&&!rl&&r
100
MC/DC0%0/3
@_ == 2 and not ref $_[0] and ref $_[1] eq "HASH"
@_ == 2 not ref $_[0] ref $_[1] eq "HASH"
Truth table25%1/4
A and not(B) and C
A= @_ == 2B= not ref $_[0]C= ref $_[1] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
36 # Net::STF style
37 0 @_ = ( url => $_[0], %{$_[1]} );
38 }
39  
40 1 my $self = bless {
41 agent_name => join( '/', $class, $VERSION ),
42 repl_count => 3,
43 @_
44 }, $class;
45  
46 1 ▶ if (! $self->furl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->furl) 1 0 1
47 1 $self->furl(Furl::HTTP->new( agent => $self->agent_name ));
48 }
49  
50 1 return $self;
51 }
52  
53 14 ▶sub _url_is_object {
Subroutine _url_is_object: called CC=2 SCAR=7.6
54 # Make sure the URL contains more than one level in its path.
55 # Otherwise, you may end up getting stuff like
56 # DELETE http://stf.example.com/foo/
57 # instead of
58 # DELETE http://stf.example.com/foo/bar
59 # The former deletes the BUCKET, whereas the latter deletes the object.
60  
61 # XXX regex copied from URI.pm
62  
63 14 my (undef, undef, $path) =
64 $_[0] =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
65 14 ▶ if ($path =~ m{^/+[^/]+(?:/+[\./]*)?$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ m[^/+[^/]+(?:/+[\./]*)?$]) 14 0 14
66 14 Carp::croak("Invalid object URL given -> $_[0]");
67 }
68 0 1;
69 }
70  
71 14 ▶sub _qualify_url {
Subroutine _qualify_url: called CC=2 SCAR=7.4
72 14 my ($self, $url) = @_;
73  
74 14 my $prefix = $self->url;
75 14 ▶ if (! $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prefix) 14 0 14
76 14 return $url;
77 }
78  
79 0 return URI->new($url)->abs($prefix)->canonical;
80 }
81  
82 0 ▶sub get_object {
Subroutine get_object: not called CC=2 SCAR=17.9
Pod documented
83 0 my ($self, $url, $opts) = @_;
84  
85 0 $self->error(undef);
86  
87 0 $url = $self->_qualify_url($url);
88  
89 0 my %furlopts = (
90 method => 'GET',
91 url => $url,
92 );
93 0 my @res = $self->send_request( \%furlopts, $opts );
94 0 ▶ if ( ! HTTP::Status::is_success( $res[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&HTTP::Status::is_success($res[1])) 0 0 0
95 0 $self->error( $res[2] );
96 0 return;
97 }
98 0 return Net::STF::Object->new(
99 url => $url,
100 content => $res[4]
101 );
102 }
103  
104 0 ▶sub send_request {
Subroutine send_request: not called CC=7 SCAR=40.3
Pod undocumented
105 0 my ($self, $furlopts, $opts) = @_;
106  
107 0 ▶ $furlopts->{headers} ||= [];
Condition0%0/2
$furlopts->{"headers"} ||= []
l!l
00
MC/DC0%0/1
$furlopts->{"headers"} ||= []
$furlopts->{"headers"}
Truth table0%0/2
A
A= $furlopts->{"headers"}
Aresult
11
00
108  
109 0 my ($username, $password) = ( $self->username, $self->password );
110 0 ▶ if (defined $username && defined $password ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $username and defined $password) 0 0 0
Condition0%0/3
defined $username and defined $password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and defined $password
defined $username defined $password
Truth table0%0/3
A and B
A= defined $usernameB= defined $password
ABresult
0X0
100
111
111 0 push @{ $furlopts->{headers} },
112 ( 'Authorization' => "Basic " . MIME::Base64::encode("$username:$password", "") );
113 }
114  
115 0 ▶ if ( $opts->{headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"headers"}) 0 0 0
116 0 push @{$furlopts->{headers}}, @{$opts->{headers}};
117 }
118 0 foreach my $key ( qw(write_file write_code) ) {
119 0 ▶ if (my $value = $opts->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $opts->{$key}) 0 0 0
120 0 $furlopts->{$key} = $value;
121 }
122 }
123 0 return $self->furl->request( %$furlopts );
124 }
125  
126 7 ▶sub put_object {
Subroutine put_object: called CC=9 SCAR=42.2
Pod documented
127 7 my ($self, $url, $content, $opts) = @_;
128  
129 7 $self->error(undef);
130  
131 7 $url = $self->_qualify_url($url);
132 7 _url_is_object($url);
133  
134 0 ▶ if (! defined $content ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 0 0
135 0 Carp::croak( "No content provided" );
136 }
137  
138 0 ▶ if ( ref $content eq 'SCALAR' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $content eq 'SCALAR') { } 0 0 0
elsif (&Scalar::Util::openhandle($content)) { } 0 0 0
139 # raw string passed
140 0 $content = $$content;
141 } elsif ( Scalar::Util::openhandle( $content ) ) {
142 # make sure we're at the beginning of the file
143 0 seek $content, 0, 0;
144 } else {
145 # assume it's a file.
146 0 ▶ open my $fh, '<', $content
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $content or die "Failed to open file ${content}: $!" 0 0 0
147 or die "Failed to open file $content: $!";
148 0 $content = $fh;
149 0 seek $content, 0, 0;
150 }
151  
152 0 my @hdrs;
153 push @hdrs,
154 0 ▶ ("X-STF-Replication-Count", ($opts->{repl_count} || $self->repl_count));
Condition0%0/3
$opts->{'repl_count'} || $self->repl_count
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'repl_count'} || $self->repl_count
$opts->{'repl_count'} $self->repl_count
Truth table0%0/3
A || B
A= $opts->{'repl_count'}B= $self->repl_count
ABresult
1X1
011
000
155 0 ▶ if (my $consistency = $opts->{consistency}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $consistency = $opts->{"consistency"}) 0 0 0
156 0 push @hdrs, "X-STF-Consistency", $consistency;
157 }
158  
159  
160 0 my %furlopts = (
161 method => 'PUT',
162 url => $url,
163 headers => \@hdrs,
164 content => $content,
165 );
166 0 my @res = $self->send_request( \%furlopts, $opts );
167 0 ▶ if (! HTTP::Status::is_success($res[1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&HTTP::Status::is_success($res[1])) 0 0 0
168 0 $self->error( $res[2] );
169 0 return;
170 }
171  
172 # if you don't want a result, then we don't create an object
173 0 ▶ return if !defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
174  
175 0 return Net::STF::Object->new(
176 url => $url,
177 # content => $res[4]
178 );
179 }
180  
181 7 ▶sub delete_object {
Subroutine delete_object: called CC=2 SCAR=11.6
Pod documented
182 7 my ($self, $url, $opts) = @_;
183  
184 7 $self->error(undef);
185  
186 7 $url = $self->_qualify_url($url);
187 7 _url_is_object($url);
188  
189 0 my %furlopts = (
190 method => 'DELETE',
191 url => $url,
192 );
193 0 my @res = $self->send_request( \%furlopts, $opts );
194 0 ▶ if ( ! HTTP::Status::is_success( $res[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&HTTP::Status::is_success($res[1])) 0 0 0
195 0 $self->error( $res[2] );
196 0 return;
197 }
198 0 return 1;
199 }
200  
201 0 ▶sub create_bucket {
Subroutine create_bucket: not called CC=2 SCAR=17.9
Pod undocumented
202 0 my ($self, $url, $opts) = @_;
203  
204 0 $self->error(undef);
205  
206 0 $url = $self->_qualify_url($url);
207  
208 0 my %furlopts = (
209 method => 'PUT',
210 url => $url,
211 headers => [
212 'Content-Length' => 0,
213 ],
214 );
215 0 my @res = $self->send_request( \%furlopts, $opts );
216 0 ▶ if (! HTTP::Status::is_success( $res[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&HTTP::Status::is_success($res[1])) 0 0 0
217 0 $self->error( $res[2] );
218 0 return;
219 }
220  
221 0 return Net::STF::Bucket->new(
222 client => $self,
223 name => ( URI->new($url)->path =~ m{^/([^/]+)} )[0],
224 );
225 }
226  
227 0 ▶sub delete_bucket {
Subroutine delete_bucket: not called CC=3 SCAR=24.8
Pod documented
228 0 my ($self, $url, $opts) = @_;
229  
230 0 $self->error(undef);
231  
232 0 my @hdrs;
233 0 ▶ if ( $opts->{recursive} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"recursive"}) 0 0 0
234 0 push @hdrs, "X-STF-Recursive-Delete" => "true";
235 }
236  
237 0 $url = $self->_qualify_url($url);
238  
239 0 my %furlopts = (
240 method => 'DELETE',
241 headers => \@hdrs,
242 url => $url,
243 );
244  
245 0 my @res = $self->send_request( \%furlopts, $opts );
246 0 ▶ if (! HTTP::Status::is_success( $res[1] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&HTTP::Status::is_success($res[1])) 0 0 0
247 0 $self->error( $res[2] );
248 0 return;
249 }
250  
251 0 return 1;
252 }
253  
254 1 1;
255  
256 __END__
↑ Summary Object.pm »