| 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 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | use Modern::Perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 2 | ▶ | use Storable qw/ dclone /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | has 'ua' => ( | ||||||||||||||||||||||||||||
| 40 | is => 'ro', | ||||||||||||||||||||||||||||
| 41 | default => sub { | ||||||||||||||||||||||||||||
| 42 | 1 | ▶ | my $ua = LWP::UserAgent->new(); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 1 | $ua->agent( 'Yandex::OAuth Perl API Client' ); | |||||||||||||||||||||||||||
| 44 | 1 | $ua->timeout( 120 ); | |||||||||||||||||||||||||||
| 45 | 1 | return $ua; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | 2 | ); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | has 'token' => ( | |||||||||||||||||||||||||||
| 50 | is => 'rw', | ||||||||||||||||||||||||||||
| 51 | required => 1, | ||||||||||||||||||||||||||||
| 52 | ); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | has 'endpoint' => ( | |||||||||||||||||||||||||||
| 55 | is => 'rw', | ||||||||||||||||||||||||||||
| 56 | required => 1, | ||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 2 | has 'next_url' => ( | |||||||||||||||||||||||||||
| 60 | is => 'rw', | ||||||||||||||||||||||||||||
| 61 | default => '', | ||||||||||||||||||||||||||||
| 62 | writer => 'set_next_url', | ||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 2 | has 'demo' => ( | |||||||||||||||||||||||||||
| 66 | is => 'rw' | ||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =over | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =item B<new()> | ||||||||||||||||||||||||||||
| 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<get()> | ||||||||||||||||||||||||||||
| 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 | ▶ | my ($self, $url, $query) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | return $self->request( 'get', $url, $query ); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =item B<post()> | ||||||||||||||||||||||||||||
| 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 | ▶ | my ($self, $url, $query, $body) = @_; | ||||||||||||||||||||||||||
|
Subroutine
post: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | return $self->request( 'post', $url, $query, $body ); | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =item B<put()> | ||||||||||||||||||||||||||||
| 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 | ▶ | my ($self, $url, $query, $body) = @_; | ||||||||||||||||||||||||||
|
Subroutine
put: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | return $self->request( 'put', $url, $query, $body ); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =item B<delete()> | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Send DELETE request. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | $client->delete(['counter', $counter_id]); | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =cut | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub delete { | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | my ($self, $url, $query) = @_; | ||||||||||||||||||||||||||
|
Subroutine
delete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 0 | return $self->request( 'delete', $url, $query ); | |||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | sub request { | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | my ( $self, $method, $url, $query, $body ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
request: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 0 | my $req = HTTP::Request->new( uc $method => $self->make_url( $url, $query ) ); | |||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 0 | $self->prepare_request( $req, $body ); | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | my $resp = $self->parse_response( $self->ua->request( $req )->content ); | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | return $resp; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | sub make_url { | ||||||||||||||||||||||||||||
| 171 | 4 | ▶ | my ( $self, $resource, $params ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
make_url: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 4 | ▶ | my @resources = ref $resource ? @$resource : ( $resource ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 4 | my $url = ''; | |||||||||||||||||||||||||||
| 176 | 4 | ▶ | if ( $resource =~ /^http/i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | 1 | $url = $resource; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | else { | ||||||||||||||||||||||||||||
| 180 | 3 | $url = $self->endpoint; | |||||||||||||||||||||||||||
| 181 | 3 | $url =~ s/[\\\/]+$//; | |||||||||||||||||||||||||||
| 182 | 3 | $url .= '/'; | |||||||||||||||||||||||||||
| 183 | 3 | $url .= join '/', @resources; | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 4 | ▶ | if ( $params ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | 2 | my $uri = URI->new( $url ); | |||||||||||||||||||||||||||
| 188 | 2 | $uri->query_form( %$params ); | |||||||||||||||||||||||||||
| 189 | 2 | $url = $uri->as_string; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 4 | return $url; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | sub prepare_request { | ||||||||||||||||||||||||||||
| 196 | 2 | ▶ | my ( $self, $request, $body_param ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
prepare_request: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 2 | my $body = ''; | |||||||||||||||||||||||||||
| 199 | 2 | ▶ | if ( $body_param ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 1 | my $params = dclone( $body_param ); | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 1 | state $json = JSON::XS->new->utf8; | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 1 | $body = $json->encode( $params ); | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 1 | $request->content( $body ); | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | 2 | $request->header( 'content-type' => 'application/json; charset=UTF-8' ); | |||||||||||||||||||||||||||
| 209 | 2 | $request->header( 'content-length' => length( $body ) ); | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 2 | $request->header( 'Authorization' => 'Bearer ' . $self->token ); | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 2 | return 1; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | sub parse_response { | ||||||||||||||||||||||||||||
| 217 | 2 | ▶ | my ( $self, $response ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse_response: called CC=4 SCAR=15.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 2 | ▶ | return unless $response; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 1 | my $json = JSON::XS::decode_json( $response ); | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 1 | my $next = ''; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 1 | ▶ | if ( defined $json->{links} && defined $json->{links}->{next} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $json->{"links"} and defined $json->{"links"}{"next"}
MC/DC0%0/2
defined $json->{"links"} and defined $json->{"links"}{"next"}
defined $json->{"links"}
defined $json->{"links"}{"next"}
Truth table33%1/3
A and B
A= defined $json->{"links"}B= defined $json->{"links"}{"next"}
| |||||||||||||||||||||||||||||
| 226 | 0 | $next = $json->{links}->{next}; | |||||||||||||||||||||||||||
| 227 | 0 | $next =~ s/^http:/https:/i; | |||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 0 | $self->set_next_url( $next ); | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 1 | return $json; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | 2 | 1; | |||||||||||||||||||||||||||
| 237 | __END__ | ||||||||||||||||||||||||||||