| 1 | package Google::RestApi; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '2.2.5'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Google::RestApi::Setup; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use File::Basename qw( dirname ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Furl (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use JSON::MaybeXS qw( decode_json encode_json JSON ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use List::Util (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Module::Load qw( load ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use PerlX::Maybe qw( maybe provided ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Retry::Backoff qw( retry ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Storable qw( dclone ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Try::Tiny qw( catch try ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use URI (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use URI::QueryParam (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 252 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 252 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 252 | my $self = merge_config_file(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 251 | resolve_config_file_path($self, 'log4perl_config'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | state $check = signature( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | bless => !!0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | named => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | config_file => ReadableFile, { optional => 1 }, # specify any of the below in a yaml file instead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | log4perl_config => ReadableFile, { optional => 1 }, # log4perl config file; alternative to GOOGLE_RESTAPI_LOGGER env var. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | auth => HashRef | HasMethods[qw(headers params)], # a string that will be used to construct an auth obj, or the obj itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | throttle => PositiveOrZeroInt, { default => 0 }, # mostly used for integration testing, to ensure we don't blow our rate limit. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | timeout => Int, { default => 120 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 251 | ▶ | max_attempts => PositiveInt->where(sub { $_ < 10; }), { default => 4 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 251 | $self = $check->(%$self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 251 | ▶ | if ($self->{log4perl_config} && !Log::Log4perl->initialized()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$$self{"log4perl_config"} and not "Log::Log4perl"->initialized
MC/DC50%1/2
$$self{"log4perl_config"} and not "Log::Log4perl"->initialized
$$self{"log4perl_config"}
not "Log::Log4perl"->initialized
Truth table66%2/3
A and not(B)
A= $$self{"log4perl_config"}B= not "Log::Log4perl"->initialized
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | Log::Log4perl->init($self->{log4perl_config}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 251 | $self->{ua} = Furl->new(timeout => $self->{timeout}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 251 | return bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | # this is the actual call to the google api endpoint. handles retries, error checking etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | # this would normally be called via Drive or Sheets objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 465 | ▶ | sub api { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 465 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 465 | state $check = signature( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | bless => !!0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | named => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | uri => StrMatch[qr(^https://)], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | method => StrMatch[qr/^(get|head|put|patch|post|delete)$/i], { default => 'get' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | params => HashRef[Str|ArrayRef[Str]], { default => {} }, # uri param string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | headers => ArrayRef[Str], { default => [] }, # http headers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | content => 0, # rest payload. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 465 | my $request = $check->(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | # reset our transaction for this new one. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 463 | $self->{transaction} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 463 | $self->_stat( $request->{method}, 'total' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 463 | $request->{method} = uc($request->{method}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 463 | $request->{caller_internal} = _caller_internal(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 463 | $request->{caller_external} = _caller_external(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 463 | my $request_content = $request->{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 463 | ▶ | my $request_json = defined $request_content ? encode_json($request_content) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | my @headers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 463 | ▶ | push(@headers, 'Content-Type' => 'application/json; charset=utf-8') if $request_json; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 463 | push(@headers, @{ $request->{headers} }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 463 | push(@headers, @{ $self->auth()->headers() }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # some (outdated) auth mechanisms may allow auth info in the params. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 463 | my %params = (%{ $request->{params} }, %{ $self->auth()->params() }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 463 | my $uri = URI->new($request->{uri}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 463 | $uri->query_form_hash(\%params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 463 | $request->{uri} = $uri->as_string(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 463 | DEBUG("Rest API request:\n", Dump($request)); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | my $http_req = HTTP::Request->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 463 | $request->{method}, $request->{uri}, \@headers, $request_json | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | # this is where the action is. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 463 | my ($response, $tries, $last_error) = $self->_api($http_req); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | # save the api call details so that the user can examine it in detail if necessary. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # further information is also recorded below in this routine. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | $self->{transaction} = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 463 | ▶ | request => $request, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | tries => $tries, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | ($response ? (response => $response) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | ($last_error ? (error => $last_error) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 463 | ▶ | if ($response) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 461 | my $decoded_response = $response->decoded_content(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | 461 | ▶ | $decoded_response = $decoded_response ? decode_json($decoded_response) : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 461 | $self->{transaction}->{decoded_response} = $decoded_response; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 461 | DEBUG("Rest API response:\n", Dump( $decoded_response )); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | # calls the callback when an api call is made, if any. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 463 | $self->_api_callback(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 463 | ▶ | if (!$response || !$response->is_success()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$response and $response->is_success
MC/DC100%2/2
$response and $response->is_success
$response
$response->is_success
Truth table100%3/3
A and B
A= $responseB= $response->is_success
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 11 | $self->_stat('error'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 11 | my $activation_url = _activation_url($self->transaction()); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 11 | ▶ | LOGDIE( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | ($activation_url ? "API not enabled in Google Cloud Console. Visit:\n $activation_url\n" : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | "Rest API failure:\n", Dump( $self->transaction() ), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # used for to avoid google 403's and 429's as with integration tests. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 452 | ▶ | sleep($self->{throttle}) if $self->{throttle}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 452 | return $self->{transaction}->{decoded_response}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | # this wraps the http api call around retries. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 463 | ▶ | sub _api { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_api: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 463 | my ($self, $req) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # default is exponential backoff, initial delay 1. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 463 | my $tries = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 463 | my $last_error; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 463 | my $refreshed_auth = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 483 | ▶ | my $response = retry sub { $self->{ua}->request($req); }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 483 | ▶ | retry_if => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 483 | my $h = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 483 | my $r = $h->{attempt_result}; # Furl::Response | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 483 | ▶ | if (!$r) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 8 | ▶ | $last_error = $@ || "Unknown error"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$@ || 'Unknown error'
MC/DC0%0/1
$@ || 'Unknown error'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 8 | WARN("API call error: $last_error"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 8 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 475 | ▶ | $last_error = $r->status_line() if !$r->is_success(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 475 | ▶ | if ($r->code() == 401 && !$refreshed_auth) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$r->code == 401 and not $refreshed_auth
MC/DC100%2/2
$r->code == 401 and not $refreshed_auth
$r->code == 401
not $refreshed_auth
Truth table100%3/3
A and not(B)
A= $r->code == 401B= not $refreshed_auth
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 2 | $refreshed_auth = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 2 | WARN("Got 401, refreshing auth token and retrying"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 2 | my @new_auth = @{ $self->auth()->refresh_headers() }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 2 | ▶ | while (my ($name, $val) = splice(@new_auth, 0, 2)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | $req->header($name => $val); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 473 | ▶ | if ($r->code() =~ /^(403|429|50[0234])$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 16 | WARN("Retrying: $last_error"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 16 | return 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 457 | return; # we're accepting the response. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 457 | ▶ | on_success => sub { $tries++; }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 26 | ▶ | on_failure => sub { $tries++; }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 463 | max_attempts => $self->max_attempts(); # override default max_attempts 10. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 463 | return ($response, $tries, $last_error); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | # convert a plain hash auth to an object if a hash was passed in new() above. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 938 | ▶ | sub auth { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
auth: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 938 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 938 | ▶ | if (!blessed($self->{auth})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | # Work on a shallow copy: the auth hashref may be owned by the caller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | # (e.g. a live slice of an application's on-disk config). Deleting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | # 'class' or adding 'config_dir' on the original would silently corrupt | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | # the caller's config, dropping the 'class' key when it is next saved. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 144 | my %auth = %{ $self->{auth} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | # turn OAuth2Client into Google::RestApi::Auth::OAuth2Client etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 144 | my $class = __PACKAGE__ . "::Auth::" . delete $auth{class}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | 144 | load $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | # add the path to the base config file so auth hash doesn't have | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | # to store the full path name for things like token_file etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | $auth{config_dir} = dirname($self->{config_file}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 143 | ▶ | if $self->{config_file}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 143 | $self->{auth} = $class->new(%auth); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 935 | return $self->{auth}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | # if user registered a callback, notify them of an api call. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 463 | ▶ | sub _api_callback { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_api_callback: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 463 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 463 | ▶ | return if !$self->{api_callback}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 5 | ▶ | try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 5 | $self->{api_callback}->( $self->transaction() ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 1 | ▶ | } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | 1 | my $err = $_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 1 | FATAL("Post process died: $err"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # sets the api callback code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 5 | ▶ | sub api_callback { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
api_callback: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 5 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 5 | state $check = signature(positional => [CodeRef, { optional => 1 }]); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 5 | my ($api_callback) = $check->(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | 5 | my $prev_api_callback = delete $self->{api_callback}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | 5 | ▶ | $self->{api_callback} = $api_callback if $api_callback; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 5 | return $prev_api_callback; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | # a simple record of how many gets, posts, deletes etc were completed. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | # useful for tuning network calls. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 474 | ▶ | sub _stat { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_stat: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 474 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 474 | my @stats = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 474 | foreach (@stats) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 937 | $_ = lc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 937 | ▶ | $self->{stats}->{$_} //= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$$self{"stats"}{$_} //= 0
MC/DC100%1/1
$$self{"stats"}{$_} //= 0
$$self{"stats"}{$_}
Truth table100%2/2
A
A= $$self{"stats"}{$_}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 937 | $self->{stats}->{$_}++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 474 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | # returns the stats recorded above. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 6 | ▶ | sub stats { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stats: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 6 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 6 | ▶ | my $stats = dclone($self->{stats} || {}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$self{'stats'} || {}
MC/DC0%0/1
$$self{'stats'} || {}
$$self{'stats'}
Truth table50%1/2
A
A= $$self{'stats'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 6 | return $stats; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 1 | ▶ | sub reset_stats { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
reset_stats: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 1 | delete $self->{stats}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | # this is built for every api call so the entire api call can be examined, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | # params, body content, http codes, etc etc. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 37 | ▶ | sub transaction { shift->{transaction} || {}; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
transaction: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | # used for debugging/logging purposes. tries to dig out a useful caller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | # so api calls can be traced back. skips some stuff that we use internally | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | # like cache. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 463 | ▶ | sub _caller_internal { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_caller_internal: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 463 | my ($package, $subroutine, $line, $i) = ('', '', 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 463 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition88%8/9
$package and $package =~ /^Cache::Memory/ || $subroutine =~ /^Cache|_cache/ || $subroutine =~ /api$/
$package =~ /^Cache::Memory/ or $subroutine =~ /^Cache|_cache/ or $subroutine =~ /api$/
$package =~ /^Cache::Memory/ or $subroutine =~ /^Cache|_cache/
MC/DC75%3/4
$package and $package =~ /^Cache::Memory/ || $subroutine =~ /^Cache|_cache/ || $subroutine =~ /api$/
$package
$package =~ /^Cache::Memory/
$subroutine =~ /^Cache|_cache/
$subroutine =~ /api$/
Truth table80%4/5
A and B or C or D
A= $packageB= $package =~ /^Cache::Memory/C= $subroutine =~ /^Cache|_cache/D= $subroutine =~ /api$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 2103 | ($package, undef, $line, $subroutine) = caller(++$i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | } while($package && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | $package =~ m|^Cache::Memory| || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | $subroutine =~ m[^Cache|_cache] || | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | $subroutine =~ m|api$| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | # not usually going to happen, but during testing we call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | # RestApi::api directly, so have to backtrack. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 463 | ▶ | ($package, undef, $line, $subroutine) = caller(--$i) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | if !$package; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 463 | return "$package:$line => $subroutine"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 463 | ▶ | sub _caller_external { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_caller_external: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | 463 | my ($package, $subroutine, $line, $i) = ('', '', 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 463 | ▶ | do { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$package and $package =~ /^(Google::RestApi|Cache|Try)/
MC/DC50%1/2
$package and $package =~ /^(Google::RestApi|Cache|Try)/
$package
$package =~ /^(Google::RestApi|Cache|Try)/
Truth table66%2/3
A and B
A= $packageB= $package =~ /^(Google::RestApi|Cache|Try)/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 3024 | ($package, undef, $line, $subroutine) = caller(++$i); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } while($package && $package =~ m[^(Google::RestApi|Cache|Try)]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 463 | return "$package:$line => $subroutine"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | # extract the activation URL from a 403 "API not enabled" error response, if present. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 11 | ▶ | sub _activation_url { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_activation_url: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 11 | my ($transaction) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 11 | my $details = eval { $transaction->{decoded_response}{error}{details} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 11 | ▶ | return unless ref $details eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 1 | for my $detail (@$details) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 1 | my $url = eval { $detail->{metadata}{activationUrl} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 1 | ▶ | return $url if $url; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 1 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | # the maximum number of attempts to call the google api endpoint before giving up. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | # undef returns current value. postitive int sets and returns new value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | # 0 sets and returns default value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 23 | ▶ | sub max_attempts { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
max_attempts: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 23 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 23 | ▶ | state $check = signature(positional => [PositiveOrZeroInt->where(sub { $_ < 10; }), { optional => 1 }]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 23 | my ($max_attempts) = $check->(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 23 | ▶ | $self->{max_attempts} = $max_attempts if $max_attempts; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 23 | ▶ | $self->{max_attempts} = 4 if defined $max_attempts && $max_attempts == 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $max_attempts and $max_attempts == 0
MC/DC100%2/2
defined $max_attempts and $max_attempts == 0
defined $max_attempts
$max_attempts == 0
Truth table100%3/3
A and B
A= defined $max_attemptsB= $max_attempts == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 23 | return $self->{max_attempts}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||