File Coverage

blib/lib/API/Google.pm
Criterion Covered Total %
statement 18 86 20.9
branch 0 30 0.0
condition 0 8 0.0
subroutine 6 18 33.3
pod 4 12 33.3
total 28 154 18.1


line stmt bran cond sub pod time code
1             package API::Google;
2             $API::Google::VERSION = '0.12';
3 1     1   15309 use Data::Dumper;
  1         8609  
  1         66  
4              
5             # ABSTRACT: Perl library for easy access to Google services via their API
6              
7              
8 1     1   8 use strict;
  1         2  
  1         23  
9 1     1   6 use warnings;
  1         8  
  1         35  
10 1     1   669 use Mojo::UserAgent;
  1         336722  
  1         8  
11 1     1   546 use Config::JSON;
  1         48384  
  1         47  
12 1     1   8 use Data::Dumper;
  1         1  
  1         933  
13              
14              
15             sub new {
16 0     0 0   my ($class, $params) = @_;
17 0           my $h = {};
18 0 0         if ($params->{tokensfile}) {
19 0           $h->{tokensfile} = Config::JSON->new($params->{tokensfile});
20             } else {
21 0           die 'no json file specified!';
22             }
23 0           $h->{ua} = Mojo::UserAgent->new();
24 0           $h->{debug} = $params->{debug};
25 0   0       $h->{max_refresh_attempts} = $params->{max_refresh_attempts} || 5;
26 0           return bless $h, $class;
27             }
28              
29              
30              
31             sub refresh_access_token {
32 0     0 0   my ($self, $params) = @_;
33 0 0         warn "Attempt to refresh access_token with params: ".Dumper $params if $self->{debug};
34 0           $params->{grant_type} = 'refresh_token';
35 0           $self->{ua}->post('https://www.googleapis.com/oauth2/v4/token' => form => $params)->res->json; # tokens
36             };
37              
38              
39             sub client_id {
40 0     0 0   shift->{tokensfile}->get('gapi/client_id');
41             }
42              
43             sub ua {
44 0     0 0   shift->{ua};
45             }
46              
47              
48             sub client_secret {
49 0     0 0   shift->{tokensfile}->get('gapi/client_secret');
50             }
51              
52              
53              
54             sub refresh_access_token_silent {
55 0     0 1   my ($self, $user) = @_;
56 0           my $tokens = $self->refresh_access_token({
57             client_id => $self->client_id,
58             client_secret => $self->client_secret,
59             refresh_token => $self->get_refresh_token_from_storage($user)
60             });
61 0 0         warn "New tokens got" if $self->{debug};
62 0           my $res = {};
63 0           $res->{old} = $self->get_access_token_from_storage($user);
64 0 0         warn Dumper $tokens if $self->{debug};
65 0 0         if ($tokens->{access_token}) {
66 0           $self->set_access_token_to_storage($user, $tokens->{access_token});
67             }
68 0           $res->{new} = $self->get_access_token_from_storage($user);
69 0           return $res;
70             };
71              
72              
73             sub get_refresh_token_from_storage {
74 0     0 0   my ($self, $user) = @_;
75 0 0         warn "get_refresh_token_from_storage(".$user.")" if $self->{debug};
76 0           return $self->{tokensfile}->get('gapi/tokens/'.$user.'/refresh_token');
77             };
78              
79             sub get_access_token_from_storage {
80 0     0 0   my ($self, $user) = @_;
81 0           $self->{tokensfile}->get('gapi/tokens/'.$user.'/access_token');
82             };
83              
84             sub set_access_token_to_storage {
85 0     0 0   my ($self, $user, $token) = @_;
86 0           $self->{tokensfile}->set('gapi/tokens/'.$user.'/access_token', $token);
87             };
88              
89              
90              
91             sub build_headers {
92 0     0 1   my ($self, $user) = @_;
93 0           my $t = $self->get_access_token_from_storage($user);
94 0           my $headers = {};
95 0           $headers->{'Authorization'} = 'Bearer '.$t;
96 0           return $headers;
97             }
98              
99              
100             sub build_http_transaction {
101 0     0 1   my ($self, $params) = @_;
102              
103 0 0         warn "build_http_transaction() params : ".Dumper $params if $self->{debug};
104              
105 0           my $headers = $self->build_headers($params->{user});
106 0           my $http_method = $params->{method};
107 0           my $tx;
108              
109 0 0 0       if ($http_method eq 'get' || $http_method eq 'delete') {
    0 0        
110 0           $tx = $self->{ua}->build_tx(uc $http_method => $params->{route} => $headers);
111             } elsif (($http_method eq 'post') && $params->{payload}) {
112             $tx = $self->{ua}->build_tx(uc $http_method => $params->{route} => $headers => json => $params->{payload})
113 0           } else {
114 0           die 'wrong http_method on no payload if using POST';
115             }
116 0           return $tx;
117              
118             }
119              
120              
121              
122              
123             sub api_query {
124 0     0 1   my ($self, $params, $payload) = @_;
125              
126 0 0         warn "api_query() params : ".Dumper $params if $self->{debug};
127              
128 0           $payload = { payload => $payload };
129 0           %$params = (%$params, %$payload);
130              
131 0           my $tx = $self->build_http_transaction($params);
132 0           my $res = $self->{ua}->start($tx)->res->json;
133            
134             # for future:
135             # if ( grep { $_->{message} eq 'Invalid Credentials' && $_->{reason} eq 'authError'} @{$res->{error}{errors}} ) { ... }
136              
137 0 0         warn "First api_query() result : ".Dumper $res if $self->{debug};
138              
139 0 0         if (defined $res->{error}) { # token expired error handling
140              
141 0           my $attempt = 1;
142              
143 0           while ($res->{error}{message} eq 'Invalid Credentials') {
144 0 0         if ($attempt == $self->{max_refresh_attempts}) {
145 0           last;
146             }
147 0 0         warn "Seems like access_token was expired. Attemptimg update it automatically ..." if $self->{debug};
148 0           $self->refresh_access_token_silent($params->{user});
149 0           $tx = $self->build_http_transaction($params);
150 0           $res = $self->{ua}->start($tx)->res->json;
151 0           $attempt++;
152             }
153              
154 0 0         if ($attempt > 1) {
155 0           warn "access_token was automatically refreshed";
156             }
157              
158             }
159              
160 0           return $res;
161             };
162              
163              
164             1;
165              
166             __END__