Storage.pm

filter: Statementstmt 29.1% 37 / 127 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 52.6% 10 / 19 Podpod 100% 9 / 9 totaltotal 27.5% 56 / 203 CC 22 SCAR 37.1
CC
22
Cov
22%
insert_object
132.0
get_bucket
12.0
get_object
12.0
CRAP 40.8
↑ Summary Storage/Agent.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 package Net::Google::Storage;
5 2 $Net::Google::Storage::VERSION = '0.2.0';
6 # ABSTRACT: Access the Google Storage JSON API (currently experimental).
7 # https://developers.google.com/storage/docs/json_api/
8  
9 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Net::Google::Storage::Bucket;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Net::Google::Storage::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 with 'Net::Google::Storage::Agent';
18  
19  
20 2 has projectId => (
21 is => 'rw',
22 isa => 'Int',
23 );
24  
25 2 my $api_base = 'https://www.googleapis.com/storage/v1/b';
26 2 my $upload_api_base = 'https://www.googleapis.com/upload/storage/v1/b';
27  
28  
29 sub list_buckets
30 0 ▶{
Subroutine list_buckets: not called CC=2 SCAR=17.9
Pod documented
31 0 my $self = shift;
32
33 0 my $projectId = $self->projectId;
34
35 0 my $res = $self->_get("$api_base?project=$projectId");
36
37 0 ▶ die 'Failed to list buckets' . $res->decoded_content unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
38
39 0 my $response = decode_json($res->decoded_content);
40
41 0 my @buckets = map {Net::Google::Storage::Bucket->new($_)} @{$response->{items}};
42 0 return \@buckets;
43 }
44  
45  
46 sub get_bucket
47 0 ▶{
Subroutine get_bucket: not called CC=3 SCAR=24.8
Pod documented
48 0 my $self = shift;
49
50 0 my $bucket_name = shift;
51
52 0 my $res = $self->_get($self->_form_url("$api_base/%s", $bucket_name));
53 0 ▶ return undef if $res->code == HTTP_NOT_FOUND;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 404 0 0 0
54 0 ▶ die "Failed to get bucket: $bucket_name" unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
55
56 0 my $response = decode_json($res->decoded_content);
57
58 0 return Net::Google::Storage::Bucket->new($response);
59 }
60  
61  
62 sub insert_bucket
63 0 ▶{
Subroutine insert_bucket: not called CC=2 SCAR=17.9
Pod documented
64 0 my $self = shift;
65
66 0 my $bucket_args = shift;
67 0 my $res = $self->_json_post($api_base . '?project=' . $self->projectId, $bucket_args);
68 0 ▶ die "Failed to create bucket: $bucket_args->{id}" . $res->content unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
69
70 0 my $response = decode_json($res->decoded_content);
71
72 0 return Net::Google::Storage::Bucket->new($response);
73 }
74  
75  
76 sub delete_bucket
77 0 ▶{
Subroutine delete_bucket: not called CC=2 SCAR=17.9
Pod documented
78 0 my $self = shift;
79
80 0 my $bucket_name = shift;
81
82 0 my $res = $self->_delete($self->_form_url("$api_base/%s", $bucket_name));
83 0 ▶ die "Failed to delete bucket: $bucket_name" unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
84
85 0 return;
86 }
87  
88  
89 sub list_objects
90 0 ▶{
Subroutine list_objects: not called CC=2 SCAR=17.9
Pod documented
91 0 my $self = shift;
92
93 0 my $bucket = shift;
94
95 0 my $res = $self->_get($self->_form_url("$api_base/%s/o", $bucket));
96
97 0 ▶ die 'Failed to list objects' unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
98
99 0 my $response = decode_json($res->decoded_content);
100
101 0 my @objects = map {Net::Google::Storage::Object->new($_)} @{$response->{items}};
102 0 return \@objects;
103 }
104  
105  
106 sub get_object
107 0 ▶{
Subroutine get_object: not called CC=3 SCAR=24.8
Pod documented
108 0 my $self = shift;
109
110 0 my %args = @_;
111
112 0 my $res = $self->_get($self->_form_url("$api_base/%s/o/%s?alt=json", $args{bucket}, $args{object}));
113 0 ▶ return undef if $res->code == HTTP_NOT_FOUND;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 404 0 0 0
114 0 ▶ die "Failed to get object: $args{object} in bucket: $args{bucket}" unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
115
116 0 my $response = decode_json($res->decoded_content);
117
118 0 return Net::Google::Storage::Object->new($response);
119 }
120  
121  
122 sub download_object
123 0 ▶{
Subroutine download_object: not called CC=3 SCAR=24.8
Pod documented
124 0 my $self = shift;
125
126 0 my %args = @_;
127
128 0 my $res = $self->_get($self->_form_url("$api_base/%s/o/%s?alt=media", $args{bucket}, $args{object}), ':content_file' => $args{filename});
129 0 ▶ return undef if $res->code == HTTP_NOT_FOUND;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 404 0 0 0
130 0 ▶ die "Failed to get object: $args{object} in bucket: $args{bucket}" unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
131 }
132  
133  
134 sub insert_object
135 0 ▶{
Subroutine insert_object: not called CC=11 SCAR=48.8
Pod documented
136 0 my $self = shift;
137
138 0 my %args = @_;
139
140 0 my $url = $self->_form_url("$upload_api_base/%s/o?uploadType=resumable", $args{bucket});
141 0 ▶ my $filename = $args{filename} || die 'A filename is required';
Condition0%0/2
$args{'filename'} || die('A filename is required')
l!l
00
MC/DC0%0/1
$args{'filename'} || die('A filename is required')
$args{'filename'}
Truth table0%0/2
A
A= $args{'filename'}
Aresult
11
00
142
143 0 ▶ die "Unable to find $filename" unless -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename 0 0 0
144 0 my $filesize = -s _;
145
146 0 my $object_hash = $args{object};
147 0 ▶ unless($object_hash->{media}->{contentType})
Branch: 1 branch
BranchTrueFalseTotal
unless ($object_hash->{"media"}{"contentType"}) 0 0 0
148 {
149 0 require LWP::MediaTypes;
150 0 $object_hash->{media}->{contentType} = LWP::MediaTypes::guess_media_type($filename);
151 }
152
153 0 my $content_type = $object_hash->{media}->{contentType};
154 0 my $res = $self->_json_post($url, 'X-Upload-Content-Type' => $content_type, 'X-Upload-Content-Length' => $filesize, $object_hash);
155 0 my $resumable_url = $res->header('Location');
156
157 0 my %headers = (
158 'Content-Length' => $filesize,
159 'Content-Type' => $content_type,
160 );
161
162 0 local $/;
163 0 open(my $fh, '<', $filename);
164 0 my $file_contents = <$fh>;
165
166 0 $res = $self->_put($resumable_url, %headers, Content => $file_contents);
167
168 #resuming code
169 0 my $retry_count = 0;
170 0 my $code = $res->code;
171 0 ▶ while($code >=500 && $code <600 && $retry_count++ < 8)
Branch: 1 branch
BranchTrueFalseTotal
if ($code >= 500 and $code < 600 and $retry_count++ < 8) 0 0 0
Condition0%0/6
$code >= 500 and $code < 600 and $retry_count++ < 8
!ll&&!rl&&r
000
$code >= 500 and $code < 600
!ll&&!rl&&r
000
MC/DC0%0/3
$code >= 500 and $code < 600 and $retry_count++ < 8
$code >= 500 $code < 600 $retry_count++ < 8
Truth table0%0/4
A and B and C
A= $code >= 500B= $code < 600C= $retry_count++ < 8
ABCresult
0XX0
10X0
1100
1111
172 {
173 0 sleep 2**$retry_count;
174 0 $res = $self->_put($resumable_url, 'Content-Length' => 0, 'Content-Range' => "bytes */$filesize");
175 0 ▶ last if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
176 0 ▶ next unless $res->code == 308;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 308 0 0 0
177
178 0 my $range = $res->header('Range');
179 0 ▶ next unless $range;
Branch: 1 branch
BranchTrueFalseTotal
unless $range 0 0 0
180
181 0 ▶ if($range =~ /bytes=0-(\d+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($range =~ /bytes=0-(\d+)/) { } 0 0 0
182 {
183 0 my $offset = $1+1;
184 0 seek($fh, $offset, 0);
185 0 $file_contents = <$fh>;
186
187 0 %headers = (
188 'Content-Length' => $filesize - $offset,
189 'Content-Range' => sprintf('bytes %d-%d/%d', $offset, $filesize-1, $filesize),
190 );
191 0 $res = $self->_put($resumable_url, %headers, Content => $file_contents);
192 0 $code = $res->code;
193 }
194 else
195 {
196 0 next;
197 }
198 }
199
200 0 my $response = decode_json($res->decoded_content);
201
202 0 return Net::Google::Storage::Object->new($response);
203 }
204  
205  
206 sub delete_object
207 0 ▶{
Subroutine delete_object: not called CC=2 SCAR=17.9
Pod documented
208 0 my $self = shift;
209
210 0 my %args = @_;
211
212 0 my $res = $self->_delete($self->_form_url("$api_base/%s/o/%s", $args{bucket}, $args{object}));
213 0 ▶ die "Failed to delete object: $args{object} in bucket: $args{bucket}" unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
214
215 0 return;
216 }
217  
218 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
219 2 __PACKAGE__->meta->make_immutable;
220  
221 2 1;
222  
223 __END__
↑ Summary Storage/Agent.pm »