Client/UserAgent.pm

filter: Statementstmt 75.5% 65 / 86 Branchbran 36.5% 19 / 52 Conditioncond 41.1% 7 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 92.8% 13 / 14 Podpod 75.0% 3 / 4 totaltotal 58.4% 107 / 183 CC 34 SCAR 37.2
CC
34
Cov
59%
build_http_transaction
69.0
validated_api_query
46.0
header_with_bearer_auth_token
3.2
CRAP 41.1
↑ Summary Client.pm »
1 2 ▶use strictures;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package WebService::GoogleAPI::Client::UserAgent;
4  
5 2 our $VERSION = '0.27'; # VERSION
6 # ABSTRACT: User Agent wrapper for working with Google APIs
7  
8 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 extends 'Mojo::UserAgent';
11  
12 #extends 'Mojo::UserAgent::Mockable';
13 2 ▶use WebService::GoogleAPI::Client::AuthStorage::GapiJSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Data::Dump qw/pp/; # for dev debug
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Data::Printer filters => 'Web';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 ▶use Carp qw/croak carp cluck/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 has 'do_autorefresh' => (is => 'rw', default => 1);
21 2 has 'debug' => (is => 'rw', default => 0);
22 has 'auth_storage' => (
23 is => 'rw',
24 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 0 WebService::GoogleAPI::Client::AuthStorage::GapiJSON->new;
26 },
27 handles => [qw/get_access_token scopes user/],
28 trigger => 1,
29 2 ▶ isa => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
30 2 my $role = 'WebService::GoogleAPI::Client::AuthStorage';
31 2 ▶ die "auth_storage must implement the $role role to work!"
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]->does($role) 0 2 2
32 unless $_[0]->does($role);
33 },
34 2 lazy => 1
35 );
36  
37 2 ▶sub _trigger_auth_storage {
Subroutine _trigger_auth_storage: called CC=1 SCAR=0.0
38 2 my ($self) = @_;
39  
40 # give the auth_storage a ua
41 # TODO - this seems like code smell to me. Should these storage things be
42 # roles that get applied to this ua?
43 2 $self->auth_storage->ua($self);
44 }
45  
46 ## NB - could cache using https://metacpan.org/pod/Mojo::UserAgent::Cached
47 # TODO: Review source of this for ideas
48  
49  
50 ## NB - used by both Client and Discovery
51  
52 # Keep access_token in headers always actual
53  
54 ## performance tip as per https://developers.google.com/calendar/performance and similar links
55 ## NB - to work with Google APIs also assumes that Accept-Encoding: gzip is set in HTTP headers
56 3 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
57 3 my ($self) = @_;
58 3 $self->transactor->name(__PACKAGE__ . ' (gzip enabled)');
59 ## MAX SIZE ETC _ WHAT OTHER CONFIGURABLE PARAMS ARE AVAILABLE
60 }
61  
62  
63  
64 1 ▶sub header_with_bearer_auth_token {
Subroutine header_with_bearer_auth_token: called CC=3 SCAR=11.6
Pod documented
65 1 my ($self, $headers) = @_;
66  
67 1 ▶ $headers = {} unless defined $headers;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headers 0 1 1
68  
69 1 $headers->{'Accept-Encoding'} = 'gzip';
70  
71 1 ▶ if (my $token = $self->get_access_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $self->get_access_token) { } 1 0 1
72 1 $headers->{Authorization} = "Bearer $token";
73 } else {
74  
75 # TODO - why is this not fatal?
76 0 carp "Can't build Auth header, couldn't get an access token. Is your AuthStorage set up correctly?";
77 }
78 1 return $headers;
79 }
80  
81  
82 1 ▶sub build_http_transaction {
Subroutine build_http_transaction: called CC=19 SCAR=42.3
Pod documented
83 1 my ($self, $params) = @_;
84 ## hack to allow method option as alias for httpMethod
85  
86 1 ▶ $params->{httpMethod} = $params->{method} if defined $params->{method};
Branch: 1 branch
BranchTrueFalseTotal
if defined $params->{"method"} 1 0 1
87 1 ▶ $params->{httpMethod} = '' unless defined $params->{httpMethod};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params->{"httpMethod"} 0 1 1
88  
89 1 ▶ my $http_method = uc($params->{httpMethod}) || 'GET'; # uppercase ?
Condition50%1/2
uc $params->{'httpMethod'} || 'GET'
l!l
10
MC/DC0%0/1
uc $params->{'httpMethod'} || 'GET'
uc $params->{'httpMethod'}
Truth table50%1/2
A
A= uc $params->{'httpMethod'}
Aresult
11
00
90 1 ▶ my $optional_data = $params->{options} || '';
Condition50%1/2
$params->{'options'} || ''
l!l
10
MC/DC0%0/1
$params->{'options'} || ''
$params->{'options'}
Truth table50%1/2
A
A= $params->{'options'}
Aresult
11
00
91 my $path = $params->{path}
92 1 ▶ || cluck('path parameter required for build_http_transaction');
Condition33%1/3
$params->{'path'} || cluck('path parameter required for build_http_transaction')
l!l&&r!l&&!r
100
MC/DC0%0/2
$params->{'path'} || cluck('path parameter required for build_http_transaction')
$params->{'path'} cluck('path parameter required for build_http_transaction')
Truth table33%1/3
A || B
A= $params->{'path'}B= cluck('path parameter required for build_http_transaction')
ABresult
1X1
011
000
93 my $no_auth = $params->{no_auth}
94 1 ▶ || 0; ## default to including auth header - ie not setting no_auth
Condition50%1/2
$params->{'no_auth'} || 0
l!l
01
MC/DC0%0/1
$params->{'no_auth'} || 0
$params->{'no_auth'}
Truth table50%1/2
A
A= $params->{'no_auth'}
Aresult
11
00
95 1 ▶ my $headers = $params->{headers} || {};
Condition50%1/2
$params->{'headers'} || {}
l!l
01
MC/DC0%0/1
$params->{'headers'} || {}
$params->{'headers'}
Truth table50%1/2
A
A= $params->{'headers'}
Aresult
11
00
96  
97 1 ▶ cluck 'Attention! You are using POST, but no payload specified'
Branch: 1 branch
BranchTrueFalseTotal
if $http_method eq "POST" and not defined $optional_data 0 1 1
Condition33%1/3
$http_method eq "POST" and not defined $optional_data
!ll&&!rl&&r
100
MC/DC0%0/2
$http_method eq "POST" and not defined $optional_data
$http_method eq "POST" not defined $optional_data
Truth table33%1/3
A and not(B)
A= $http_method eq "POST"B= not defined $optional_data
ABresult
0X0
100
111
98 if (($http_method eq 'POST') && !defined $optional_data);
99 1 ▶ cluck "build_http_transaction:: $http_method $path " if ($self->debug > 11);
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 11 0 1 1
100 1 ▶ cluck "$http_method Not a SUPPORTED HTTP method parameter specified to build_http_transaction" . pp $params
Branch: 1 branch
BranchTrueFalseTotal
unless $http_method =~ /^GET|PATH|PUT|POST|PATCH|DELETE$/mix 0 1 1
101 unless $http_method =~ /^GET|PATH|PUT|POST|PATCH|DELETE$/ixm;
102  
103 ## NB - headers not passed if no_auth
104 1 ▶ $headers = $self->header_with_bearer_auth_token($headers) unless $no_auth;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_auth 1 0 1
105 1 ▶ if ($http_method =~ /^POST|PATH|PUT|PATCH$/ixg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($http_method =~ /^POST|PATH|PUT|PATCH$/gix) { } 0 1 1
106 ## ternary conditional on whether optional_data is set
107 ## return $optional_data eq '' ? $self->build_tx( $http_method => $path => $headers ) : $self->build_tx( $http_method => $path => $headers => json => $optional_data );
108 0 ▶ if ($optional_data eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($optional_data eq '') { } 0 0 0
109 0 return $self->build_tx($http_method => $path => $headers);
110 } else {
111 0 ▶ if (ref($optional_data) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $optional_data eq 'HASH') { } 0 0 0
elsif (ref $optional_data eq '') { } 0 0 0
112 0 return $self->build_tx($http_method => $path => $headers => json => $optional_data);
113 } elsif (ref($optional_data) eq '') ## am assuming is a post with options containing a binary payload
114 {
115 0 return $self->build_tx($http_method => $path => $headers => $optional_data);
116 }
117  
118 }
119 } else { ## DELETE or GET
120 1 ▶ return $self->build_tx($http_method => $path => $headers => form => $optional_data)
Branch: 1 branch
BranchTrueFalseTotal
if $http_method eq "GET" 1 0 1
121 if ($http_method eq 'GET');
122 0 ▶ return $self->build_tx($http_method => $path => $headers)
Branch: 1 branch
BranchTrueFalseTotal
if $http_method eq "DELETE" 0 0 0
123 if ($http_method eq 'DELETE');
124 }
125  
126 #return undef; ## assert: should never get here
127 }
128  
129  
130  
131  
132 # NOTE validated means that we assume checking against discovery specs has already been done.
133 1 ▶sub validated_api_query {
Subroutine validated_api_query: called CC=13 SCAR=38.3
Pod documented
134 1 my ($self, $params) = @_;
135  
136 ## assume is a GET for the URI at $params
137 1 ▶ if (ref($params) eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $params eq "") 1 0 1
138 1 ▶ cluck("transcribing $params to a hashref for validated_api_query")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
139 if $self->debug;
140 1 my $val = $params;
141 1 $params = { path => $val, method => 'get', options => {}, };
142 }
143  
144 1 my $tx = $self->build_http_transaction($params);
145  
146 1 ▶ cluck("$params->{method} $params->{path}") if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
147  
148 #TODO- figure out how we can alter this to use promises
149 # at this point, i think we'd have to make a different method entirely to
150 # do this promise-wise
151 1 my $res = $self->start($tx)->res;
152 1 $res->{_token} = $self->get_access_token;
153 1 ▶ if (!$res->code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->code) 0 1 1
154 0 cluck "Response has no code! Network error? here's the object:\n" . np $res;
155 0 return $res;
156 }
157  
158 1 ▶ if (($res->code == 401) && $self->do_autorefresh) {
Branch: 3 branches
BranchTrueFalseTotal
if ($res->code == 401 and $self->do_autorefresh) { } 0 1 1
elsif ($res->code == 403) { } 0 1 1
elsif ($res->code == 429) { } 0 1 1
Condition33%1/3
$res->code == 401 and $self->do_autorefresh
!ll&&!rl&&r
100
MC/DC0%0/2
$res->code == 401 and $self->do_autorefresh
$res->code == 401 $self->do_autorefresh
Truth table33%1/3
A and B
A= $res->code == 401B= $self->do_autorefresh
ABresult
0X0
100
111
159 0 ▶ cluck "Your access token was expired. Attempting to update it automatically..."
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 11 0 0 0
160 if ($self->debug > 11);
161  
162 0 $self->auth_storage->refresh_access_token($self);
163  
164 0 return $self->validated_api_query($params);
165 } elsif ($res->code == 403) {
166 0 cluck('Unexpected permission denied 403 error');
167 0 return $res;
168 } elsif ($res->code == 429) {
169 0 cluck('HTTP 429 - you hit a rate limit. Try again later');
170 0 return $res;
171 }
172 1 ▶ return $res if $res->code == 200;
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 200 1 0 1
173 0 ▶ return $res if $res->code == 204; ## NO CONTENT - INDICATES OK FOR DELETE ETC
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 204 0 0 0
174 0 ▶ return $res if $res->code == 400; ## general failure
Branch: 1 branch
BranchTrueFalseTotal
if $res->code == 400 0 0 0
175 0 cluck("unhandled validated_api_query response code; here's the object:\n" . np $res);
176 0 return $res;
177 }
178  
179 2 1;
180  
181 __END__
↑ Summary Client.pm »