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