Storage/Agent.pm

filter: Statementstmt 32.9% 28 / 85 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.3% 35 / 128 CC 10 SCAR 25.8
CC
10
Cov
26%
__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 2 $Net::Google::Storage::Agent::VERSION = '0.2.0';
5 # ABSTRACT: Access the Google Storage JSON API (currently experimental).
6 # https://developers.google.com/storage/docs/json_api/
7  
8 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use LWP::UserAgent 6.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use URI::Escape 3.29;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 2 has access_token => (
15 is => 'rw',
16 isa => 'Str',
17 );
18  
19  
20 2 has refresh_token => (
21 is => 'ro',
22 isa => 'Str',
23 );
24  
25  
26 2 has client_id => (
27 is => 'ro',
28 isa => 'Str',
29 );
30  
31  
32 2 has client_secret => (
33 is => 'ro',
34 isa => 'Str',
35 );
36  
37  
38 2 has has_refreshed_access_token => (
39 is => 'rw',
40 isa => 'Bool',
41 default => 0,
42 );
43  
44  
45 2 has access_token_expiry => (
46 is => 'rw',
47 isa => 'Int',
48 );
49  
50 2 has _ua => (
51 is => 'ro',
52 isa => 'LWP::UserAgent',
53 lazy => 1,
54 builder => '_build_ua',
55 );
56  
57 sub _build_ua
58 0 ▶{
Subroutine _build_ua: not called CC=2 SCAR=17.9
59 0 my $self = shift;
60 0 my $ua = LWP::UserAgent->new(agent => 'Net::Google::Storage ');
61
62 0 my @encodings = HTTP::Message::decodable;
63 0 ▶ if(grep {$_ eq 'gzip'} @encodings)
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "gzip";} @encodings) 0 0 0
64 {
65 0 $ua->agent($ua->agent . ' (gzip)');
66 0 $ua->default_header('Accept-Encoding' => join ', ', @encodings);
67 }
68
69 0 return $ua;
70 }
71  
72 sub _set_auth_header
73 0 ▶{
Subroutine _set_auth_header: not called CC=3 SCAR=24.8
74 0 my $self = shift;
75 0 my $ua = $self->_ua;
76
77 0 ▶ if($self->access_token)
Branch: 2 branches
BranchTrueFalseTotal
if ($self->access_token) { } 0 0 0
elsif ($self->refresh_token) { } 0 0 0
78 {
79 0 $ua->default_header(Authorization => "OAuth " . $self->access_token);
80 }
81 elsif($self->refresh_token)
82 {
83 0 $self->refresh_access_token
84 }
85 }
86  
87  
88 sub refresh_access_token
89 0 ▶{
Subroutine refresh_access_token: not called CC=2 SCAR=17.9
Pod documented
90 0 my $self = shift;
91
92 0 my $ua = $self->_ua;
93 0 my $res = $ua->post('https://accounts.google.com/o/oauth2/token', {
94 client_id => $self->client_id,
95 client_secret => $self->client_secret,
96 refresh_token => $self->refresh_token,
97 grant_type => 'refresh_token',
98 });
99
100 0 ▶ die 'Failed to refresh the access token' . $res->content unless $res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $res->is_success 0 0 0
101
102 0 my $response = decode_json($res->decoded_content);
103 0 $self->access_token($response->{access_token});
104 0 $self->access_token_expiry(time + $response->{expires_in});
105 0 $self->has_refreshed_access_token(1);
106 0 $self->_set_auth_header;
107 }
108  
109 sub _get
110 0 ▶{
Subroutine _get: not called CC=1 SCAR=6.9
111 0 my $self = shift;
112 0 my $ua = $self->_ua;
113
114 0 my $res = $ua->get(@_);
115
116 0 return $res;
117 }
118  
119 sub _post
120 0 ▶{
Subroutine _post: not called CC=1 SCAR=6.9
121 0 my $self = shift;
122 0 my $ua = $self->_ua;
123
124 0 my $res = $ua->post(@_);
125 0 return $res;
126 }
127  
128 sub _json_post
129 0 ▶{
Subroutine _json_post: not called CC=1 SCAR=6.9
130 0 my $self = shift;
131
132 0 my $args = pop;
133 0 return $self->_post(@_, 'Content-Type' => 'application/json', Content => encode_json($args));
134 }
135  
136 sub _delete
137 0 ▶{
Subroutine _delete: not called CC=1 SCAR=6.9
138 0 my $self = shift;
139 0 my $ua = $self->_ua;
140
141 0 my $res = $ua->delete(@_);
142 0 return $res;
143 }
144  
145 sub _put
146 0 ▶{
Subroutine _put: not called CC=1 SCAR=6.9
147 0 my $self = shift;
148 0 my $ua = $self->_ua;
149
150 0 my $res = $ua->put(@_);
151 0 return $res;
152 }
153  
154 0 ▶around [qw(_get _post _delete _put)] => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
155 0 my $orig = shift;
156 0 my $self = shift;
157
158 0 my $ua = $self->_ua;
159 0 my $expiry = $self->access_token_expiry;
160
161 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
162 {
163 0 $self->_set_auth_header;
164 }
165
166 0 my $res = $self->$orig(@_);
167 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
168 {
169 0 $self->refresh_access_token;
170 0 $res = $self->$orig(@_);
171 }
172
173 0 return $res;
174 2 };
175  
176 sub _form_url
177 0 ▶{
Subroutine _form_url: not called CC=1 SCAR=6.9
178 0 my $self = shift;
179
180 0 my $format = shift;
181 0 my @args = map {uri_escape_utf8($_)} @_;
182
183 0 return sprintf $format, @args;
184 }
185  
186 2 1;
187  
188 __END__
« Storage.pm ↑ Summary Storage/Bucket.pm »