Storage/Agent.pm

filter: Statementstmt 33.7% 29 / 86 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 37.5% 6 / 16 Podpod 100% 1 / 1 totaltotal 27.9% 36 / 129 CC 10 SCAR 25.8
CC
10
Cov
27%
__ANON__
42.0
_set_auth_header
12.0
_build_ua
6.0
CRAP 13.2
« Storage.pm ↑ Summary Storage/Bucket.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 package Net::Google::Storage::Agent;
4 {
5 2 $Net::Google::Storage::Agent::VERSION = '0.1.2';
6 }
7  
8 # ABSTRACT: Access the Google Storage JSON API (currently experimental).
9 # https://developers.google.com/storage/docs/json_api/
10  
11 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use LWP::UserAgent 6.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use URI::Escape 3.29;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 2 has access_token => (
18 is => 'rw',
19 isa => 'Str',
20 );
21  
22  
23 2 has refresh_token => (
24 is => 'ro',
25 isa => 'Str',
26 );
27  
28  
29 2 has client_id => (
30 is => 'ro',
31 isa => 'Str',
32 );
33  
34  
35 2 has client_secret => (
36 is => 'ro',
37 isa => 'Str',
38 );
39  
40  
41 2 has has_refreshed_access_token => (
42 is => 'rw',
43 isa => 'Bool',
44 default => 0,
45 );
46  
47  
48 2 has access_token_expiry => (
49 is => 'rw',
50 isa => 'Int',
51 );
52  
53 2 has _ua => (
54 is => 'ro',
55 isa => 'LWP::UserAgent',
56 lazy => 1,
57 builder => '_build_ua',
58 );
59  
60 sub _build_ua
61 {
62 0 ▶ my $self = shift;
Subroutine _build_ua: not called CC=2 SCAR=17.9
63 0 my $ua = LWP::UserAgent->new(agent => 'Net::Google::Storage ');
64
65 0 my @encodings = HTTP::Message::decodable;
66 0 ▶ if(grep {$_ eq 'gzip'} @encodings)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "gzip";} @encodings) 0 0 0
67 {
68 0 $ua->agent($ua->agent . ' (gzip)');
69 0 $ua->default_header('Accept-Encoding' => join ', ', @encodings);
70 }
71
72 0 return $ua;
73 }
74  
75 sub _set_auth_header
76 {
77 0 ▶ my $self = shift;
Subroutine _set_auth_header: not called CC=3 SCAR=24.8
78 0 my $ua = $self->_ua;
79
80 0 ▶ if($self->access_token)
Branch: 2 branches
BranchTrueFalseTotal
if ($self->access_token) { } 0 0 0
elsif ($self->refresh_token) { } 0 0 0
81 {
82 0 $ua->default_header(Authorization => "OAuth " . $self->access_token);
83 }
84 elsif($self->refresh_token)
85 {
86 0 $self->refresh_access_token
87 }
88 }
89  
90  
91 sub refresh_access_token
92 {
93 0 ▶ my $self = shift;
Subroutine refresh_access_token: not called CC=2 SCAR=17.9
Pod documented
94
95 0 my $ua = $self->_ua;
96 0 my $res = $ua->post('https://accounts.google.com/o/oauth2/token', {
97 client_id => $self->client_id,
98 client_secret => $self->client_secret,
99 refresh_token => $self->refresh_token,
100 grant_type => 'refresh_token',
101 });
102
103 0 ▶ die 'Failed to refresh the access token' unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
104
105 0 my $response = decode_json($res->decoded_content);
106 0 $self->access_token($response->{access_token});
107 0 $self->access_token_expiry(time + $response->{expires_in});
108 0 $self->has_refreshed_access_token(1);
109 0 $self->_set_auth_header;
110 }
111  
112 sub _get
113 {
114 0 ▶ my $self = shift;
Subroutine _get: not called CC=1 SCAR=6.9
115 0 my $ua = $self->_ua;
116
117 0 my $res = $ua->get(@_);
118
119 0 return $res;
120 }
121  
122 sub _post
123 {
124 0 ▶ my $self = shift;
Subroutine _post: not called CC=1 SCAR=6.9
125 0 my $ua = $self->_ua;
126
127 0 my $res = $ua->post(@_);
128 0 return $res;
129 }
130  
131 sub _json_post
132 {
133 0 ▶ my $self = shift;
Subroutine _json_post: not called CC=1 SCAR=6.9
134
135 0 my $args = pop;
136 0 return $self->_post(@_, 'Content-Type' => 'application/json', Content => encode_json($args));
137 }
138  
139 sub _delete
140 {
141 0 ▶ my $self = shift;
Subroutine _delete: not called CC=1 SCAR=6.9
142 0 my $ua = $self->_ua;
143
144 0 my $res = $ua->delete(@_);
145 0 return $res;
146 }
147  
148 sub _put
149 {
150 0 ▶ my $self = shift;
Subroutine _put: not called CC=1 SCAR=6.9
151 0 my $ua = $self->_ua;
152
153 0 my $res = $ua->put(@_);
154 0 return $res;
155 }
156  
157 around [qw(_get _post _delete _put)] => sub {
158 0 ▶ my $orig = shift;
Subroutine __ANON__: not called CC=6 SCAR=37.4
159 0 my $self = shift;
160
161 0 my $ua = $self->_ua;
162 0 my $expiry = $self->access_token_expiry;
163
164 0 ▶ if((!$ua->default_header('Authorization')) || ($expiry && $expiry < time))
Branch: 1 branch
BranchTrueFalseTotal
if (not $ua->default_header("Authorization") or $expiry and $expiry < time) 0 0 0
Condition0%0/6
not $ua->default_header("Authorization") or $expiry and $expiry < time
l!l&&r!l&&!r
000
$expiry and $expiry < time
!ll&&!rl&&r
000
MC/DC0%0/3
not $ua->default_header("Authorization") or $expiry and $expiry < time
not $ua->default_header("Authorization") $expiry $expiry < time
Truth table0%0/4
not(A) or B and C
A= not $ua->default_header("Authorization")B= $expiryC= $expiry < time
ABCresult
1XX1
0111
00X0
0100
165 {
166 0 $self->_set_auth_header;
167 }
168
169 0 my $res = $self->$orig(@_);
170 0 ▶ if($res->code == 401 && $self->refresh_token)
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 401 and $self->refresh_token) 0 0 0
Condition0%0/3
$res->code == 401 and $self->refresh_token
!ll&&!rl&&r
000
MC/DC0%0/2
$res->code == 401 and $self->refresh_token
$res->code == 401 $self->refresh_token
Truth table0%0/3
A and B
A= $res->code == 401B= $self->refresh_token
ABresult
0X0
100
111
171 {
172 0 $self->refresh_access_token;
173 0 $res = $self->$orig(@_);
174 }
175
176 0 return $res;
177 2 };
178  
179 sub _form_url
180 {
181 0 ▶ my $self = shift;
Subroutine _form_url: not called CC=1 SCAR=6.9
182
183 0 my $format = shift;
184 0 my @args = map {uri_escape_utf8($_)} @_;
185
186 0 return sprintf $format, @args;
187 }
188  
189 2 1;
190  
191 __END__
192 =pod
193  
194 =head1 NAME
195  
196 Net::Google::Storage::Agent - Access the Google Storage JSON API (currently experimental).
197  
198 =head1 VERSION
199  
200 version 0.1.2
201  
202 =head1 DESCRIPTION
203  
204 Role-module for L<Net::Google::Storage>, handles the http communication side
205 of things.
206  
207 Some or all of the following attributes should be passed in as an argument to
208 L<Net::Google::Storage/new>
209  
210 =head1 ATTRIBUTES
211  
212 =head2 access_token
213  
214 An OAuth2 access token used to actually access the resources.
215  
216 =head2 refresh_token
217  
218 An OAuth2 refresh token used for acquiring a new L</access_tokens> - you
219 don't need both a refresh_token and an access_token, but you'll need at least
220 one of them.
221  
222 =head2 client_id
223  
224 The client ID for the user being authenticated - retrieved from Google's
225 L<API Console|https://code.google.com/apis/console/#access>.
226  
227 Required for refreshing access tokens (ie provide if you are also providing
228 the L</refresh_token>).
229  
230 =head2 client_secret
231  
232 Counterpart to the client ID, also retrieved from the API Console.
233  
234 Again, only required for refreshing access tokens.
235  
236 =head2 access_token_expiry
237  
238 The time (in seconds since the epoch) at which the access_token will be
239 invalidated. Not required, but if supplied with the L</access_token> it
240 B<will> be trusted, and token refresh will be attempted after this time
241 without attempting communication.
242  
243 =head1 METHODS
244  
245 =head2 has_refreshed_access_token
246  
247 Call without parameters to find whether the L</access_token> has been
248 refreshed.
249  
250 Call with a false value to indicate you know about that refresh, so future
251 calls without any parameters will still be useful.
252  
253 =head2 refresh_access_token
254  
255 Call (on the L<Net::Google::Storage> object) to refresh the access token.
256 Requires the C<client_id>, the C<client_secret> and the C<refresh_token> to
257 all be set. Updates the C<access_token>, the C<access_token_expiry> and
258 C<has_refreshed_access_token> will start returning true.
259  
260 =head1 AUTHOR
261  
262 Glenn Fowler <cebjyre@cpan.org>
263  
264 =head1 COPYRIGHT AND LICENSE
265  
266 This software is copyright (c) 2012 by Glenn Fowler.
267  
268 This is free software; you can redistribute it and/or modify it under
269 the same terms as the Perl 5 programming language system itself.
270  
271 =cut
272  
« Storage.pm ↑ Summary Storage/Bucket.pm »