| 1 | 2 | ▶ | use strictures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use 5.14.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package WebService::GoogleAPI::Client; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.27'; # VERSION | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Data::Dump qw/pp/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use WebService::GoogleAPI::Client::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use WebService::GoogleAPI::Client::Discovery; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use WebService::GoogleAPI::Client::AuthStorage::GapiJSON; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use WebService::GoogleAPI::Client::AuthStorage::ServiceAccount; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use CHI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Mojo::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | #TODO- batch requests. The only thing necessary is to send a | ||||||||||||||||||||||||||||
| 19 | #multipart request as I wrote in that e-mail. | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | #TODO- allow using promises instead of just calling. Also allow | ||||||||||||||||||||||||||||
| 22 | # for full access to the tx instead of assuming it's always | ||||||||||||||||||||||||||||
| 23 | # returning the res. Perhaps mix in something that delegates the | ||||||||||||||||||||||||||||
| 24 | # json method to the res? | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # ABSTRACT: Google API Discovery and SDK | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # FROM MCE POD -- <p><img src="https://img.shields.io/cpan/v/WebService-GoogleAPI-Client.png" width="664" height="446" alt="Bank Queuing Model" /></p> | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 2 | has 'debug' => (is => 'rw', default => 0, lazy => 1); | |||||||||||||||||||||||||||
| 32 | has 'ua' => ( | ||||||||||||||||||||||||||||
| 33 | handles => [qw/do_autorefresh auth_storage get_access_token scopes user/], | ||||||||||||||||||||||||||||
| 34 | is => 'ro', | ||||||||||||||||||||||||||||
| 35 | 1 | ▶ | default => sub { WebService::GoogleAPI::Client::UserAgent->new(debug => shift->debug) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 2 | lazy => 1, | |||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub get_scopes_as_array { | ||||||||||||||||||||||||||
|
Subroutine
get_scopes_as_array: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 0 | carp 'get_scopes_as_array has deprecated in favor of the shorter "scopes"'; | |||||||||||||||||||||||||||
| 41 | 0 | return $_[0]->scopes; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | has 'chi' => ( | ||||||||||||||||||||||||||||
| 45 | is => 'rw', | ||||||||||||||||||||||||||||
| 46 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 1 | CHI->new(driver => 'File', max_key_length => 512, namespace => __PACKAGE__); | |||||||||||||||||||||||||||
| 48 | }, | ||||||||||||||||||||||||||||
| 49 | 2 | lazy => 1 | |||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | has 'discovery' => ( | ||||||||||||||||||||||||||||
| 53 | handles => [ | ||||||||||||||||||||||||||||
| 54 | qw/ discover_all | ||||||||||||||||||||||||||||
| 55 | get_method_details | ||||||||||||||||||||||||||||
| 56 | get_api_document service_exists | ||||||||||||||||||||||||||||
| 57 | methods_available_for_google_api_id list_api_ids / | ||||||||||||||||||||||||||||
| 58 | ], | ||||||||||||||||||||||||||||
| 59 | is => 'ro', | ||||||||||||||||||||||||||||
| 60 | 1 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 1 | return WebService::GoogleAPI::Client::Discovery->new( | |||||||||||||||||||||||||||
| 63 | debug => $self->debug, | ||||||||||||||||||||||||||||
| 64 | ua => $self->ua, | ||||||||||||||||||||||||||||
| 65 | chi => $self->chi | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | }, | ||||||||||||||||||||||||||||
| 68 | 2 | lazy => 1, | |||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | ## provides a way of augmenting constructor (new) without overloading it | ||||||||||||||||||||||||||||
| 72 | ## see https://metacpan.org/pod/distribution/Moose/lib/Moose/Manual/Construction.pod if like me you an new to Moose | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | #NOTE- in terms of implementing google's ADC | ||||||||||||||||||||||||||||
| 76 | # (see https://cloud.google.com/docs/authentication/production and also | ||||||||||||||||||||||||||||
| 77 | # https://github.com/googleapis/python-cloud-core/blob/master/google/cloud/client.py) | ||||||||||||||||||||||||||||
| 78 | # I looked into it and based on that, it seems that every environment has different reqs, | ||||||||||||||||||||||||||||
| 79 | # so it's a maintenance liability | ||||||||||||||||||||||||||||
| 80 | 1 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=7 SCAR=26.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 1 | my ($self, $params) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | my ($storage, $file); | |||||||||||||||||||||||||||
| 84 | 1 | ▶ | if ($params->{auth_storage}) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 85 | 0 | $storage = $params->{auth_storage}; | |||||||||||||||||||||||||||
| 86 | } elsif ($file = $params->{gapi_json}) { | ||||||||||||||||||||||||||||
| 87 | 1 | $storage = WebService::GoogleAPI::Client::AuthStorage::GapiJSON->new(path => $file); | |||||||||||||||||||||||||||
| 88 | } elsif ($file = $params->{service_account}) { | ||||||||||||||||||||||||||||
| 89 | $storage = WebService::GoogleAPI::Client::AuthStorage::ServiceAccount->new( | ||||||||||||||||||||||||||||
| 90 | path => $file, | ||||||||||||||||||||||||||||
| 91 | scopes => $params->{scopes} | ||||||||||||||||||||||||||||
| 92 | 0 | ); | |||||||||||||||||||||||||||
| 93 | } elsif ($file = $ENV{GOOGLE_APPLICATION_CREDENTIALS}) { | ||||||||||||||||||||||||||||
| 94 | $storage = WebService::GoogleAPI::Client::AuthStorage::ServiceAccount->new( | ||||||||||||||||||||||||||||
| 95 | path => $file, | ||||||||||||||||||||||||||||
| 96 | scopes => $params->{scopes} | ||||||||||||||||||||||||||||
| 97 | 0 | ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 1 | ▶ | $self->auth_storage($storage) if $storage; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | ▶ | $self->user($params->{user}) if (defined $params->{user}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | ## ASSUMPTIONS: | ||||||||||||||||||||||||||||
| 106 | ## - no complex parameter checking ( eg required mediaUpload in endpoint gmail.users.messages.send ) so user assumes responsiiblity | ||||||||||||||||||||||||||||
| 107 | ## TODO: Exceeding a rate limit will cause an HTTP 403 or HTTP 429 Too Many Requests response and your app should respond by retrying with exponential backoff. (https://developers.google.com/gmail/api/v1/reference/quota) | ||||||||||||||||||||||||||||
| 108 | ## follows the method spec reqeust reference to the api spec schema api_discovery_struct->{schemas}{Message}; | ||||||||||||||||||||||||||||
| 109 | ## THE METHOD SPEC CONTAINS | ||||||||||||||||||||||||||||
| 110 | ## 'request' => { | ||||||||||||||||||||||||||||
| 111 | ## '$ref' => 'Message' | ||||||||||||||||||||||||||||
| 112 | ## }, | ||||||||||||||||||||||||||||
| 113 | ## THE SCHEMA api_discovery_struct->{schemas}{Message} CONTAINS | ||||||||||||||||||||||||||||
| 114 | ## 'id' => 'Message', | ||||||||||||||||||||||||||||
| 115 | ## 'properties' => { | ||||||||||||||||||||||||||||
| 116 | ## 'raw' => { | ||||||||||||||||||||||||||||
| 117 | ## {annotations}{required}[ 'gmail.users.drafts.create','gmail.users.drafts.update','gmail.users.messages.insert','gmail.users.messages.send' ] | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | ## NB - uses the ua api_query to execute the server request | ||||||||||||||||||||||||||||
| 120 | ################################################## | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub api_query { | ||||||||||||||||||||||||||
|
Subroutine
api_query: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 0 | my ($self, @params_array) = @_; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | ## TODO - find a more elgant idiom to do this - pulled this off top of head for quick imeplementation | ||||||||||||||||||||||||||||
| 125 | 0 | my $params = {}; | |||||||||||||||||||||||||||
| 126 | 0 | ▶ | if (scalar(@params_array) == 1 && ref($params_array[0]) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
scalar @params_array == 1 and ref $params_array[0] eq "HASH"
MC/DC0%0/2
scalar @params_array == 1 and ref $params_array[0] eq "HASH"
scalar @params_array == 1
ref $params_array[0] eq "HASH"
Truth table0%0/3
A and B
A= scalar @params_array == 1B= ref $params_array[0] eq "HASH"
| |||||||||||||||||||||||||||||
| 127 | 0 | $params = $params_array[0]; | |||||||||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||||||||||
| 129 | 0 | $params = {@params_array}; ## what happens if not even count | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | 0 | ▶ | carp(pp $params) if $self->debug > 10; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | croak "Missing neccessary scopes to access $params->{api_endpoint_id}" | ||||||||||||||||||||||||||||
| 134 | 0 | ▶ | unless $self->has_scope_to_access_api_endpoint($params->{api_endpoint_id}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | ## used to collect pre-query validation errors - if set we return a response | ||||||||||||||||||||||||||||
| 137 | # with 418 I'm a teapot | ||||||||||||||||||||||||||||
| 138 | 0 | my @teapot_errors = (); | |||||||||||||||||||||||||||
| 139 | ## pre-query validation if api_id parameter is included | ||||||||||||||||||||||||||||
| 140 | @teapot_errors = $self->_process_params($params) | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | if (defined $params->{api_endpoint_id}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | ## either as param or from discovery | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | if (not defined $params->{path}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 0 | push @teapot_errors, 'path is a required parameter'; | |||||||||||||||||||||||||||
| 146 | 0 | $params->{path} = ''; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | push @teapot_errors, "Path '$params->{path}' includes unfilled variable after processing" | ||||||||||||||||||||||||||||
| 149 | 0 | ▶ | if ($params->{path} =~ /\{.+\}/xms); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | ## carp and include in 418 TEAPOT ERROR - response body with @teapot errors | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if (@teapot_errors > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | carp(join("\n", @teapot_errors)) if $self->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | return Mojo::Message::Response->new( | |||||||||||||||||||||||||||
| 154 | content_type => 'text/plain', | ||||||||||||||||||||||||||||
| 155 | code => 418, | ||||||||||||||||||||||||||||
| 156 | message => 'Teapot Error - Reqeust blocked before submitting to server with pre-query validation errors', | ||||||||||||||||||||||||||||
| 157 | body => join("\n", @teapot_errors) | ||||||||||||||||||||||||||||
| 158 | ); | ||||||||||||||||||||||||||||
| 159 | } else { | ||||||||||||||||||||||||||||
| 160 | ## query looks good - send to user agent to execute | ||||||||||||||||||||||||||||
| 161 | 0 | return $self->ua->validated_api_query($params); | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | ################################################## | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | ################################################## | ||||||||||||||||||||||||||||
| 167 | ## _ensure_api_spec_has_defined_fields is really only used to allow carping without undef warnings if needed | ||||||||||||||||||||||||||||
| 168 | 10 | ▶ | sub _ensure_api_spec_has_defined_fields { | ||||||||||||||||||||||||||
|
Subroutine
_ensure_api_spec_has_defined_fields: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 169 | 10 | my ($self, $api_discovery_struct) = @_; | |||||||||||||||||||||||||||
| 170 | ## Ensure API Discovery has expected fields defined | ||||||||||||||||||||||||||||
| 171 | 10 | foreach my $expected_key ( | |||||||||||||||||||||||||||
| 172 | qw/path title ownerName version id discoveryVersion | ||||||||||||||||||||||||||||
| 173 | revision description documentationLink rest/ | ||||||||||||||||||||||||||||
| 174 | ) { | ||||||||||||||||||||||||||||
| 175 | $api_discovery_struct->{$expected_key} = '' | ||||||||||||||||||||||||||||
| 176 | 100 | ▶ | unless defined $api_discovery_struct->{$expected_key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | $api_discovery_struct->{canonicalName} = $api_discovery_struct->{title} | ||||||||||||||||||||||||||||
| 179 | 10 | ▶ | unless defined $api_discovery_struct->{canonicalName}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 10 | return $api_discovery_struct; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | ################################################## | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | ################################################## | ||||||||||||||||||||||||||||
| 185 | 0 | ▶ | sub _process_params_for_api_endpoint_and_return_errors { | ||||||||||||||||||||||||||
|
Subroutine
_process_params_for_api_endpoint_and_return_errors: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 186 | 0 | warn '_process_params_for_api_endpoint_and_return_errors has been deprecated. Please use _process_params'; | |||||||||||||||||||||||||||
| 187 | 0 | _process_params(@_); | |||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 10 | ▶ | sub _process_params { | ||||||||||||||||||||||||||
|
Subroutine
_process_params: called CC=11 SCAR=24.2
| |||||||||||||||||||||||||||||
| 191 | ## nb - api_endpoint is a param - param key values are modified through this sub | ||||||||||||||||||||||||||||
| 192 | 10 | my ($self, $params) = @_; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | croak('this should never happen - this method is internal only!') | ||||||||||||||||||||||||||||
| 195 | 10 | ▶ | unless defined $params->{api_endpoint_id}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | ## $api_discovery_struct requried for service base URL | ||||||||||||||||||||||||||||
| 198 | my $api_discovery_struct | ||||||||||||||||||||||||||||
| 199 | 10 | = $self->_ensure_api_spec_has_defined_fields($self->discovery->get_api_document($params->{api_endpoint_id})); | |||||||||||||||||||||||||||
| 200 | ## remove trailing '/' from baseUrl | ||||||||||||||||||||||||||||
| 201 | 10 | $api_discovery_struct->{baseUrl} =~ s/\/$//sxmg; | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | ## if can get discovery data for google api endpoint then continue to perform | ||||||||||||||||||||||||||||
| 204 | # detailed checks | ||||||||||||||||||||||||||||
| 205 | 10 | my $method_discovery_struct = $self->get_method_details($params->{api_endpoint_id}); | |||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | #save away original path so we can know if it's fiddled with | ||||||||||||||||||||||||||||
| 208 | #later | ||||||||||||||||||||||||||||
| 209 | 10 | $method_discovery_struct->{origPath} = $method_discovery_struct->{path}; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | ## allow optional user callback pre-processing of method_discovery_struct | ||||||||||||||||||||||||||||
| 212 | 1 | $method_discovery_struct = &{ $params->{cb_method_discovery_modify} }($method_discovery_struct) | |||||||||||||||||||||||||||
| 213 | if (defined $params->{cb_method_discovery_modify} | ||||||||||||||||||||||||||||
| 214 | 10 | ▶ | && ref($params->{cb_method_discovery_modify}) eq 'CODE'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $params->{"cb_method_discovery_modify"} and ref $params->{"cb_method_discovery_modify"} eq "CODE"
MC/DC50%1/2
defined $params->{"cb_method_discovery_modify"} and ref $params->{"cb_method_discovery_modify"} eq "CODE"
defined $params->{"cb_method_discovery_modify"}
ref $params->{"cb_method_discovery_modify"} eq "CODE"
Truth table66%2/3
A and B
A= defined $params->{"cb_method_discovery_modify"}B= ref $params->{"cb_method_discovery_modify"} eq "CODE"
| |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 10 | my @teapot_errors = (); ## errors are pushed into this as encountered | |||||||||||||||||||||||||||
| 217 | $params->{method} = $method_discovery_struct->{httpMethod} || 'GET' | ||||||||||||||||||||||||||||
| 218 | 10 | ▶ | if (not defined $params->{method}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$method_discovery_struct->{'httpMethod'} || 'GET'
MC/DC0%0/1
$method_discovery_struct->{'httpMethod'} || 'GET'
$method_discovery_struct->{'httpMethod'}
Truth table50%1/2
A
A= $method_discovery_struct->{'httpMethod'}
| |||||||||||||||||||||||||||||
| 219 | push(@teapot_errors, | ||||||||||||||||||||||||||||
| 220 | "method mismatch - you requested a $params->{method} which conflicts with discovery spec requirement for $method_discovery_struct->{httpMethod}" | ||||||||||||||||||||||||||||
| 221 | 10 | ▶ | ) if ($params->{method} !~ /^$method_discovery_struct->{httpMethod}$/sxim); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | ## Set default path iff not set by user - NB - will prepend baseUrl later | ||||||||||||||||||||||||||||
| 224 | 10 | ▶ | $params->{path} = $method_discovery_struct->{path} unless $params->{path}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | 10 | ▶ | push @teapot_errors, 'path is a required parameter' unless $params->{path}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 10 | push @teapot_errors, | |||||||||||||||||||||||||||
| 228 | $self->_interpolate_path_parameters_append_query_params_and_return_errors($params, $method_discovery_struct); | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 10 | $params->{path} =~ s/^\///sxmg; ## remove leading '/' from path | |||||||||||||||||||||||||||
| 231 | $params->{path} = "$api_discovery_struct->{baseUrl}/$params->{path}" | ||||||||||||||||||||||||||||
| 232 | 10 | ▶ | unless $params->{path} =~ /^$api_discovery_struct->{baseUrl}/ixsmg; ## prepend baseUrl if required | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | ## if errors - add detail available in the discovery struct for the method and service to aid debugging | ||||||||||||||||||||||||||||
| 235 | 10 | ▶ | push @teapot_errors, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | qq{ $api_discovery_struct->{title} $api_discovery_struct->{rest} API into $api_discovery_struct->{ownerName} $api_discovery_struct->{canonicalName} $api_discovery_struct->{version} with id $method_discovery_struct->{id} as described by discovery document version $api_discovery_struct->{discoveryVersion} revision $api_discovery_struct->{revision} with documentation at $api_discovery_struct->{documentationLink} \nDescription: $method_discovery_struct->{description}\n} | ||||||||||||||||||||||||||||
| 237 | if @teapot_errors; | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | 10 | return @teapot_errors; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | ################################################## | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | #small subs to convert between these_types to theseTypes of params | ||||||||||||||||||||||||||||
| 244 | #TODO- should probs move this into a Util module | ||||||||||||||||||||||||||||
| 245 | 43 | ▶ | sub camel { shift if @_ > 1; $_[0] =~ s/ _(\w) /\u$1/grx } | ||||||||||||||||||||||||||
|
Subroutine
camel: called CC=2 SCAR=7.2
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 246 | 0 | ▶ | sub snake { shift if @_ > 1; $_[0] =~ s/([[:upper:]])/_\l$1/grx } | ||||||||||||||||||||||||||
|
Subroutine
snake: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | ################################################## | ||||||||||||||||||||||||||||
| 249 | 10 | ▶ | sub _interpolate_path_parameters_append_query_params_and_return_errors { | ||||||||||||||||||||||||||
|
Subroutine
_interpolate_path_parameters_append_query_params_and_return_errors: called CC=18 SCAR=29.0
| |||||||||||||||||||||||||||||
| 250 | 10 | my ($self, $params, $discovery_struct) = @_; | |||||||||||||||||||||||||||
| 251 | 10 | my @teapot_errors = (); | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 10 | my @get_query_params = (); | |||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | #create a hash of whatever the expected params may be | ||||||||||||||||||||||||||||
| 256 | 10 | my %path_params; | |||||||||||||||||||||||||||
| 257 | 10 | my $param_regex = qr/\{ \+? ([^\}]+) \}/x; | |||||||||||||||||||||||||||
| 258 | 10 | ▶ | if ($params->{path} ne $discovery_struct->{origPath}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | #check if the path was given as a custom path. If it is, just | ||||||||||||||||||||||||||||
| 261 | #interpolate things directly, and assume the user is responsible | ||||||||||||||||||||||||||||
| 262 | 3 | %path_params = map { $_ => 'custom' } ($params->{path} =~ /$param_regex/xg); | |||||||||||||||||||||||||||
| 263 | } else { | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | #label which param names are from the normal path and from the | ||||||||||||||||||||||||||||
| 266 | #flat path | ||||||||||||||||||||||||||||
| 267 | 7 | %path_params = map { $_ => 'plain' } ($discovery_struct->{path} =~ /$param_regex/xg); | |||||||||||||||||||||||||||
| 268 | 7 | ▶ | if ($discovery_struct->{flatPath}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 7 | %path_params = (%path_params, map { $_ => 'flat' } ($discovery_struct->{flatPath} =~ /$param_regex/xg)); | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | #switch the path we're dealing with to the flat path if any of | ||||||||||||||||||||||||||||
| 275 | #the parameters match the flat path | ||||||||||||||||||||||||||||
| 276 | $params->{path} = $discovery_struct->{flatPath} | ||||||||||||||||||||||||||||
| 277 | 9 | if grep { $_ eq 'flat' } | |||||||||||||||||||||||||||
| 278 | 10 | ▶ | map { $path_params{ camel $_} || () } keys %{ $params->{options} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$path_params{camel($_)} || ()
MC/DC0%0/2
$path_params{camel($_)} || ()
$path_params{camel($_)}
()
Truth table66%2/3
A || B
A= $path_params{camel($_)}B= ()
| |||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | #loop through params given, placing them in the path or query, | ||||||||||||||||||||||||||||
| 282 | #or leaving them for the request body | ||||||||||||||||||||||||||||
| 283 | 10 | for my $param_name (keys %{ $params->{options} }) { | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | #first check if it needs to be interpolated into the path | ||||||||||||||||||||||||||||
| 286 | 17 | ▶ | if ($path_params{$param_name} || $path_params{ camel $param_name}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$path_params{$param_name} or $path_params{camel($param_name)}
MC/DC50%1/2
$path_params{$param_name} or $path_params{camel($param_name)}
$path_params{$param_name}
$path_params{camel($param_name)}
Truth table66%2/3
A or B
A= $path_params{$param_name}B= $path_params{camel($param_name)}
| |||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | #pull out the value from the hash, and remove the key | ||||||||||||||||||||||||||||
| 289 | 9 | my $param_value = delete $params->{options}{$param_name}; | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | #Camelize the param name if not passed in customly, allowing | ||||||||||||||||||||||||||||
| 292 | #the user to pass in camelCase or snake_case param names. | ||||||||||||||||||||||||||||
| 293 | #This implictly allows for a non camelCased param to be left | ||||||||||||||||||||||||||||
| 294 | #alone in a custom param. | ||||||||||||||||||||||||||||
| 295 | 9 | ▶ | $param_name = camel $param_name if $path_params{ camel $param_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | #first deal with any param that doesn't have a plus, b/c | ||||||||||||||||||||||||||||
| 298 | #those just get interpolated | ||||||||||||||||||||||||||||
| 299 | 9 | $params->{path} =~ s/\{$param_name\}/$param_value/; | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | #if there's a plus in the path spec, we need more work | ||||||||||||||||||||||||||||
| 302 | 9 | ▶ | if ($params->{path} =~ /\{ \+ $param_name \}/x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | 4 | my $pattern = $discovery_struct->{parameters}{$param_name}{pattern}; | |||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | #if the given param matches google's pattern for the | ||||||||||||||||||||||||||||
| 306 | #param, just interpolate it straight | ||||||||||||||||||||||||||||
| 307 | 4 | ▶ | if ($param_value =~ /$pattern/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 1 | $params->{path} =~ s/\{\+$param_name\}/$param_value/; | |||||||||||||||||||||||||||
| 309 | } else { | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | #N.B. perhaps support passing an arrayref or csv for those | ||||||||||||||||||||||||||||
| 312 | #params such as jobs.projects.jobs.delete which have two | ||||||||||||||||||||||||||||
| 313 | #dynamic parts. But for now, unnecessary | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | #remove the regexy parts of the pattern to interpolate it | ||||||||||||||||||||||||||||
| 316 | #into the path, assuming the user has provided just the | ||||||||||||||||||||||||||||
| 317 | #dynamic portion of the param. | ||||||||||||||||||||||||||||
| 318 | 3 | $pattern =~ s/^\^ | \$$//gx; | |||||||||||||||||||||||||||
| 319 | 3 | my $placeholder = qr/ \[ \^ \/ \] \+ /x; | |||||||||||||||||||||||||||
| 320 | 3 | $params->{path} =~ s/\{\+$param_name\}/$pattern/x; | |||||||||||||||||||||||||||
| 321 | 3 | $params->{path} =~ s/$placeholder/$param_value/x; | |||||||||||||||||||||||||||
| 322 | push @teapot_errors, "Not enough parameters given for {+$param_name}." | ||||||||||||||||||||||||||||
| 323 | 3 | ▶ | if $params->{path} =~ /$placeholder/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | #skip to the next run, so I don't need an else clause later | ||||||||||||||||||||||||||||
| 328 | 9 | next; #I don't like nested if-elses | |||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | #if it's not in the list of params, then it goes in the | ||||||||||||||||||||||||||||
| 332 | #request body, and our work here is done | ||||||||||||||||||||||||||||
| 333 | 8 | ▶ | next unless $discovery_struct->{parameters}{$param_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | #it must be a GET type query param, so push the name and value | ||||||||||||||||||||||||||||
| 336 | #on our param stack and take it off of the options list | ||||||||||||||||||||||||||||
| 337 | 3 | push @get_query_params, $param_name, delete $params->{options}{$param_name}; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | #if there are any query params... | ||||||||||||||||||||||||||||
| 341 | 10 | ▶ | if (@get_query_params) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | #interpolate and escape the get query params built up in our | ||||||||||||||||||||||||||||
| 344 | #former for loop | ||||||||||||||||||||||||||||
| 345 | 3 | ▶ | $params->{path} .= ($params->{path} =~ /\?/) ? '&' : '?'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | 3 | $params->{path} .= Mojo::Parameters->new(@get_query_params); | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | #interpolate default value for path params if not given. Needed | ||||||||||||||||||||||||||||
| 350 | #for things like the gmail API, where userID is 'me' by default | ||||||||||||||||||||||||||||
| 351 | 10 | for my $param_name ($params->{path} =~ /$param_regex/g) { | |||||||||||||||||||||||||||
| 352 | 2 | my $param_value = $discovery_struct->{parameters}{$param_name}{default}; | |||||||||||||||||||||||||||
| 353 | 2 | ▶ | $params->{path} =~ s/\{$param_name\}/$param_value/ if $param_value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | 10 | push @teapot_errors, "Missing a parameter for {$_}." for $params->{path} =~ /$param_regex/g; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | #print pp $params; | ||||||||||||||||||||||||||||
| 358 | #exit; | ||||||||||||||||||||||||||||
| 359 | 10 | return @teapot_errors; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | ################################################## | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 0 | ▶ | sub has_scope_to_access_api_endpoint { | ||||||||||||||||||||||||||
|
Subroutine
has_scope_to_access_api_endpoint: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 365 | 0 | my ($self, $api_ep) = @_; | |||||||||||||||||||||||||||
| 366 | 0 | my $method_spec = $self->get_method_details($api_ep); | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 0 | ▶ | if (keys %$method_spec) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | 0 | my $configured_scopes = $self->scopes; ## get user scopes arrayref | |||||||||||||||||||||||||||
| 370 | ## create a hashindex to facilitate quick lookups | ||||||||||||||||||||||||||||
| 371 | 0 | my %configured_scopes_hash = map { (s/\/$//xr, 1) } @$configured_scopes; | |||||||||||||||||||||||||||
| 372 | ## NB r switch as per https://www.perlmonks.org/?node_id=613280 to filter | ||||||||||||||||||||||||||||
| 373 | #out any trailing '/' | ||||||||||||||||||||||||||||
| 374 | 0 | my $granted = 0; | |||||||||||||||||||||||||||
| 375 | ## assume permission not granted until we find a matching scope | ||||||||||||||||||||||||||||
| 376 | 0 | my $required_scope_count = 0; | |||||||||||||||||||||||||||
| 377 | ## if the final count of scope constraints = 0 then we will assume permission is granted - this has proven necessary for the experimental Google My Business because scopes are not defined in the current discovery data as at 14/10/18 | ||||||||||||||||||||||||||||
| 378 | 0 | for my $method_scope (map {s/\/$//xr} @{ $method_spec->{scopes} }) { | |||||||||||||||||||||||||||
| 379 | 0 | $required_scope_count++; | |||||||||||||||||||||||||||
| 380 | 0 | ▶ | $granted = 1 if defined $configured_scopes_hash{$method_scope}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 381 | 0 | ▶ | last if $granted; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | 0 | ▶ | $granted = 1 if $required_scope_count == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 0 | return $granted; | |||||||||||||||||||||||||||
| 385 | } else { | ||||||||||||||||||||||||||||
| 386 | 0 | return 0; | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | #TODO: Consider rename to return_fetched_google_v1_apis_discovery_structure | ||||||||||||||||||||||||||||
| 393 | # | ||||||||||||||||||||||||||||
| 394 | #TODO - handle auth required error and resubmit request with OAUTH headers if response indicates | ||||||||||||||||||||||||||||
| 395 | # access requires auth ( when exceed free access limits ) | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | 2 | 1; | |||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | __END__ | ||||||||||||||||||||||||||||