File Coverage

blib/lib/Google/Client/Role/FurlAgent.pm
Criterion Covered Total %
statement 39 39 100.0
branch 9 10 90.0
condition 2 2 100.0
subroutine 9 9 100.0
pod n/a
total 59 60 98.3


line stmt bran cond sub pod time code
1             package Google::Client::Role::FurlAgent;
2             $Google::Client::Role::FurlAgent::VERSION = '0.005';
3 17     17   6912 use strict;
  17         20  
  17         411  
4 17     17   53 use warnings;
  17         17  
  17         296  
5              
6 17     17   41 use Moo::Role;
  17         18  
  17         68  
7              
8 17     17   3757 use Carp;
  17         20  
  17         710  
9 17     17   56 use Cpanel::JSON::XS;
  17         19  
  17         715  
10 17     17   1214 use Furl;
  17         47147  
  17         346  
11 17     17   7545 use URI;
  17         52443  
  17         4394  
12              
13             has ua => (
14             is => 'ro',
15             default => sub { return Furl->new(); }
16             );
17              
18             # Hook that checks if an access token is available before
19             # making API requests. Will die with error if not found.
20              
21             # It would be wise to store the access token in a cache
22             # which expires in the 'expires_in' seconds returned
23             # by Google with the token, that way you will know
24             # when to refresh it or request a new one.
25              
26             before _request => sub {
27             my $self = shift;
28              
29             unless ( $self->access_token ) {
30             confess('access token not found or may have expired');
31             }
32             };
33              
34             # Performs a request with the given parameters. These should be the same parameters
35             # accepted by L. Returns the responses
36             # JSON.
37             # Will add an Authorization header with the access_token attributes value to the request.
38             # Can die with an error if the response code was not a successful one, or if there was
39             # an error decoding the JSON data. For requests that do not return any content, will
40             # just return undef to indicate we have received no content.
41              
42             sub _request {
43 14     14   1613 my ($self, %req) = @_;
44              
45 14         232 $req{headers} = ['Authorization', 'Bearer '.$self->access_token];
46 14         893 my $response = $self->ua->request(%req);
47              
48 14 100       1024 unless ( $response->is_success ) {
49 1         39 confess("Google API request failed: \n\n" . $response->as_string);
50             }
51              
52 13 100       506 return unless ( $response->decoded_content );
53              
54 11 100       377 if ( $response->content_type !~ m/application\/json/ ) {
55             # not sure what it is, just return the content since it's successful
56 1         31 return $response->decoded_content;
57             }
58              
59 10         319 my $json = eval { decode_json($response->decoded_content); };
  10         40  
60 10 50       439 confess("Error decoding JSON: $@") if $@;
61              
62 10         40 return $json;
63             }
64              
65             sub _url {
66 12     12   20 my ($self, $uri, $params) = @_;
67 12   100     56 $uri ||= '';
68 12         111 my $url = URI->new($self->base_url . $uri);
69 12 100       67409 if ( $params ) {
70 9         59 $url->query_form($params);
71             }
72 12         875 return $url->as_string;
73             }
74              
75             =head1 NAME
76              
77             Google::Client::Role::FurlAgent
78              
79             =head1 DESCRIPTION
80              
81             A Furl useragent used to make requests to Googles REST API and do other helpful
82             tasks such as building URLs and return JSON content.
83              
84             Used by the Google::Client::* modules
85              
86             =head1 AUTHOR
87              
88             Ali Zia, C<< >>
89              
90             =head1 REPOSITORY
91              
92             L
93              
94             =head1 COPYRIGHT AND LICENSE
95              
96             This is free software. You may use it and distribute it under the same terms as Perl itself.
97             Copyright (C) 2016 - Ali Zia
98              
99             =cut
100              
101             1;