line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=encoding utf-8 |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
Yandex::OAuth::Client - base class for any Yandex.API modules |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Yandex::OAuth::Client; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $client = Yandex::OAuth::Client->new(token => '************', endpoint => 'https://api-metrika.yandex.ru/'); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
my $client->get(['stat','user_vars'], { |
14
|
|
|
|
|
|
|
pretty => 1, |
15
|
|
|
|
|
|
|
# other params specified for resource |
16
|
|
|
|
|
|
|
}); |
17
|
|
|
|
|
|
|
# url converting to https://api-metrika.yandex.ru/stat/user_vars/?pretty=1 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 DESCRIPTION |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
Yandex::OAuth::Client is base class for any Yandex.API modules |
22
|
|
|
|
|
|
|
Contains get, post, put, delete methods for queries to any APIs Yandex. |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Looking for contributors for this and other Yandex.APIs |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=cut |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
package Yandex::OAuth::Client; |
29
|
2
|
|
|
2
|
|
1678
|
use 5.008001; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
70
|
|
30
|
2
|
|
|
2
|
|
873
|
use utf8; |
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
11
|
|
31
|
2
|
|
|
2
|
|
735
|
use Modern::Perl; |
|
2
|
|
|
|
|
9087
|
|
|
2
|
|
|
|
|
11
|
|
32
|
2
|
|
|
2
|
|
1285
|
use Data::Dumper; |
|
2
|
|
|
|
|
4419
|
|
|
2
|
|
|
|
|
115
|
|
33
|
|
|
|
|
|
|
|
34
|
2
|
|
|
2
|
|
657
|
use LWP::UserAgent; |
|
2
|
|
|
|
|
14980
|
|
|
2
|
|
|
|
|
59
|
|
35
|
2
|
|
|
2
|
|
14
|
use Storable qw/ dclone /; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
218
|
|
36
|
2
|
|
|
2
|
|
9
|
use JSON::XS; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
77
|
|
37
|
2
|
|
|
2
|
|
664
|
use Moo; |
|
2
|
|
|
|
|
8983
|
|
|
2
|
|
|
|
|
12
|
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
has 'ua' => ( |
40
|
|
|
|
|
|
|
is => 'ro', |
41
|
|
|
|
|
|
|
default => sub { |
42
|
|
|
|
|
|
|
my $ua = LWP::UserAgent->new(); |
43
|
|
|
|
|
|
|
$ua->agent( 'Yandex::OAuth Perl API Client' ); |
44
|
|
|
|
|
|
|
$ua->timeout( 120 ); |
45
|
|
|
|
|
|
|
return $ua; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
); |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
has 'token' => ( |
50
|
|
|
|
|
|
|
is => 'rw', |
51
|
|
|
|
|
|
|
required => 1, |
52
|
|
|
|
|
|
|
); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has 'endpoint' => ( |
55
|
|
|
|
|
|
|
is => 'rw', |
56
|
|
|
|
|
|
|
required => 1, |
57
|
|
|
|
|
|
|
); |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
has 'next_url' => ( |
60
|
|
|
|
|
|
|
is => 'rw', |
61
|
|
|
|
|
|
|
default => '', |
62
|
|
|
|
|
|
|
writer => 'set_next_url', |
63
|
|
|
|
|
|
|
); |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
has 'demo' => ( |
66
|
|
|
|
|
|
|
is => 'rw' |
67
|
|
|
|
|
|
|
); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=head1 CONSTRUCTOR |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=over |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=item B |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Require two arguments: token and endpoint. For module based on Yandex::OAuth::Client |
76
|
|
|
|
|
|
|
you can override atribute 'endpoint' |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
extends 'Yandex::OAuth::Client'; |
79
|
|
|
|
|
|
|
has '+endpoint' => ( |
80
|
|
|
|
|
|
|
is => 'ro', |
81
|
|
|
|
|
|
|
default => 'https://api-metrika.yandex.ru/', |
82
|
|
|
|
|
|
|
); |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=back |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 METHODS |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=over |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item B |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Send GET request. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
$client->get(['stat','load']); |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# or full link. for example, when using "next" link from previous response |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$client->get('https://api-metrika.yandex.ru/stat/load/?pretty=1'); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=cut |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
sub get { |
103
|
0
|
|
|
0
|
1
|
0
|
my ($self, $url, $query) = @_; |
104
|
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
0
|
return $self->request( 'get', $url, $query ); |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=item B |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Send POST request. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
$client->post(['counters'], { |
113
|
|
|
|
|
|
|
# query params |
114
|
|
|
|
|
|
|
}, { |
115
|
|
|
|
|
|
|
# body params |
116
|
|
|
|
|
|
|
}); |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub post { |
121
|
0
|
|
|
0
|
1
|
0
|
my ($self, $url, $query, $body) = @_; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
0
|
return $self->request( 'post', $url, $query, $body ); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=item B |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
Send PUT request. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
$client->put(['counter', $counter_id], { |
131
|
|
|
|
|
|
|
# query params |
132
|
|
|
|
|
|
|
}, { |
133
|
|
|
|
|
|
|
# body params |
134
|
|
|
|
|
|
|
}); |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=cut |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
sub put { |
139
|
0
|
|
|
0
|
1
|
0
|
my ($self, $url, $query, $body) = @_; |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
0
|
return $self->request( 'put', $url, $query, $body ); |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=item B |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Send DELETE request. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
$client->delete(['counter', $counter_id]); |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=cut |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub delete { |
153
|
0
|
|
|
0
|
1
|
0
|
my ($self, $url, $query) = @_; |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
0
|
return $self->request( 'delete', $url, $query ); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub request { |
159
|
0
|
|
|
0
|
0
|
0
|
my ( $self, $method, $url, $query, $body ) = @_; |
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
0
|
my $req = HTTP::Request->new( uc $method => $self->make_url( $url, $query ) ); |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
0
|
$self->prepare_request( $req, $body ); |
164
|
|
|
|
|
|
|
|
165
|
0
|
|
|
|
|
0
|
my $resp = $self->parse_response( $self->ua->request( $req )->content ); |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
0
|
return $resp; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
sub make_url { |
171
|
4
|
|
|
4
|
0
|
18
|
my ( $self, $resource, $params ) = @_; |
172
|
|
|
|
|
|
|
|
173
|
4
|
100
|
|
|
|
68
|
my @resources = ref $resource ? @$resource : ( $resource ); |
174
|
|
|
|
|
|
|
|
175
|
4
|
|
|
|
|
3
|
my $url = ''; |
176
|
4
|
100
|
|
|
|
15
|
if ( $resource =~ /^http/i ) { |
177
|
1
|
|
|
|
|
4
|
$url = $resource; |
178
|
|
|
|
|
|
|
} |
179
|
|
|
|
|
|
|
else { |
180
|
3
|
|
|
|
|
22
|
$url = $self->endpoint; |
181
|
3
|
|
|
|
|
12
|
$url =~ s/[\\\/]+$//; |
182
|
3
|
|
|
|
|
4
|
$url .= '/'; |
183
|
3
|
|
|
|
|
8
|
$url .= join '/', @resources; |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
4
|
100
|
|
|
|
10
|
if ( $params ) { |
187
|
2
|
|
|
|
|
10
|
my $uri = URI->new( $url ); |
188
|
2
|
|
|
|
|
6702
|
$uri->query_form( %$params ); |
189
|
2
|
|
|
|
|
230
|
$url = $uri->as_string; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
4
|
|
|
|
|
47
|
return $url; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub prepare_request { |
196
|
2
|
|
|
2
|
0
|
887
|
my ( $self, $request, $body_param ) = @_; |
197
|
|
|
|
|
|
|
|
198
|
2
|
|
|
|
|
4
|
my $body = ''; |
199
|
2
|
100
|
|
|
|
6
|
if ( $body_param ) { |
200
|
1
|
|
|
|
|
106
|
my $params = dclone( $body_param ); |
201
|
|
|
|
|
|
|
|
202
|
1
|
|
|
|
|
22
|
state $json = JSON::XS->new->utf8; |
203
|
|
|
|
|
|
|
|
204
|
1
|
|
|
|
|
18
|
$body = $json->encode( $params ); |
205
|
|
|
|
|
|
|
|
206
|
1
|
|
|
|
|
10
|
$request->content( $body ); |
207
|
|
|
|
|
|
|
} |
208
|
2
|
|
|
|
|
40
|
$request->header( 'content-type' => 'application/json; charset=UTF-8' ); |
209
|
2
|
|
|
|
|
154
|
$request->header( 'content-length' => length( $body ) ); |
210
|
|
|
|
|
|
|
|
211
|
2
|
|
|
|
|
72
|
$request->header( 'Authorization' => 'Bearer ' . $self->token ); |
212
|
|
|
|
|
|
|
|
213
|
2
|
|
|
|
|
45
|
return 1; |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
sub parse_response { |
217
|
2
|
|
|
2
|
0
|
932
|
my ( $self, $response ) = @_; |
218
|
|
|
|
|
|
|
|
219
|
2
|
100
|
|
|
|
15
|
return unless $response; |
220
|
|
|
|
|
|
|
|
221
|
1
|
|
|
|
|
21
|
my $json = JSON::XS::decode_json( $response ); |
222
|
|
|
|
|
|
|
|
223
|
1
|
|
|
|
|
3
|
my $next = ''; |
224
|
|
|
|
|
|
|
|
225
|
1
|
50
|
33
|
|
|
7
|
if ( defined $json->{links} && defined $json->{links}->{next} ) { |
226
|
0
|
|
|
|
|
0
|
$next = $json->{links}->{next}; |
227
|
0
|
|
|
|
|
0
|
$next =~ s/^http:/https:/i; |
228
|
|
|
|
|
|
|
|
229
|
0
|
|
|
|
|
0
|
$self->set_next_url( $next ); |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
1
|
|
|
|
|
10
|
return $json; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
1; |
237
|
|
|
|
|
|
|
__END__ |