| 1 | 3 | ▶ | use strictures; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package WebService::GoogleAPI::Client::Discovery; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | our $VERSION = '0.25'; # VERSION | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # ABSTRACT: Google API discovery service | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use WebService::GoogleAPI::Client::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use List::Util qw/uniq reduce/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use List::SomeUtils qw/pairwise/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Data::Dump qw/pp/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use CHI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | has ua => ( | ||||||||||||||||||||||||||||
| 19 | is => 'rw', | ||||||||||||||||||||||||||||
| 20 | 2 | ▶ | default => sub { WebService::GoogleAPI::Client::UserAgent->new } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 3 | ); | |||||||||||||||||||||||||||
| 22 | 3 | has debug => (is => 'rw', default => 0); | |||||||||||||||||||||||||||
| 23 | has 'chi' => ( | ||||||||||||||||||||||||||||
| 24 | is => 'rw', | ||||||||||||||||||||||||||||
| 25 | 1 | ▶ | default => sub { CHI->new(driver => 'File', namespace => __PACKAGE__) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 3 | lazy => 1 | |||||||||||||||||||||||||||
| 27 | ); ## i believe that this gives priority to param if provided ? | ||||||||||||||||||||||||||||
| 28 | has 'stats' => is => 'rw', | ||||||||||||||||||||||||||||
| 29 | 4 | ▶ | default => sub { { network => { get => 0 }, cache => { get => 0 } } }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | sub get_with_cache { | ||||||||||||||||||||||||||||
| 33 | 32 | ▶ | my ($self, $key, $force, $authorized) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_with_cache: called CC=10 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 32 | ▶ | my $expiration = $self->chi->get_expires_at($key) // 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->chi->get_expires_at($key) // 0
MC/DC100%1/1
$self->chi->get_expires_at($key) // 0
$self->chi->get_expires_at($key)
Truth table100%2/2
A
A= $self->chi->get_expires_at($key)
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 32 | my $will_expire = $expiration - time(); | |||||||||||||||||||||||||||
| 38 | 32 | ▶ | if ($will_expire > 0 && not $force) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$will_expire > 0 and not $force
MC/DC100%2/2
$will_expire > 0 and not $force
$will_expire > 0
not $force
Truth table100%3/3
A and not(B)
A= $will_expire > 0B= not $force
| |||||||||||||||||||||||||||||
| 39 | 24 | ▶ | carp "discovery_data cached data expires in $will_expire seconds" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | if $self->debug > 2; | ||||||||||||||||||||||||||||
| 41 | 24 | my $ret = $self->chi->get($key); | |||||||||||||||||||||||||||
| 42 | 24 | ▶ | croak 'was expecting a HASHREF!' unless ref $ret eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 24 | $self->stats->{cache}{get}++; | |||||||||||||||||||||||||||
| 44 | 24 | return $ret; | |||||||||||||||||||||||||||
| 45 | } else { | ||||||||||||||||||||||||||||
| 46 | 8 | my $ret; | |||||||||||||||||||||||||||
| 47 | 8 | ▶ | if ($authorized) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 1 | $ret = $self->ua->validated_api_query($key); | |||||||||||||||||||||||||||
| 49 | 1 | $self->stats->{network}{authorized}++; | |||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 7 | $ret = $self->ua->get($key)->res; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 8 | ▶ | if ($ret->is_success) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 8 | my $all = $ret->json; | |||||||||||||||||||||||||||
| 55 | 8 | $self->stats->{network}{get}++; | |||||||||||||||||||||||||||
| 56 | 8 | $self->chi->set($key, $all, '30d'); | |||||||||||||||||||||||||||
| 57 | 8 | return $all; | |||||||||||||||||||||||||||
| 58 | } else { | ||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if ($ret->code == 403 && !$authorized) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$ret->code == 403 and not $authorized
MC/DC0%0/2
$ret->code == 403 and not $authorized
$ret->code == 403
not $authorized
Truth table0%0/3
A and not(B)
A= $ret->code == 403B= not $authorized
| |||||||||||||||||||||||||||||
| 60 | 0 | return $self->get_with_cache($key, $force, 1); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 0 | croak $ret->message; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 0 | return {}; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 12 | ▶ | sub discover_key { 'https://www.googleapis.com/discovery/v1/apis' } | ||||||||||||||||||||||||||
|
Subroutine
discover_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub discover_all { | ||||||||||||||||||||||||||||
| 72 | 10 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
discover_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 10 | $self->get_with_cache($self->discover_key, @_); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | #TODO- double triple check that we didn't break anything with the | ||||||||||||||||||||||||||||
| 77 | #hashref change | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | #TODO- maybe cache this on disk too? | ||||||||||||||||||||||||||||
| 81 | 3 | my $available; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub _invalidate_available { | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | $available = undef; | ||||||||||||||||||||||||||
|
Subroutine
_invalidate_available: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | sub available_APIs { | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | #cache this crunch | ||||||||||||||||||||||||||||
| 90 | 100 | ▶ | return $available if $available; | ||||||||||||||||||||||||||
|
Subroutine
available_APIs: called CC=5 SCAR=16.1
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 93 | 3 | my $d_all = $self->discover_all; | |||||||||||||||||||||||||||
| 94 | 3 | ▶ | croak 'no items in discovery data' unless defined $d_all->{items}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | #grab only entries with the four keys we want | ||||||||||||||||||||||||||||
| 97 | #and strip other keys | ||||||||||||||||||||||||||||
| 98 | 3 | my @keys = qw/name version documentationLink discoveryRestUrl/; | |||||||||||||||||||||||||||
| 99 | 3 | my @relevant; | |||||||||||||||||||||||||||
| 100 | 3 | for my $i (@{ $d_all->{items} }) { | |||||||||||||||||||||||||||
| 101 | 1570 | ▶ | next unless @keys == grep { exists $i->{$_} } @keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 1567 | push @relevant, { %{$i}{@keys} }; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | my $reduced = reduce { | ||||||||||||||||||||||||||||
| 106 | 1567 | ▶ | for my $key (qw/version documentationLink discoveryRestUrl/) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 107 | 4701 | ▶ | $a->{ $b->{name} }->{$key} //= []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$$a{$WebService::GoogleAPI::Client::Discovery::b->{"name"}}{$key} //= []
MC/DC100%1/1
$$a{$WebService::GoogleAPI::Client::Discovery::b->{"name"}}{$key} //= []
$$a{$WebService::GoogleAPI::Client::Discovery::b->{"name"}}{$key}
Truth table100%2/2
A
A= $$a{$WebService::GoogleAPI::Client::Discovery::b->{"name"}}{$key}
| |||||||||||||||||||||||||||||
| 108 | 4701 | push @{ $a->{ $b->{name} }->{$key} }, $b->{$key}; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 1567 | $a; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 3 | {}, @relevant; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | #store it away globally | ||||||||||||||||||||||||||||
| 115 | 3 | $available = $reduced; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub augment_discover_all_with_unlisted_experimental_api { | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | my ($self, $api_spec) = @_; | ||||||||||||||||||||||||||
|
Subroutine
augment_discover_all_with_unlisted_experimental_api: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | 0 | carp <<DEPRECATION; | |||||||||||||||||||||||||||
| 122 | This lengthy function name (augment_discover_all_with_unlisted_experimental_api) | ||||||||||||||||||||||||||||
| 123 | will be removed soon. Please use 'augment_with' instead. | ||||||||||||||||||||||||||||
| 124 | DEPRECATION | ||||||||||||||||||||||||||||
| 125 | 0 | $self->augment_with($api_spec); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub augment_with { | ||||||||||||||||||||||||||||
| 129 | 1 | ▶ | my ($self, $api_spec) = @_; | ||||||||||||||||||||||||||
|
Subroutine
augment_with: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 1 | my $all = $self->discover_all(); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | ## fail if any of the expected fields are not provided | ||||||||||||||||||||||||||||
| 134 | 1 | for my $field ( | |||||||||||||||||||||||||||
| 135 | qw/version title description id kind documentationLink | ||||||||||||||||||||||||||||
| 136 | discoveryRestUrl name/ | ||||||||||||||||||||||||||||
| 137 | ) { | ||||||||||||||||||||||||||||
| 138 | 8 | ▶ | if (not defined $api_spec->{$field}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | carp("required $field in provided api spec missing"); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 1 | push @{ $all->{items} }, $api_spec; | |||||||||||||||||||||||||||
| 144 | 1 | $self->chi->set($self->discover_key, $all, '30d'); | |||||||||||||||||||||||||||
| 145 | 1 | $self->_invalidate_available; | |||||||||||||||||||||||||||
| 146 | 1 | return $all; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub service_exists { | ||||||||||||||||||||||||||||
| 152 | 43 | ▶ | my ($self, $api) = @_; | ||||||||||||||||||||||||||
|
Subroutine
service_exists: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | 43 | ▶ | return unless $api; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 43 | return $self->available_APIs->{$api}; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | sub available_versions { | ||||||||||||||||||||||||||||
| 159 | 28 | ▶ | my ($self, $api) = @_; | ||||||||||||||||||||||||||
|
Subroutine
available_versions: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 160 | 28 | ▶ | return [] unless $api; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | 28 | ▶ | return $self->available_APIs->{$api}->{version} // []; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->available_APIs->{$api}{"version"} // []
MC/DC100%1/1
$self->available_APIs->{$api}{"version"} // []
$self->available_APIs->{$api}{"version"}
Truth table100%2/2
A
A= $self->available_APIs->{$api}{"version"}
| |||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | sub latest_stable_version { | ||||||||||||||||||||||||||||
| 166 | 23 | ▶ | my ($self, $api) = @_; | ||||||||||||||||||||||||||
|
Subroutine
latest_stable_version: called CC=5 SCAR=16.7
Pod
documented
| |||||||||||||||||||||||||||||
| 167 | 23 | ▶ | return '' unless $api; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 23 | my $versions = $self->available_versions($api); | |||||||||||||||||||||||||||
| 169 | 23 | ▶ | return '' unless $versions; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 23 | ▶ | return '' unless @{$versions} > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | #remove alpha or beta versions | ||||||||||||||||||||||||||||
| 173 | 23 | my @stable = grep { !/beta|alpha/ } @$versions; | |||||||||||||||||||||||||||
| 174 | 23 | ▶ | return $stable[-1] || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$stable[-1] || ''
MC/DC0%0/1
$stable[-1] || ''
$stable[-1]
Truth table50%1/2
A
A= $stable[-1]
| |||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | sub process_api_version { | ||||||||||||||||||||||||||||
| 179 | 28 | ▶ | my ($self, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process_api_version: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # scalar parameter not hashref - so assume is intended to be $params->{api} | ||||||||||||||||||||||||||||
| 182 | 28 | ▶ | $params = { api => $params } if ref $params eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 28 | ▶ | croak "'api' must be defined" unless $params->{api}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | ## trim any resource, method or version details in api id | ||||||||||||||||||||||||||||
| 187 | 28 | ▶ | if ($params->{api} =~ /([^:]+):(v[^\.]+)/ixsm) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 3 | $params->{api} = $1; | |||||||||||||||||||||||||||
| 189 | 3 | $params->{version} = $2; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | 28 | ▶ | if ($params->{api} =~ /^(.*?)\./xsm) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 10 | $params->{api} = $1; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 28 | ▶ | unless ($self->service_exists($params->{api})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 2 | croak "$params->{api} does not seem to be a valid Google API"; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 26 | ▶ | $params->{version} //= $self->latest_stable_version($params->{api}); | ||||||||||||||||||||||||||
|
Condition66%2/3
$params->{"version"} //= $self->latest_stable_version($params->{"api"})
MC/DC0%0/2
$params->{"version"} //= $self->latest_stable_version($params->{"api"})
$params->{"version"}
$self->latest_stable_version($params->{"api"})
Truth table66%2/3
A //= B
A= $params->{"version"}B= $self->latest_stable_version($params->{"api"})
| |||||||||||||||||||||||||||||
| 200 | 26 | return $params; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | sub get_api_discovery_for_api_id { | ||||||||||||||||||||||||||||
| 206 | 0 | ▶ | carp <<DEPRECATION; | ||||||||||||||||||||||||||
|
Subroutine
get_api_discovery_for_api_id: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 207 | This long method name (get_api_discovery_for_api_id) is being deprecated | ||||||||||||||||||||||||||||
| 208 | in favor of get_api_document. Please switch your code soon | ||||||||||||||||||||||||||||
| 209 | DEPRECATION | ||||||||||||||||||||||||||||
| 210 | 0 | shift->get_api_document(@_); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | sub get_api_document { | ||||||||||||||||||||||||||||
| 214 | 23 | ▶ | my ($self, $arg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_api_document: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 23 | my $params = $self->process_api_version($arg); | |||||||||||||||||||||||||||
| 217 | 22 | my $apis = $self->available_APIs(); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 22 | my $api = $apis->{ $params->{api} }; | |||||||||||||||||||||||||||
| 220 | 22 | ▶ | croak "No versions found for $params->{api}" unless $api->{version}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 22 | my @versions = @{ $api->{version} }; | |||||||||||||||||||||||||||
| 223 | 22 | my @urls = @{ $api->{discoveryRestUrl} }; | |||||||||||||||||||||||||||
| 224 | 22 | ▶ | my ($url) = pairwise { $a eq $params->{version} ? $b : () } @versions, @urls; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 22 | ▶ | croak "Couldn't find correct url for $params->{api} $params->{version}" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | unless $url; | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 22 | $self->get_with_cache($url); | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | #TODO- HERE - we are here in refactoring | ||||||||||||||||||||||||||||
| 233 | sub _extract_resource_methods_from_api_spec { | ||||||||||||||||||||||||||||
| 234 | 152 | ▶ | my ($self, $tree, $api_spec, $ret) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_extract_resource_methods_from_api_spec: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||
| 235 | 152 | ▶ | $ret = {} unless defined $ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 152 | ▶ | croak("ret not a hash - $tree, $api_spec, $ret") unless ref($ret) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 152 | ▶ | if (defined $api_spec->{methods} && ref($api_spec->{methods}) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $api_spec->{"methods"} and ref $api_spec->{"methods"} eq "HASH"
MC/DC50%1/2
defined $api_spec->{"methods"} and ref $api_spec->{"methods"} eq "HASH"
defined $api_spec->{"methods"}
ref $api_spec->{"methods"} eq "HASH"
Truth table66%2/3
A and B
A= defined $api_spec->{"methods"}B= ref $api_spec->{"methods"} eq "HASH"
| |||||||||||||||||||||||||||||
| 239 | 116 | foreach my $method (keys %{ $api_spec->{methods} }) { | |||||||||||||||||||||||||||
| 240 | $ret->{"$tree.$method"} = $api_spec->{methods}{$method} | ||||||||||||||||||||||||||||
| 241 | 530 | ▶ | if ref($api_spec->{methods}{$method}) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | 152 | ▶ | if (defined $api_spec->{resources}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | 54 | foreach my $resource (keys %{ $api_spec->{resources} }) { | |||||||||||||||||||||||||||
| 246 | ## NB - recursive traversal down tree of api_spec resources | ||||||||||||||||||||||||||||
| 247 | $self->_extract_resource_methods_from_api_spec("$tree.$resource", | ||||||||||||||||||||||||||||
| 248 | 132 | $api_spec->{resources}{$resource}, $ret); | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | 152 | return $ret; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | sub extract_method_discovery_detail_from_api_spec { | ||||||||||||||||||||||||||||
| 257 | 0 | ▶ | carp <<DEPRECATION; | ||||||||||||||||||||||||||
|
Subroutine
extract_method_discovery_detail_from_api_spec: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 258 | This rather long method name (extract_method_discovery_detail_from_api_spec) | ||||||||||||||||||||||||||||
| 259 | is being deprecated in favor of get_method_details. Please switch soon | ||||||||||||||||||||||||||||
| 260 | DEPRECATION | ||||||||||||||||||||||||||||
| 261 | 0 | shift->get_method_details(@_); | |||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | sub get_method_details { | ||||||||||||||||||||||||||||
| 265 | 11 | ▶ | my ($self, $tree, $api_version) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_method_details: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||
| 266 | ## where tree is the method in format from _extract_resource_methods_from_api_spec() like projects.models.versions.get | ||||||||||||||||||||||||||||
| 267 | ## the root is the api id - further '.' sep levels represent resources until the tailing label that represents the method | ||||||||||||||||||||||||||||
| 268 | 11 | ▶ | croak 'You must ask for a method!' unless defined $tree; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 11 | my @nodes = split /\./smx, $tree; | |||||||||||||||||||||||||||
| 271 | 11 | ▶ | croak( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | "tree structure '$tree' must contain at least 2 nodes including api id, [list of hierarchical resources ] and method - not " | ||||||||||||||||||||||||||||
| 273 | . scalar(@nodes)) | ||||||||||||||||||||||||||||
| 274 | unless @nodes > 1; | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 11 | my $api_id = shift(@nodes); ## api was head | |||||||||||||||||||||||||||
| 277 | 11 | my $method = pop(@nodes); ## method was tail | |||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | ## split out version if is defined as part of $tree | ||||||||||||||||||||||||||||
| 280 | ## trim any resource, method or version details in api id | ||||||||||||||||||||||||||||
| 281 | ## we have already isolated head from api tree children | ||||||||||||||||||||||||||||
| 282 | 11 | ▶ | if ($api_id =~ /([^:]+):([^\.]+)$/ixsm) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 2 | $api_id = $1; | |||||||||||||||||||||||||||
| 284 | 2 | $api_version = $2; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | ## handle incorrect api_id | ||||||||||||||||||||||||||||
| 288 | 11 | ▶ | if ($self->service_exists($api_id) == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 0 | croak("unable to confirm that '$api_id' is a valid Google API service id"); | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||
| 292 | 11 | ▶ | $api_version = $self->latest_stable_version($api_id) unless $api_version; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | ## TODO: confirm that spec available for api version | ||||||||||||||||||||||||||||
| 296 | 11 | my $api_spec = | |||||||||||||||||||||||||||
| 297 | $self->get_api_document({ api => $api_id, version => $api_version }); | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | ## we use the schemas to substitute into '$ref' keyed placeholders | ||||||||||||||||||||||||||||
| 301 | 11 | my $schemas = {}; | |||||||||||||||||||||||||||
| 302 | 11 | foreach my $schema_key (sort keys %{ $api_spec->{schemas} }) { | |||||||||||||||||||||||||||
| 303 | 978 | $schemas->{$schema_key} = $api_spec->{'schemas'}{$schema_key}; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | ## recursive walk through the structure in _fix_ref | ||||||||||||||||||||||||||||
| 307 | ## substitute the schema keys into the total spec to include | ||||||||||||||||||||||||||||
| 308 | ## '$ref' values within the schema structures themselves | ||||||||||||||||||||||||||||
| 309 | ## including within the schema spec structures (NB assumes no cyclic structures ) | ||||||||||||||||||||||||||||
| 310 | ## otherwise would could recursive chaos | ||||||||||||||||||||||||||||
| 311 | 11 | my $api_spec_fix = $self->_fix_ref($api_spec, $schemas) | |||||||||||||||||||||||||||
| 312 | ; ## first level ( '$ref' in the method params and return values etc ) | ||||||||||||||||||||||||||||
| 313 | 11 | $api_spec = $self->_fix_ref($api_spec_fix, $schemas) | |||||||||||||||||||||||||||
| 314 | ; ## second level ( '$ref' in the interpolated schemas from first level ) | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | ## now extract all the methods (recursive ) | ||||||||||||||||||||||||||||
| 317 | 11 | my $all_api_methods = | |||||||||||||||||||||||||||
| 318 | $self->_extract_resource_methods_from_api_spec("$api_id:$api_version", | ||||||||||||||||||||||||||||
| 319 | $api_spec); | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 11 | ▶ | unless (defined $all_api_methods->{$tree}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 9 | $all_api_methods = | |||||||||||||||||||||||||||
| 323 | $self->_extract_resource_methods_from_api_spec($api_id, $api_spec); | ||||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | 11 | ▶ | if ($all_api_methods->{$tree}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | #add in the global parameters to the endpoint, | ||||||||||||||||||||||||||||
| 328 | #stored in the top level of the api_spec | ||||||||||||||||||||||||||||
| 329 | # TODO - why are we mutating the main hash? | ||||||||||||||||||||||||||||
| 330 | $all_api_methods->{$tree}{parameters} = { | ||||||||||||||||||||||||||||
| 331 | 10 | %{ $all_api_methods->{$tree}{parameters} }, | |||||||||||||||||||||||||||
| 332 | 10 | %{ $api_spec->{parameters} } | |||||||||||||||||||||||||||
| 333 | }; | ||||||||||||||||||||||||||||
| 334 | 10 | return $all_api_methods->{$tree}; | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | croak( | ||||||||||||||||||||||||||||
| 338 | 1 | "Unable to find method detail for '$tree' within Google Discovery Spec for $api_id version $api_version" | |||||||||||||||||||||||||||
| 339 | ); | ||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | ######################################################## | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | ######################################################## | ||||||||||||||||||||||||||||
| 344 | ######################################################## | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | #=head2 C<fix_ref> | ||||||||||||||||||||||||||||
| 347 | # | ||||||||||||||||||||||||||||
| 348 | #This sub walks through the structure and replaces any hashes keyed with '$ref' with | ||||||||||||||||||||||||||||
| 349 | #the value defined in $schemas->{ <value of keyed $ref> } | ||||||||||||||||||||||||||||
| 350 | # | ||||||||||||||||||||||||||||
| 351 | #eg | ||||||||||||||||||||||||||||
| 352 | # ->{'response'}{'$ref'}{'Buckets'} | ||||||||||||||||||||||||||||
| 353 | # is replaced with | ||||||||||||||||||||||||||||
| 354 | # ->{response}{ $schemas->{Buckets} } | ||||||||||||||||||||||||||||
| 355 | # | ||||||||||||||||||||||||||||
| 356 | # It assumes that the schemas have been extracted from the original discover for the API | ||||||||||||||||||||||||||||
| 357 | # and is typically applied to the method ( api endpoint ) to provide a fully descriptive | ||||||||||||||||||||||||||||
| 358 | # structure without external references. | ||||||||||||||||||||||||||||
| 359 | # | ||||||||||||||||||||||||||||
| 360 | #=cut | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | ######################################################## | ||||||||||||||||||||||||||||
| 363 | sub _fix_ref { | ||||||||||||||||||||||||||||
| 364 | 136333 | ▶ | my ($self, $node, $schemas) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_fix_ref: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 365 | 136333 | my $ret = undef; | |||||||||||||||||||||||||||
| 366 | 136333 | my $r = ref($node); | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 136333 | ▶ | if ($r eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 370 | 5584 | $ret = []; | |||||||||||||||||||||||||||
| 371 | 5584 | foreach my $el (@$node) { | |||||||||||||||||||||||||||
| 372 | 33674 | push @$ret, $self->_fix_ref($el, $schemas); | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | } elsif ($r eq 'HASH') { | ||||||||||||||||||||||||||||
| 375 | 33489 | $ret = {}; | |||||||||||||||||||||||||||
| 376 | 33489 | foreach my $key (keys %$node) { | |||||||||||||||||||||||||||
| 377 | 107837 | ▶ | if ($key eq '$ref') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | #say $node->{'$ref'}; | ||||||||||||||||||||||||||||
| 380 | 5200 | $ret = $schemas->{ $node->{'$ref'} }; | |||||||||||||||||||||||||||
| 381 | } else { | ||||||||||||||||||||||||||||
| 382 | 102637 | $ret->{$key} = $self->_fix_ref($node->{$key}, $schemas); | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | } else { | ||||||||||||||||||||||||||||
| 386 | 97260 | $ret = $node; | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 136333 | return $ret; | |||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||
| 391 | ######################################################## | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | #TODO: consider ? refactor to allow parameters either as a single api id such as 'gmail' | ||||||||||||||||||||||||||||
| 396 | # as well as the currently accepted hash keyed on the api and version | ||||||||||||||||||||||||||||
| 397 | # | ||||||||||||||||||||||||||||
| 398 | #SEE ALSO: | ||||||||||||||||||||||||||||
| 399 | # The following methods are delegated through to Client::Discovery - see perldoc WebService::Client::Discovery for detils | ||||||||||||||||||||||||||||
| 400 | # | ||||||||||||||||||||||||||||
| 401 | # get_method_meta | ||||||||||||||||||||||||||||
| 402 | # discover_all | ||||||||||||||||||||||||||||
| 403 | # extract_method_discovery_detail_from_api_spec | ||||||||||||||||||||||||||||
| 404 | # get_api_discovery_for_api_id | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | ######################################################## | ||||||||||||||||||||||||||||
| 407 | #TODO- give short name and deprecate | ||||||||||||||||||||||||||||
| 408 | sub methods_available_for_google_api_id { | ||||||||||||||||||||||||||||
| 409 | 0 | ▶ | my ($self, $api_id, $version) = @_; | ||||||||||||||||||||||||||
|
Subroutine
methods_available_for_google_api_id: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 0 | ▶ | $version = $self->latest_stable_version($api_id) unless $version; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 412 | ## TODO: confirm that spec available for api version | ||||||||||||||||||||||||||||
| 413 | 0 | my $api_spec = $self->get_api_discovery_for_api_id( | |||||||||||||||||||||||||||
| 414 | { api => $api_id, version => $version }); | ||||||||||||||||||||||||||||
| 415 | 0 | my $methods = | |||||||||||||||||||||||||||
| 416 | $self->_extract_resource_methods_from_api_spec($api_id, $api_spec); | ||||||||||||||||||||||||||||
| 417 | 0 | return $methods; | |||||||||||||||||||||||||||
| 418 | } | ||||||||||||||||||||||||||||
| 419 | ######################################################## | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | sub list_of_available_google_api_ids { | ||||||||||||||||||||||||||||
| 424 | 0 | ▶ | carp <<DEPRECATION; | ||||||||||||||||||||||||||
|
Subroutine
list_of_available_google_api_ids: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 425 | This rather long function name (list_of_available_google_api_ids) | ||||||||||||||||||||||||||||
| 426 | is being deprecated in favor of the shorter list_api_ids. Please | ||||||||||||||||||||||||||||
| 427 | update your code accordingly. | ||||||||||||||||||||||||||||
| 428 | DEPRECATION | ||||||||||||||||||||||||||||
| 429 | 0 | shift->list_api_ids; | |||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||
| 431 | ## returns a list of all available API Services | ||||||||||||||||||||||||||||
| 432 | sub list_api_ids { | ||||||||||||||||||||||||||||
| 433 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
list_api_ids: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 434 | 6 | my @api_list = keys %{ $self->available_APIs }; | |||||||||||||||||||||||||||
| 435 | 6 | ▶ | return wantarray ? @api_list : join(',', @api_list); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 436 | } | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | 3 | 1; | |||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||
| 441 | __END__ | ||||||||||||||||||||||||||||