line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Google::API::Client; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
207996
|
use strict; |
|
3
|
|
|
|
|
28
|
|
|
3
|
|
|
|
|
88
|
|
4
|
3
|
|
|
3
|
|
90
|
use 5.008_001; |
|
3
|
|
|
|
|
10
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.15'; |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
1338
|
use Google::API::Method; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
122
|
|
8
|
3
|
|
|
3
|
|
1402
|
use Google::API::Resource; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
104
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
19
|
use constant AUTH_URI => 'https://accounts.google.com/o/oauth2/auth'; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
211
|
|
12
|
3
|
|
|
3
|
|
18
|
use constant TOKEN_URI => 'https://accounts.google.com/o/oauth2/token'; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
2919
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
2
|
|
|
2
|
1
|
222
|
my $class = shift; |
16
|
2
|
|
|
|
|
6
|
my ($param) = @_; |
17
|
2
|
50
|
|
|
|
11
|
unless (defined $param->{ua}) { |
18
|
2
|
|
|
|
|
10
|
$param->{ua} = $class->_new_ua; |
19
|
|
|
|
|
|
|
} |
20
|
2
|
50
|
|
|
|
11
|
unless (defined $param->{json_parser}) { |
21
|
2
|
|
|
|
|
18
|
$param->{json_parser} = $class->_new_json_parser; |
22
|
|
|
|
|
|
|
} |
23
|
2
|
|
|
|
|
21
|
bless { %$param }, $class; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub build { |
27
|
17
|
|
|
17
|
1
|
32241
|
my $self = shift; |
28
|
17
|
|
|
|
|
62
|
my ($service, $version, $args) = @_; |
29
|
|
|
|
|
|
|
|
30
|
17
|
|
|
|
|
29
|
my $discovery_service_url; |
31
|
17
|
100
|
|
|
|
69
|
if ($args->{discovery_service_url}) { |
32
|
1
|
|
|
|
|
3
|
$discovery_service_url = $args->{discovery_service_url}; |
33
|
|
|
|
|
|
|
} else { |
34
|
16
|
|
|
|
|
45
|
$discovery_service_url = 'https://{api}.googleapis.com/$discovery/rest'; |
35
|
16
|
100
|
|
|
|
57
|
if ($version) { |
36
|
15
|
|
|
|
|
35
|
$discovery_service_url .= '?version={apiVersion}'; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
16
|
|
|
|
|
58
|
$service = $self->_replace_to_subdomain($service); |
40
|
|
|
|
|
|
|
|
41
|
16
|
100
|
|
|
|
66
|
if ($self->_is_v1_discovery_url($service, $version)) { |
42
|
6
|
|
|
|
|
17
|
$discovery_service_url = 'https://www.googleapis.com/discovery/v1/apis/{api}/{apiVersion}/rest'; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
} |
45
|
17
|
|
|
|
|
102
|
$discovery_service_url =~ s/{api}/$service/; |
46
|
17
|
|
|
|
|
70
|
$discovery_service_url =~ s/{apiVersion}/$version/; |
47
|
|
|
|
|
|
|
|
48
|
17
|
|
|
|
|
165
|
my $req = HTTP::Request->new(GET => $discovery_service_url); |
49
|
17
|
|
|
|
|
19823
|
my $res = $self->{ua}->request($req); |
50
|
17
|
|
|
|
|
6754836
|
$self->{ua}{response} = $res; |
51
|
17
|
50
|
|
|
|
119
|
unless ($res->is_success) { |
52
|
|
|
|
|
|
|
# throw an error |
53
|
0
|
|
|
|
|
0
|
die 'could not get service document.' . $res->status_line; |
54
|
|
|
|
|
|
|
} |
55
|
17
|
|
|
|
|
357
|
my $document = $self->{json_parser}->decode($res->content); |
56
|
17
|
|
|
|
|
157422
|
$self->build_from_document($document, $discovery_service_url, $args); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub build_from_document { |
60
|
17
|
|
|
17
|
1
|
67
|
my $self = shift; |
61
|
17
|
|
|
|
|
77
|
my ($document, $url, $args) = @_; |
62
|
17
|
|
|
|
|
94
|
my $base = $document->{rootUrl}.$document->{servicePath}; |
63
|
17
|
|
|
|
|
252
|
my $base_url = URI->new($base); |
64
|
17
|
|
|
|
|
2420
|
my $resource = $self->_create_resource($document, $base_url, $args); |
65
|
17
|
|
|
|
|
1099
|
return $resource; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _create_resource { |
69
|
47
|
|
|
47
|
|
91
|
my $self = shift; |
70
|
47
|
|
|
|
|
101
|
my ($document, $base_url, $args) = @_; |
71
|
47
|
|
|
|
|
217
|
my $root_resource_obj = Google::API::Resource->new; |
72
|
47
|
|
|
|
|
91
|
for my $resource (keys %{$document->{resources}}) { |
|
47
|
|
|
|
|
337
|
|
73
|
430
|
|
|
|
|
637
|
my $resource_obj; |
74
|
430
|
100
|
|
|
|
1131
|
if ($document->{resources}{$resource}{resources}) { |
75
|
30
|
|
|
|
|
91
|
$resource_obj = $self->_create_resource($document->{resources}{$resource}, $base_url, $args); |
76
|
|
|
|
|
|
|
} |
77
|
430
|
100
|
|
|
|
900
|
if ($document->{resources}{$resource}{methods}) { |
78
|
424
|
100
|
|
|
|
749
|
unless ($resource_obj) { |
79
|
400
|
|
|
|
|
771
|
$resource_obj = Google::API::Resource->new; |
80
|
|
|
|
|
|
|
} |
81
|
424
|
|
|
|
|
589
|
for my $method (keys %{$document->{resources}{$resource}{methods}}) { |
|
424
|
|
|
|
|
1862
|
|
82
|
|
|
|
|
|
|
$resource_obj->set_attr($method, sub { |
83
|
0
|
|
|
0
|
|
0
|
my (%param) = @_; |
84
|
|
|
|
|
|
|
return Google::API::Method->new( |
85
|
|
|
|
|
|
|
ua => $self->{ua}, |
86
|
|
|
|
|
|
|
json_parser => $self->{json_parser}, |
87
|
|
|
|
|
|
|
base_url => $base_url, |
88
|
0
|
|
|
|
|
0
|
doc => $document->{resources}{$resource}{methods}{$method}, |
89
|
|
|
|
|
|
|
opt => \%param, |
90
|
|
|
|
|
|
|
); |
91
|
2728
|
|
|
|
|
10231
|
}); |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
430
|
|
|
0
|
|
1487
|
$root_resource_obj->set_attr($resource, sub { $resource_obj } ); |
|
0
|
|
|
|
|
0
|
|
95
|
|
|
|
|
|
|
} |
96
|
47
|
100
|
|
|
|
135
|
if ($document->{auth}) { |
97
|
17
|
|
|
|
|
51
|
$root_resource_obj->{auth_doc} = $document->{auth}; |
98
|
|
|
|
|
|
|
} |
99
|
47
|
|
|
|
|
93
|
return $root_resource_obj; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub _new_ua { |
103
|
3
|
|
|
3
|
|
625
|
my $class = shift; |
104
|
3
|
|
|
|
|
1408
|
require LWP::UserAgent; |
105
|
3
|
|
|
|
|
53380
|
my $ua = LWP::UserAgent->new; |
106
|
3
|
|
|
|
|
6366
|
return $ua; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _new_json_parser { |
110
|
3
|
|
|
3
|
|
10
|
my $class = shift; |
111
|
3
|
|
|
|
|
1439
|
require JSON; |
112
|
3
|
|
|
|
|
17040
|
my $parser = JSON->new; |
113
|
3
|
|
|
|
|
19
|
return $parser; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub _replace_to_subdomain { |
117
|
16
|
|
|
16
|
|
41
|
my ($self, $service) = @_; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Following services are different from subdomains. |
120
|
|
|
|
|
|
|
# It needs to be converted. |
121
|
16
|
|
|
|
|
99
|
my %replacement = ( |
122
|
|
|
|
|
|
|
'adexchangebuyer2' => 'adexchangebuyer', |
123
|
|
|
|
|
|
|
'calendar' => 'calendar-json', |
124
|
|
|
|
|
|
|
'content' => 'shoppingcontent', |
125
|
|
|
|
|
|
|
'prod_tt_sasportal' => 'prod-tt-sasportal', |
126
|
|
|
|
|
|
|
'translate' => 'translation', |
127
|
|
|
|
|
|
|
); |
128
|
16
|
100
|
|
|
|
61
|
if (grep { $service eq $_ } keys %replacement) { |
|
80
|
|
|
|
|
185
|
|
129
|
7
|
|
|
|
|
20
|
$service = $replacement{$service}; |
130
|
|
|
|
|
|
|
} |
131
|
16
|
|
|
|
|
66
|
return $service; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
sub _is_v1_discovery_url { |
135
|
16
|
|
|
16
|
|
53
|
my ($self, $service, $version) = @_; |
136
|
|
|
|
|
|
|
# Following services are still using V1 type URL |
137
|
16
|
100
|
100
|
|
|
296
|
if (($service eq 'compute' && $version eq 'alpha') || |
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
100
|
|
|
|
|
138
|
|
|
|
|
|
|
($service eq 'compute' && $version eq 'beta') || |
139
|
|
|
|
|
|
|
($service eq 'compute' && $version eq 'v1') || |
140
|
|
|
|
|
|
|
($service eq 'drive' && $version eq 'v2') || |
141
|
|
|
|
|
|
|
($service eq 'drive' && $version eq 'v3') || |
142
|
|
|
|
|
|
|
($service eq 'oauth2' && $version eq 'v2')) { |
143
|
6
|
|
|
|
|
25
|
return 1; |
144
|
|
|
|
|
|
|
} |
145
|
10
|
|
|
|
|
28
|
return; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
1; |
149
|
|
|
|
|
|
|
__END__ |