Storage.pm

filter: Statementstmt 29.4% 38 / 129 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 52.6% 10 / 19 Podpod 100% 9 / 9 totaltotal 27.1% 57 / 210 CC 23 SCAR 37.0
CC
23
Cov
22%
insert_object
132.0
get_bucket
12.0
insert_bucket
12.0
CRAP 40.3
↑ 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 {
6 2 $Net::Google::Storage::VERSION = '0.1.2';
7 }
8  
9 # ABSTRACT: Access the Google Storage JSON API (currently experimental).
10 # https://developers.google.com/storage/docs/json_api/
11  
12 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use HTTP::Status qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 ▶use Net::Google::Storage::Bucket;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Net::Google::Storage::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 with 'Net::Google::Storage::Agent';
21  
22  
23 2 has projectId => (
24 is => 'rw',
25 isa => 'Int',
26 );
27  
28 2 my $api_base = 'https://www.googleapis.com/storage/v1beta1/b';
29 2 my $upload_api_base = 'https://www.googleapis.com/upload/storage/v1beta1/b';
30  
31  
32 sub list_buckets
33 {
34 0 ▶ my $self = shift;
Subroutine list_buckets: not called CC=2 SCAR=17.9
Pod documented
35
36 0 my $projectId = $self->projectId;
37
38 0 my $res = $self->_get("$api_base?projectId=$projectId");
39
40 0 ▶ die 'Failed to list buckets' unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
41
42 0 my $response = decode_json($res->decoded_content);
43
44 0 my @buckets = map {Net::Google::Storage::Bucket->new($_)} @{$response->{items}};
45 0 return \@buckets;
46 }
47  
48  
49 sub get_bucket
50 {
51 0 ▶ my $self = shift;
Subroutine get_bucket: not called CC=3 SCAR=24.8
Pod documented
52
53 0 my $bucket_name = shift;
54
55 0 my $res = $self->_get($self->_form_url("$api_base/%s", $bucket_name));
56 0 ▶ return undef if $res->code == HTTP_NOT_FOUND;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 404 0 0 0
57 0 ▶ die "Failed to get bucket: $bucket_name" unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
58
59 0 my $response = decode_json($res->decoded_content);
60
61 0 return Net::Google::Storage::Bucket->new($response);
62 }
63  
64  
65 sub insert_bucket
66 {
67 0 ▶ my $self = shift;
Subroutine insert_bucket: not called CC=3 SCAR=24.8
Pod documented
68
69 0 my $bucket_args = shift;
70 0 ▶ $bucket_args->{projectId} ||= $self->projectId;
Condition0%0/3
$bucket_args->{"projectId"} ||= $self->projectId
l!l&&r!l&&!r
000
MC/DC0%0/2
$bucket_args->{"projectId"} ||= $self->projectId
$bucket_args->{"projectId"} $self->projectId
Truth table0%0/3
A ||= B
A= $bucket_args->{"projectId"}B= $self->projectId
ABresult
1X1
011
000
71 0 my $res = $self->_json_post($api_base, $bucket_args);
72 0 ▶ die "Failed to create bucket: $bucket_args->{id}" unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
73
74 0 my $response = decode_json($res->decoded_content);
75
76 0 return Net::Google::Storage::Bucket->new($response);
77 }
78  
79  
80 sub delete_bucket
81 {
82 0 ▶ my $self = shift;
Subroutine delete_bucket: not called CC=2 SCAR=17.9
Pod documented
83
84 0 my $bucket_name = shift;
85
86 0 my $res = $self->_delete($self->_form_url("$api_base/%s", $bucket_name));
87 0 ▶ die "Failed to delete bucket: $bucket_name" unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
88
89 0 return;
90 }
91  
92  
93 sub list_objects
94 {
95 0 ▶ my $self = shift;
Subroutine list_objects: not called CC=2 SCAR=17.9
Pod documented
96
97 0 my $bucket = shift;
98
99 0 my $res = $self->_get($self->_form_url("$api_base/%s/o", $bucket));
100
101 0 ▶ die 'Failed to list objects' unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
102
103 0 my $response = decode_json($res->decoded_content);
104
105 0 my @objects = map {Net::Google::Storage::Object->new($_)} @{$response->{items}};
106 0 return \@objects;
107 }
108  
109  
110 sub get_object
111 {
112 0 ▶ my $self = shift;
Subroutine get_object: not called CC=3 SCAR=24.8
Pod documented
113
114 0 my %args = @_;
115
116 0 my $res = $self->_get($self->_form_url("$api_base/%s/o/%s?alt=json", $args{bucket}, $args{object}));
117 0 ▶ return undef if $res->code == HTTP_NOT_FOUND;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 404 0 0 0
118 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
119
120 0 my $response = decode_json($res->decoded_content);
121
122 0 return Net::Google::Storage::Object->new($response);
123 }
124  
125  
126 sub download_object
127 {
128 0 ▶ my $self = shift;
Subroutine download_object: not called CC=3 SCAR=24.8
Pod documented
129
130 0 my %args = @_;
131
132 0 my $res = $self->_get($self->_form_url("$api_base/%s/o/%s", $args{bucket}, $args{object}), ':content_file' => $args{filename});
133 0 ▶ return undef if $res->code == HTTP_NOT_FOUND;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 404 0 0 0
134 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
135 }
136  
137  
138 sub insert_object
139 {
140 0 ▶ my $self = shift;
Subroutine insert_object: not called CC=11 SCAR=48.8
Pod documented
141
142 0 my %args = @_;
143
144 0 my $url = $self->_form_url("$upload_api_base/%s/o?uploadType=resumable", $args{bucket});
145 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
146
147 0 ▶ die "Unable to find $filename" unless -e $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename 0 0 0
148 0 my $filesize = -s _;
149
150 0 my $object_hash = $args{object};
151 0 ▶ unless($object_hash->{media}->{contentType})
Branch: 1 branch
BranchTrueFalseTotal
unless ($object_hash->{"media"}{"contentType"}) 0 0 0
152 {
153 0 require LWP::MediaTypes;
154 0 $object_hash->{media}->{contentType} = LWP::MediaTypes::guess_media_type($filename);
155 }
156
157 0 my $content_type = $object_hash->{media}->{contentType};
158 0 my $res = $self->_json_post($url, 'X-Upload-Content-Type' => $content_type, 'X-Upload-Content-Length' => $filesize, $object_hash);
159 0 my $resumable_url = $res->header('Location');
160
161 0 my %headers = (
162 'Content-Length' => $filesize,
163 'Content-Type' => $content_type,
164 );
165
166 0 local $/;
167 0 open(my $fh, '<', $filename);
168 0 my $file_contents = <$fh>;
169
170 0 $res = $self->_put($resumable_url, %headers, Content => $file_contents);
171
172 #resuming code
173 0 my $retry_count = 0;
174 0 my $code = $res->code;
175 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
176 {
177 0 sleep 2**$retry_count;
178 0 $res = $self->_put($resumable_url, 'Content-Length' => 0, 'Content-Range' => "bytes */$filesize");
179 0 ▶ last if $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
if $res->is_success 0 0 0
180 0 ▶ next unless $res->code == 308;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->code == 308 0 0 0
181
182 0 my $range = $res->header('Range');
183 0 ▶ next unless $range;
Branch: 1 branch
BranchTrueFalseTotal
unless $range 0 0 0
184
185 0 ▶ if($range =~ /bytes=0-(\d+)/)
Branch: 1 branch
BranchTrueFalseTotal
if ($range =~ /bytes=0-(\d+)/) { } 0 0 0
186 {
187 0 my $offset = $1+1;
188 0 seek($fh, $offset, 0);
189 0 $file_contents = <$fh>;
190
191 0 %headers = (
192 'Content-Length' => $filesize - $offset,
193 'Content-Range' => sprintf('bytes %d-%d/%d', $offset, $filesize-1, $filesize),
194 );
195 0 $res = $self->_put($resumable_url, %headers, Content => $file_contents);
196 0 $code = $res->code;
197 }
198 else
199 {
200 0 next;
201 }
202 }
203
204 0 my $response = decode_json($res->decoded_content);
205
206 0 return Net::Google::Storage::Object->new($response);
207 }
208  
209  
210 sub delete_object
211 {
212 0 ▶ my $self = shift;
Subroutine delete_object: not called CC=2 SCAR=17.9
Pod documented
213
214 0 my %args = @_;
215
216 0 my $res = $self->_delete($self->_form_url("$api_base/%s/o/%s", $args{bucket}, $args{object}));
217 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
218
219 0 return;
220 }
221  
222 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 2 __PACKAGE__->meta->make_immutable;
224  
225 2 1;
226  
227 __END__
228 =pod
229  
230 =head1 NAME
231  
232 Net::Google::Storage - Access the Google Storage JSON API (currently experimental).
233  
234 =head1 VERSION
235  
236 version 0.1.2
237  
238 =head1 SYNOPSIS
239  
240 my $gs = Net::Google::Storage->new(
241 projectId => $projectId,
242 %agent_args
243 );
244  
245 See L<Net::Google::Storage::Agent> for a decription of C<%agent_args>.
246  
247 my $buckets = $gs->list_buckets();
248  
249 my $bucket = $gs->get_bucket($bucket_name);
250  
251 my $new_bucket = $gs->insert_bucket({id => $new_bucket_name});
252  
253 $gs->delete_bucket($bucket_name);
254  
255 my $objects = $gs->list_objects($bucket_name);
256  
257 my $object = $gs->get_object(bucket => $bucket_name, object => $object_name);
258  
259 $gs->download_object(bucket => $bucket_name, object => $object_name, filename => $filename);
260  
261 my $object = $gs->insert_object(bucket => $bucket_name, object => {name => $object_name}, filename => $filename);
262  
263 $gs->delete_object(bucket => $bucket_name, object => $object_name);
264  
265 =head1 DESCRIPTION
266  
267 Net::Google::Storage is a library for interacting with the JSON version of
268 the Google Storage API, which is currently (as at 2012-09-17) marked as
269 experimental.
270  
271 This module does not (yet) cover the entire surface of the API, but it is a
272 decent attempt at providing the most important functionality.
273  
274 See L<https://developers.google.com/storage/docs/json_api/> for documentation
275 of the API itself.
276  
277 =head1 ATTRIBUTES
278  
279 =head2 projectId
280  
281 Google's identifier of the project you are accessing. Available from the
282 L<API Console|https://code.google.com/apis/console/#:storage>.
283  
284 =head1 METHODS
285  
286 =head2 new
287  
288 Constructs a shiny new B<Net::Google::Storage> object. Arguments include
289 C<projectId> and the attributes of L<Net::Google::Storage::Agent>
290  
291 =head2 list_buckets
292  
293 Returns an arrayref of L<Net::Google::Storage::Bucket> objects for the
294 current projectId.
295  
296 =head2 get_bucket
297  
298 Takes a bucket name as the only argument, returns the matching
299 L<Net::Google::Storage::Bucket> object or undef if nothing matches.
300  
301 =head2 insert_bucket
302  
303 Takes some bucket metadata as the only argument (could be as simple as
304 C<< {id => $bucket_name} >>), creates a new bucket and returns the matching
305 L<Net::Google::Storage::Bucket> object.
306  
307 =head2 delete_bucket
308  
309 Takes a bucket name as the only argument, deletes the bucket.
310  
311 =head2 list_objects
312  
313 Takes a bucket name as the only argument, returns an arrayref of
314 L<Net::Google::Storage::Object> objects.
315  
316 =head2 get_object
317  
318 Takes a hash (not hashref) of arguments with keys: I<bucket> and I<object>
319 and returns the matching L<Net::Google::Storage::Object> object (or undef if
320 no match was found).
321  
322 =head2 download_object
323  
324 Takes a hash (not hashref) of arguments with keys: I<bucket>, I<object>, and
325 I<filename> and downloads the matching object as the desired filename.
326  
327 Returns undef if the object doesn't exist, true for success.
328  
329 =head2 insert_object
330  
331 Takes a hash of arguments with keys: I<bucket>, I<filename> and I<object>
332 where I<object> contains the necessary metadata to upload the file, which is,
333 at minimum, the I<name> field.
334  
335 =head2 delete_object
336  
337 Takes a hash of arguments with keys: I<bucket> and I<object> and deletes the
338 matching object.
339  
340 =head1 AUTHOR
341  
342 Glenn Fowler <cebjyre@cpan.org>
343  
344 =head1 COPYRIGHT AND LICENSE
345  
346 This software is copyright (c) 2012 by Glenn Fowler.
347  
348 This is free software; you can redistribute it and/or modify it under
349 the same terms as the Perl 5 programming language system itself.
350  
351 =cut
352  
↑ Summary Storage/Agent.pm »