Google/Client.pm

filter: Statementstmt 33.3% 25 / 75 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 50.0% 6 / 12 Podpod 75.0% 3 / 4 totaltotal 20.2% 34 / 168 CC 27 SCAR 48.6
CC
27
Cov
18%
build_http_transaction
272.0
api_query
56.0
refresh_access_token
30.0
CRAP 129.3
↑ Summary Google/Services.pm »
1 package Moo::Google::Client;
2 1 $Moo::Google::Client::VERSION = '0.03';
3  
4 # ABSTRACT: User Agent wrapper for working with Google APIs
5  
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moo::Google::Credentials;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moo::Google::AuthStorage;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Data::Dumper; # for debug
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Data::Printer; # for debug
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶has 'ua' => ( is => 'ro', default => sub { Mojo::UserAgent->new } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
14 1 has 'do_autorefresh' => ( is => 'rw', default => 1 )
15 ; # if 1 storage must be configured
16 1 has 'auto_update_tokens_in_storage' => ( is => 'rw', default => 1 );
17 1 has 'debug' => ( is => 'rw', default => 0 );
18 has 'credentials' => (
19 is => 'rw',
20 0 ▶ default => sub { Moo::Google::Credentials->instance },
Subroutine __ANON__: not called CC=1 SCAR=6.9
21 1 handles => [qw(access_token user auth_storage)],
22 lazy => 1
23 );
24  
25 # Keep access_token in headers always actual
26  
27 0 ▶sub build_headers {
Subroutine build_headers: not called CC=2 SCAR=17.9
Pod undocumented
28  
29 # warn "".(caller(0))[3]."() : ".Dumper \@_;
30 0 my $self = shift;
31  
32 # p $self;
33 0 my $headers = {};
34  
35 # warn "build_headers: ".$self->access_token;
36 0 ▶ if ( $self->access_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->access_token) { } 0 0 0
37 0 $headers->{'Authorization'} = 'Bearer ' . $self->access_token;
38 0 return $headers;
39 }
40 else {
41 0 die 'No access_token, cant build headers';
42 }
43  
44 }
45  
46  
47 0 ▶sub build_http_transaction {
Subroutine build_http_transaction: not called CC=16 SCAR=56.1
Pod documented
48 0 my ( $self, $params ) = @_;
49  
50 # warn "".(caller(0))[3]."() : ".Dumper \@_;
51  
52 0 my $headers = $self->build_headers;
53 0 ▶ warn "build_http_transaction HEADERS: " . Dumper $headers
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
54 if ( $self->debug );
55  
56 # Hash key names
57 0 my $http_method = $params->{httpMethod}; # uppercase
58 0 my $path = $params->{path};
59 0 my $optional_data = $params->{options};
60 0 ▶ warn "build_http_transaction() Options: " . Dumper $optional_data
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
61 if ( $self->debug );
62  
63 0 my $tx;
64  
65 0 ▶ if ( !defined $http_method ) { die 'No http method specified' }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $http_method) 0 0 0
66  
67 0 ▶ if ( ( $http_method eq uc 'post' ) && !defined $optional_data ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http_method eq "POST" and not defined $optional_data) 0 0 0
Condition0%0/3
$http_method eq "POST" and not defined $optional_data
!ll&&!rl&&r
000
MC/DC0%0/2
$http_method eq "POST" and not defined $optional_data
$http_method eq "POST" not defined $optional_data
Truth table0%0/3
A and not(B)
A= $http_method eq "POST"B= not defined $optional_data
ABresult
0X0
100
111
68 0 warn 'Attention! You are using POST, but no payload specified';
69 }
70  
71 0 ▶ if ( lc $http_method eq 'get' ) {
Branch: 4 branches
BranchTrueFalseTotal
if (lc $http_method eq 'get') { } 0 0 0
elsif (lc $http_method eq 'delete') { } 0 0 0
elsif (lc $http_method eq 'post' || lc $http_method eq 'patch' || lc $http_method eq 'put' and defined $optional_data) { } 0 0 0
elsif (lc $http_method eq 'post' || lc $http_method eq 'patch' || lc $http_method eq 'put' and not defined $optional_data) { } 0 0 0
72 0 $tx = $self->ua->build_tx(
73 uc $http_method => $path => $headers => form => $optional_data );
74 }
75 elsif ( lc $http_method eq 'delete' ) {
76 0 $tx = $self->ua->build_tx( uc $http_method => $path => $headers );
77 }
78 ▶ elsif (
Condition0%0/9
lc $http_method eq "post" || lc $http_method eq "patch" || lc $http_method eq "put" and defined $optional_data
!ll&&!rl&&r
000
lc $http_method eq "post" or lc $http_method eq "patch" or lc $http_method eq "put"
l!l&&r!l&&!r
000
lc $http_method eq "post" or lc $http_method eq "patch"
l!l&&r!l&&!r
000
MC/DC0%0/4
lc $http_method eq "post" || lc $http_method eq "patch" || lc $http_method eq "put" and defined $optional_data
lc $http_method eq "post" lc $http_method eq "patch" lc $http_method eq "put" defined $optional_data
Truth table0%0/7
A or B or C and D
A= lc $http_method eq "post"B= lc $http_method eq "patch"C= lc $http_method eq "put"D= defined $optional_data
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
79 (
80 ( lc $http_method eq 'post' )
81 || ( lc $http_method eq 'patch' )
82 || ( lc $http_method eq 'put' )
83 )
84 && ( defined $optional_data )
85 )
86 {
87 0 $tx = $self->ua->build_tx(
88 uc $http_method => $path => $headers => json => $optional_data );
89 }
90 ▶ elsif (
Condition0%0/9
lc $http_method eq "post" || lc $http_method eq "patch" || lc $http_method eq "put" and not defined $optional_data
!ll&&!rl&&r
000
lc $http_method eq "post" or lc $http_method eq "patch" or lc $http_method eq "put"
l!l&&r!l&&!r
000
lc $http_method eq "post" or lc $http_method eq "patch"
l!l&&r!l&&!r
000
MC/DC0%0/4
lc $http_method eq "post" || lc $http_method eq "patch" || lc $http_method eq "put" and not defined $optional_data
lc $http_method eq "post" lc $http_method eq "patch" lc $http_method eq "put" not defined $optional_data
Truth table0%0/7
A or B or C and not(D)
A= lc $http_method eq "post"B= lc $http_method eq "patch"C= lc $http_method eq "put"D= not defined $optional_data
ABCDresult
000X0
1XX00
01X00
00100
1XX11
01X11
00111
91 (
92 ( lc $http_method eq 'post' )
93 || ( lc $http_method eq 'patch' )
94 || ( lc $http_method eq 'put' )
95 )
96 && ( !defined $optional_data )
97 )
98 {
99 0 $tx = $self->ua->build_tx( uc $http_method => $path => $headers );
100 }
101  
102 0 return $tx;
103  
104 }
105  
106  
107 0 ▶sub api_query {
Subroutine api_query: not called CC=7 SCAR=40.3
Pod documented
108 0 my ( $self, $params ) = @_;
109  
110 # warn "".(caller(0))[3]."() : ".Dumper \@_ if $self->debug;
111  
112 0 my $tx = $self->build_http_transaction($params);
113  
114 # warn Dumper $tx;
115 # warn "transaction built ok";
116  
117 0 my $res = $self->ua->start($tx)->res;
118  
119 # In case if access_token expired
120 # warn $response->message; # Unauthorized
121 # warn $response->json->{error}{message}; # Invalid Credentials
122 # warn $response->code; # 401
123 # warn $response->is_error; # 1
124  
125 # my $res = $self->ua->start($tx)->res->json; # Mojo::Message::Response
126  
127 # for future:
128 # if ( grep { $_->{message} eq 'Invalid Credentials' && $_->{reason} eq 'authError'} @{$res->{error}{errors}} ) { ... }
129  
130 # warn "First api_query() result : ".Dumper $res if $self->debug;
131 # warn "Auto refresh:".$self->do_autorefresh;
132  
133 # if ((defined $res->{error}) && ($self->autorefresh) && ($self->auth_storage->type) && ($self->auth_storage->path)) { # token expired error handling
134  
135 # https://metacpan.org/pod/Mojo::Message::Response#code
136  
137 # if ((defined $res->{error}) && ($self->do_autorefresh)) {
138  
139 0 ▶ if ( ( $res->code == 401 ) && $self->do_autorefresh ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 401 and $self->do_autorefresh) 0 0 0
Condition0%0/3
$res->code == 401 and $self->do_autorefresh
!ll&&!rl&&r
000
MC/DC0%0/2
$res->code == 401 and $self->do_autorefresh
$res->code == 401 $self->do_autorefresh
Truth table0%0/3
A and B
A= $res->code == 401B= $self->do_autorefresh
ABresult
0X0
100
111
140  
141 0 my $attempt = 1;
142  
143 #while ($res->{error}{message} eq 'Invalid Credentials') {
144 0 ▶ while ( $res->code == 401 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->code == 401) 0 0 0
145  
146 0 warn
147 "Seems like access_token was expired. Attemptimg update it automatically ...";
148  
149 # warn "Seems like access_token was expired. Attemptimg update it automatically ..." if $self->debug;
150  
151 0 ▶ if ( !$self->user ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->user) 0 0 0
152 0 die
153 "No user specified, so cant find refresh token and update access_token";
154 }
155  
156 0 my $cred =
157 $self->auth_storage->get_credentials_for_refresh( $self->user )
158 ; # get client_id, client_secret and refresh_token
159 my $new_token = $self->refresh_access_token($cred)->{access_token}
160 0 ; # here also {id_token} etc
161 0 ▶ warn "Got a new token: " . $new_token if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
162 0 $self->access_token($new_token);
163  
164 0 ▶ if ( $self->auto_update_tokens_in_storage ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_update_tokens_in_storage) 0 0 0
165 0 $self->auth_storage->set_access_token_to_storage( $self->user,
166 $self->access_token );
167 }
168  
169 0 $tx = $self->build_http_transaction($params);
170 0 $res = $self->ua->start($tx)->res; # Mojo::Message::Response
171 }
172  
173 }
174  
175 0 return $res; # Mojo::Message::Response
176 }
177  
178  
179 0 ▶sub refresh_access_token {
Subroutine refresh_access_token: not called CC=5 SCAR=34.0
Pod documented
180 0 my ( $self, $credentials ) = @_;
181  
182 0 ▶ if ( ( !defined $credentials->{client_id} )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $credentials->{"client_id"} and defined $credentials->{"client_secret"} and defined $credentials->{"refresh_token"}) 0 0 0
Condition0%0/6
defined $credentials->{"client_id"} and defined $credentials->{"client_secret"} and defined $credentials->{"refresh_token"}
!ll&&!rl&&r
000
defined $credentials->{"client_id"} and defined $credentials->{"client_secret"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $credentials->{"client_id"} and defined $credentials->{"client_secret"} and defined $credentials->{"refresh_token"}
defined $credentials->{"client_id"} defined $credentials->{"client_secret"} defined $credentials->{"refresh_token"}
Truth table0%0/4
A and B and C
A= defined $credentials->{"client_id"}B= defined $credentials->{"client_secret"}C= defined $credentials->{"refresh_token"}
ABCresult
0XX0
10X0
1100
1111
183 || ( !defined $credentials->{client_secret} )
184 || ( !defined $credentials->{refresh_token} ) )
185 {
186 0 die
187 "Not enough credentials to refresh access_token. Check that you provided client_id, client_secret and refresh_token";
188 }
189  
190 0 ▶ warn "Attempt to refresh access_token with params: " . Dumper $credentials
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
191 if $self->debug;
192 0 $credentials->{grant_type} = 'refresh_token';
193 0 $self->ua->post(
194 'https://www.googleapis.com/oauth2/v4/token' => form => $credentials )
195 ->res->json; # tokens
196 }
197  
198 1 1;
199  
200 __END__
↑ Summary Google/Services.pm »