« lib/Net/Async/Spotify/API/Generated/Users.pm
↑ Summary
lib/Net/Async/Spotify/API/Generated/Markets.pm »
| 1 | package Net::Async::Spotify; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | our $VERSION = 0.002; | |||||||||||||||||||
| 7 | 1 | our $AUTHORITY = 'cpan:VNEALV'; # AUTHORITY | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | # ABSTRACT: Interaction with spotify.com API | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =encoding utf8 | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | C<Net::Async::Spotify> - Interaction with spotify.com API | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 SYNOPSIS | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | use IO::Async::Loop; | ||||||||||||||||||||
| 20 | use Future::AsyncAwait; | ||||||||||||||||||||
| 21 | use Net::Async::Spotify; | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | my $loop = IO::Async::Loop->new; | ||||||||||||||||||||
| 24 | my $spotify = Net::Async::Spotify->new( | ||||||||||||||||||||
| 25 | client_id => '5fe01282e44241328a84e7c5cc169165', | ||||||||||||||||||||
| 26 | client_secret => '6f12e202e44241328a84e7c5dd169125', | ||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||
| 28 | $loop->add($spotify); | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # Generate the needed Authorize hash. | ||||||||||||||||||||
| 31 | # Requesting permission for all available scopes, and without prompting user if already approved. | ||||||||||||||||||||
| 32 | my %authorize = $spotify->authorize(scope => ['scopes'], show_dialog => 'false'); | ||||||||||||||||||||
| 33 | my $auth_uri = $authorize{uri}; | ||||||||||||||||||||
| 34 | my $auth_state = $authorize{state}; | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | # Obtain Authorization code from callback request. | ||||||||||||||||||||
| 37 | my $auth_code = '...'; # from `code` path parameter. | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | # Get the needed Access Token | ||||||||||||||||||||
| 40 | await $spotify->obtain_token( | ||||||||||||||||||||
| 41 | code => $auth_code, | ||||||||||||||||||||
| 42 | auto_refresh => 1, | ||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | # You can now request any API call | ||||||||||||||||||||
| 46 | await $spotify->api->player->start_a_users_playback(); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | # Token will be auto refreshed before it's expiry. | ||||||||||||||||||||
| 49 | $loop->run; | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | =head1 DESCRIPTION | ||||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | C<Net::Async::Spotify> Provides an interface for interacting with L<"Spotify API"|https://developer.spotify.com/documentation/web-api> | ||||||||||||||||||||
| 54 | It does so while being an L<IO::Async::Notifier> instance, with a L<Net::Async::HTTP> child to reach Spotify API, running on an L<IO::Async::Loop>. | ||||||||||||||||||||
| 55 | Where all listed Spotify API calls and their response objects are auto-generated and defined here from documentation page. For easier maintainability. | ||||||||||||||||||||
| 56 | |||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 1 | ▶ | use parent qw(IO::Async::Notifier); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 62 | 1 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 63 | 1 | ▶ | use Log::Any qw($log); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 64 | 1 | ▶ | use Net::Async::HTTP; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 65 | 1 | ▶ | use IO::Async::Timer::Periodic; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 66 | 1 | ▶ | use URI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 67 | 1 | ▶ | use URI::QueryParam; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 68 | 1 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 69 | 1 | ▶ | use Math::Random::Secure qw(irand); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 70 | 1 | ▶ | use JSON::MaybeUTF8 qw(:v1); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 71 | 1 | ▶ | use curry; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 1 | ▶ | use Net::Async::Spotify::Scope qw(:all); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 74 | 1 | ▶ | use Net::Async::Spotify::API; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 75 | 1 | ▶ | use Net::Async::Spotify::Token; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head1 CONSTRUCTOR | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =head2 new | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | $spotify = Net::Async::Spotify->new( %args ) | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | Constructs a new C<Net::Async::Spotify> Object, in which is actually an L<IO::Async::Notifier> instance. | ||||||||||||||||||||
| 86 | Takes a number of named arguments at construction time, which can be grouped like so: | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =head3 App Params | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | More details about them can be found in L<"Spotify App Settings page"|https://developer.spotify.com/documentation/general/guides/app-settings/> | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =over 8 | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =item client_id => STRING | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | Spotify App Client ID. L</client_id> | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =item client_secret => STRING | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | Spotify App Client Secret. L</client_secret> | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | =item redirect_uri => STRING | ||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | Spotify App callback URI. L</redirect_uri> | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | =item base_uri => STRING | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | Spotify base_uri default is set to L<https://accounts.spotify.com/>. L</base_uri> | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =back | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =head3 Token Params | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | Used for L</token>. | ||||||||||||||||||||
| 115 | It's parameter can be passed here too, however not needed as they can be obtained. | ||||||||||||||||||||
| 116 | When not passed then they should be obtained by calling L</obtain_token> | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =over 8 | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | =item access_token => STRING | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | Spotify User's L<"Access Token"|https://datatracker.ietf.org/doc/html/rfc6749#section-1.4> | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | =item refresh_token => STRING | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | Spotify User's L<"Refresh Token"|https://datatracker.ietf.org/doc/html/rfc6749#section-1.5> | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | =item token_type => STRING | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | Spotify L<"Access Token type"|https://datatracker.ietf.org/doc/html/rfc6749#section-7.1> | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =back | ||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | =head3 API param | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | used for L</API>. It is used to create only selected Spotify APIs instead of all. | ||||||||||||||||||||
| 137 | When not passed it will load all available L<"Spotify APIs"|https://developer.spotify.com/documentation/web-api/reference/#reference-index> | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | =over 8 | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | =item apis => ArrayRef | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | a List of limited APIs to be loaded. Passed to L<Net::Async::Spotify::API> when being created. | ||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | =back | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | =cut | ||||||||||||||||||||
| 148 | |||||||||||||||||||||
| 149 | 0 | ▶ | sub configure_unknown { | ||||||||||||||||||
|
Subroutine
configure_unknown: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 150 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 151 | 0 | my $apis = delete $args{apis}; | |||||||||||||||||||
| 152 | 0 | $self->{api} = Net::Async::Spotify::API->new(spotify => $self, apis => $apis); | |||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | # get Token params, as they can be passed here too | ||||||||||||||||||||
| 155 | 0 | my $token = Net::Async::Spotify::Token->new(); | |||||||||||||||||||
| 156 | 0 | for my $k (grep exists $args{$_}, qw(access_token refresh_token token_type)) { | |||||||||||||||||||
| 157 | 0 | $token->$k($args{$k}); | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | 0 | $self->{token} = $token; | |||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 0 | for my $k (grep exists $args{$_}, qw(client_id client_secret redirect_uri base_uri)) { | |||||||||||||||||||
| 162 | 0 | $self->{$k} = $args{$k}; | |||||||||||||||||||
| 163 | } | ||||||||||||||||||||
| 164 | } | ||||||||||||||||||||
| 165 | |||||||||||||||||||||
| 166 | 0 | ▶ | sub _add_to_loop { | ||||||||||||||||||
|
Subroutine
_add_to_loop: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 167 | 0 | my $self = shift; | |||||||||||||||||||
| 168 | $self->add_child( | ||||||||||||||||||||
| 169 | 0 | $self->{http} = Net::Async::HTTP->new( | |||||||||||||||||||
| 170 | fail_on_error => 1, | ||||||||||||||||||||
| 171 | close_after_request => 0, | ||||||||||||||||||||
| 172 | max_connections_per_host => 2, | ||||||||||||||||||||
| 173 | pipeline => 1, | ||||||||||||||||||||
| 174 | max_in_flight => 4, | ||||||||||||||||||||
| 175 | decode_content => 1, | ||||||||||||||||||||
| 176 | stall_timeout => 15, | ||||||||||||||||||||
| 177 | user_agent => 'Mozilla/4.0 (perl; Net::Async::Spotify; VNEALV@cpan.org)', | ||||||||||||||||||||
| 178 | )); | ||||||||||||||||||||
| 179 | $self->add_child( | ||||||||||||||||||||
| 180 | $self->{token_timer} = IO::Async::Timer::Periodic->new( | ||||||||||||||||||||
| 181 | interval => $self->token->expires_in - 46, | ||||||||||||||||||||
| 182 | 0 | ▶ | on_tick => $self->$curry::weak(sub { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 183 | 0 | my $self = shift; | |||||||||||||||||||
| 184 | 0 | $self->obtain_token()->retain; | |||||||||||||||||||
| 185 | 0 | }), | |||||||||||||||||||
| 186 | ) | ||||||||||||||||||||
| 187 | ); | ||||||||||||||||||||
| 188 | } | ||||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | =head1 METHODS | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | =head2 token | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | L<Net::Async::Spotify::Token> Object, holding Spotify Token information. | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | 0 | ▶ | sub token { shift->{token} } | ||||||||||||||||||
|
Subroutine
token: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | =head2 API | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | Returns an instance of L<Net::Async::Spotify::API> which includes all needed Spotify API Classes as methods. | ||||||||||||||||||||
| 203 | To be used to access and call any loaded Spotify API | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | $spotify->api->->player->transfer_a_users_playback( | ||||||||||||||||||||
| 206 | device_ids => '...', | ||||||||||||||||||||
| 207 | play => 'true' | ||||||||||||||||||||
| 208 | )->get; | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | Note that the response from any API call, belongs to L<Net::Async::Spotify::Object> group class. | ||||||||||||||||||||
| 211 | For both, API calls and their response objects are being collected and auto generated from L<"Spotify doc page"|https://developer.spotify.com/documentation/web-api/reference>, check </"crawl-api-doc.pl">. | ||||||||||||||||||||
| 212 | |||||||||||||||||||||
| 213 | =cut | ||||||||||||||||||||
| 214 | |||||||||||||||||||||
| 215 | 0 | ▶ | sub api { shift->{api} } | ||||||||||||||||||
|
Subroutine
api: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 216 | |||||||||||||||||||||
| 217 | =head2 authorize | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | Returns an L<URI> object with it being the needed Spotify Authorization request along with the needed parameters set. | ||||||||||||||||||||
| 220 | also return random hexadecimal number as the state attached to this request. | ||||||||||||||||||||
| 221 | Pretty much the things needed for L<"Spotify Obtaining Authorization"|https://developer.spotify.com/documentation/general/guides/authorization-guide/#obtaining-authorization> | ||||||||||||||||||||
| 222 | Accepts limited named parameters | ||||||||||||||||||||
| 223 | |||||||||||||||||||||
| 224 | =over 4 | ||||||||||||||||||||
| 225 | |||||||||||||||||||||
| 226 | =item client_id | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | Spotify ClientID, set to class L</client_id> if not passed. | ||||||||||||||||||||
| 229 | |||||||||||||||||||||
| 230 | =item response_type | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | set as C<code> for default. Since Authorization Code Flow is used. | ||||||||||||||||||||
| 233 | |||||||||||||||||||||
| 234 | =item redirect_uri | ||||||||||||||||||||
| 235 | |||||||||||||||||||||
| 236 | URI string that will be used as Authorization callback URL. Set to main app L</redirect_url> if not peresnt. | ||||||||||||||||||||
| 237 | |||||||||||||||||||||
| 238 | =item state | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | Used as linking mechanism between this generated Authorize Request, and the incoming callback code response. | ||||||||||||||||||||
| 241 | will be set to a random hexadecimal number. For more info see L<"Cross-Site Request Forgery"|https://datatracker.ietf.org/doc/html/rfc6749#section-10.12> | ||||||||||||||||||||
| 242 | Optional and defaulted to a random 8 digit hexadecimal string, using L<Math::Random::Secure::irand> | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | =item scope | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | Sets permissions to be requested. Accepts array of scopes or scopes categories. | ||||||||||||||||||||
| 247 | e.g. scope => [app_remote_control', 'user-follow-read', 'spotify_connect'] | ||||||||||||||||||||
| 248 | for more info L<"Spotify Scopes"|https://developer.spotify.com/documentation/general/guides/scopes/>, also L<Net::Async::Spotify::Scope>. | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | =item show_dialog | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | optional param can be passed set to either true|false | ||||||||||||||||||||
| 253 | Whether or not to force the user to approve the app again if they've already done so. | ||||||||||||||||||||
| 254 | C<false> (default) from Spotify API itself. | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | =back | ||||||||||||||||||||
| 257 | |||||||||||||||||||||
| 258 | $spotify->authorize( | ||||||||||||||||||||
| 259 | scope => [ | ||||||||||||||||||||
| 260 | 'user-read-playback-state', | ||||||||||||||||||||
| 261 | 'user-read-currently-playing', | ||||||||||||||||||||
| 262 | 'playlists', | ||||||||||||||||||||
| 263 | ], | ||||||||||||||||||||
| 264 | ); | ||||||||||||||||||||
| 265 | |||||||||||||||||||||
| 266 | Returns a C<Hash> containing C<uri> as the Authorization URL needed, and C<state> as the value that is used in it L</state> | ||||||||||||||||||||
| 267 | |||||||||||||||||||||
| 268 | =cut | ||||||||||||||||||||
| 269 | |||||||||||||||||||||
| 270 | 0 | ▶ | sub authorize { | ||||||||||||||||||
|
Subroutine
authorize: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||
| 271 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 272 | 0 | my $allowed_params = join '|', qw(client_id response_type redirect_uri state scope show_dialog); | |||||||||||||||||||
| 273 | |||||||||||||||||||||
| 274 | 0 | ▶ | $args{client_id} //= $self->client_id; | ||||||||||||||||||
|
Condition0%0/3
$args{"client_id"} //= $self->client_id
MC/DC0%0/2
$args{"client_id"} //= $self->client_id
$args{"client_id"}
$self->client_id
Truth table0%0/3
A //= B
A= $args{"client_id"}B= $self->client_id
| |||||||||||||||||||||
| 275 | 0 | ▶ | $args{response_type} //= 'code'; | ||||||||||||||||||
|
Condition0%0/2
$args{"response_type"} //= "code"
MC/DC0%0/1
$args{"response_type"} //= "code"
$args{"response_type"}
Truth table0%0/2
A
A= $args{"response_type"}
| |||||||||||||||||||||
| 276 | 0 | ▶ | $args{redirect_uri} //= $self->redirect_uri->as_string; | ||||||||||||||||||
|
Condition0%0/3
$args{"redirect_uri"} //= $self->redirect_uri->as_string
MC/DC0%0/2
$args{"redirect_uri"} //= $self->redirect_uri->as_string
$args{"redirect_uri"}
$self->redirect_uri->as_string
Truth table0%0/3
A //= B
A= $args{"redirect_uri"}B= $self->redirect_uri->as_string
| |||||||||||||||||||||
| 277 | 0 | ▶ | $args{state} //= sprintf '%08x', irand(2**32); | ||||||||||||||||||
|
Condition0%0/2
$args{"state"} //= sprintf("%08x", &irand("4294967296"))
MC/DC0%0/1
$args{"state"} //= sprintf("%08x", &irand("4294967296"))
$args{"state"}
Truth table0%0/2
A
A= $args{"state"}
| |||||||||||||||||||||
| 278 | 0 | ▶ | $args{scope} = join ' ', map { (\&{join('::', 'Net::Async::Spotify::Scope', $_)})->() } $args{scope}->@* if exists $args{scope}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 279 | |||||||||||||||||||||
| 280 | # Don't leak any extra params | ||||||||||||||||||||
| 281 | 0 | delete $args{$_} for grep { !/$allowed_params/ } keys %args; | |||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | 0 | my $auth_uri = URI->new($self->base_uri . 'authorize'); | |||||||||||||||||||
| 284 | 0 | $auth_uri->query_param($_, $args{$_}) for keys %args; | |||||||||||||||||||
| 285 | 0 | return (uri => $auth_uri, state => $args{state}); | |||||||||||||||||||
| 286 | # request has to be viewed on browser and accepted. | ||||||||||||||||||||
| 287 | # | ||||||||||||||||||||
| 288 | # my $r = await $self->http->do_request(uri => $auth_uri, method => 'GET'); | ||||||||||||||||||||
| 289 | # $log->warnf('rr %s', $r->content); | ||||||||||||||||||||
| 290 | # Once accepted redirect_url will be called with authorization code passed. | ||||||||||||||||||||
| 291 | } | ||||||||||||||||||||
| 292 | |||||||||||||||||||||
| 293 | =head2 obtain_token | ||||||||||||||||||||
| 294 | |||||||||||||||||||||
| 295 | Method used to obtain access and refresh token from passed Authorization code. | ||||||||||||||||||||
| 296 | L<https://developer.spotify.com/documentation/general/guides/authorization-guide/> especifically Step 2 & 4 in Authorization Code Flow. | ||||||||||||||||||||
| 297 | Support for other methods can be easily added, however not needed at the moment. | ||||||||||||||||||||
| 298 | Accepts limited parameters, and based on them will decide whether to get new token from Authorization code | ||||||||||||||||||||
| 299 | or from a previously obtained refresh token. | ||||||||||||||||||||
| 300 | Note that it does not check for L</state> value as this step should be handled by caller. | ||||||||||||||||||||
| 301 | |||||||||||||||||||||
| 302 | =over 4 | ||||||||||||||||||||
| 303 | |||||||||||||||||||||
| 304 | =item code | ||||||||||||||||||||
| 305 | |||||||||||||||||||||
| 306 | representing Spotify Authorization Code, if passed, C<grant_type> parameter will be set to authorization_code. | ||||||||||||||||||||
| 307 | and the request will be for a new Spotify Token pair. | ||||||||||||||||||||
| 308 | |||||||||||||||||||||
| 309 | =item redirect_uri | ||||||||||||||||||||
| 310 | |||||||||||||||||||||
| 311 | optional, must be matching the one used to obtain code. Only used when L</code> parameter is present | ||||||||||||||||||||
| 312 | |||||||||||||||||||||
| 313 | =item auto_refresh | ||||||||||||||||||||
| 314 | |||||||||||||||||||||
| 315 | if set it will start C<IO::Async::Timer::Periodic> in order to refresh access token before it expires. | ||||||||||||||||||||
| 316 | Accessed from L</"Token Timer"> | ||||||||||||||||||||
| 317 | |||||||||||||||||||||
| 318 | =back | ||||||||||||||||||||
| 319 | |||||||||||||||||||||
| 320 | =cut | ||||||||||||||||||||
| 321 | |||||||||||||||||||||
| 322 | 0 | ▶ | async sub obtain_token { | ||||||||||||||||||
|
Subroutine
obtain_token: not called CC=6 SCAR=35.2
Pod
documented
| |||||||||||||||||||||
| 323 | 0 | my ($self, %args) = @_; | |||||||||||||||||||
| 324 | 0 | my $allowed_params = join '|', qw(client_id client_secret grant_type redirect_uri code refresh_token); | |||||||||||||||||||
| 325 | 0 | ▶ | my $auto_refresh = delete $args{auto_refresh} || 0; | ||||||||||||||||||
|
Condition0%0/2
delete $args{'auto_refresh'} || 0
MC/DC0%0/1
delete $args{'auto_refresh'} || 0
delete $args{'auto_refresh'}
Truth table0%0/2
A
A= delete $args{'auto_refresh'}
| |||||||||||||||||||||
| 326 | |||||||||||||||||||||
| 327 | 0 | ▶ | if ( exists $args{code}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 328 | 0 | $args{grant_type} = 'authorization_code'; | |||||||||||||||||||
| 329 | 0 | ▶ | $args{redirect_uri} //= $self->redirect_uri->as_string; | ||||||||||||||||||
|
Condition0%0/3
$args{"redirect_uri"} //= $self->redirect_uri->as_string
MC/DC0%0/2
$args{"redirect_uri"} //= $self->redirect_uri->as_string
$args{"redirect_uri"}
$self->redirect_uri->as_string
Truth table0%0/3
A //= B
A= $args{"redirect_uri"}B= $self->redirect_uri->as_string
| |||||||||||||||||||||
| 330 | } else { | ||||||||||||||||||||
| 331 | 0 | $args{grant_type} = 'refresh_token'; | |||||||||||||||||||
| 332 | 0 | $args{refresh_token} = $self->token->refresh_token; | |||||||||||||||||||
| 333 | } | ||||||||||||||||||||
| 334 | |||||||||||||||||||||
| 335 | # We can also pass client_id & client_secret in body param | ||||||||||||||||||||
| 336 | # Instead of base64 encoded Authorization header | ||||||||||||||||||||
| 337 | 0 | $args{client_id} = $self->client_id; | |||||||||||||||||||
| 338 | 0 | $args{client_secret} = $self->client_secret; | |||||||||||||||||||
| 339 | |||||||||||||||||||||
| 340 | # Don't leak any extra params | ||||||||||||||||||||
| 341 | 0 | delete $args{$_} for grep { !/$allowed_params/ } keys %args; | |||||||||||||||||||
| 342 | |||||||||||||||||||||
| 343 | 0 | my $result; | |||||||||||||||||||
| 344 | try { | ||||||||||||||||||||
| 345 | 0 | $result = await $self->http->do_request( | |||||||||||||||||||
| 346 | method => 'POST', | ||||||||||||||||||||
| 347 | uri => URI->new($self->base_uri . join '/', 'api', 'token'), | ||||||||||||||||||||
| 348 | content_type => 'application/x-www-form-urlencoded', | ||||||||||||||||||||
| 349 | content => \%args, | ||||||||||||||||||||
| 350 | headers => { | ||||||||||||||||||||
| 351 | Authorization => "Basic ". encode_base64(join(':', $self->client_id, $self->client_secret), ''), | ||||||||||||||||||||
| 352 | }, | ||||||||||||||||||||
| 353 | ); | ||||||||||||||||||||
| 354 | 0 | $result = decode_json_utf8($result->decoded_content); | |||||||||||||||||||
| 355 | 0 | } catch ($e) { | |||||||||||||||||||
| 356 | 1 | ▶ | use Data::Dumper; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 357 | 0 | $log->errorf('Error obtaining Access token | %s', Dumper($e)); | |||||||||||||||||||
| 358 | 0 | return; | |||||||||||||||||||
| 359 | } | ||||||||||||||||||||
| 360 | |||||||||||||||||||||
| 361 | 0 | $self->token->renew($result); | |||||||||||||||||||
| 362 | |||||||||||||||||||||
| 363 | 0 | $log->debugf('obtaining token response: %s', $self->token); | |||||||||||||||||||
| 364 | 0 | ▶ | if ($auto_refresh) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 365 | 0 | $log->infof('Enabling auto token refresh...'); | |||||||||||||||||||
| 366 | 0 | $self->token_timer->start; | |||||||||||||||||||
| 367 | } | ||||||||||||||||||||
| 368 | |||||||||||||||||||||
| 369 | 1 | } | |||||||||||||||||||
| 370 | |||||||||||||||||||||
| 371 | =head2 http | ||||||||||||||||||||
| 372 | |||||||||||||||||||||
| 373 | Accessor to underlying L<Net::Async::HTTP> object, which is used to perform requests. | ||||||||||||||||||||
| 374 | |||||||||||||||||||||
| 375 | =cut | ||||||||||||||||||||
| 376 | |||||||||||||||||||||
| 377 | 0 | ▶ | sub http { shift->{http} } | ||||||||||||||||||
|
Subroutine
http: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 378 | |||||||||||||||||||||
| 379 | =head2 token_timer | ||||||||||||||||||||
| 380 | |||||||||||||||||||||
| 381 | An instance of L<IO::Async::Timer::Periodic> which is set to be called before C<46> seconds | ||||||||||||||||||||
| 382 | of curret Token expiry time | ||||||||||||||||||||
| 383 | Can be started by L</auto_refresh> option | ||||||||||||||||||||
| 384 | |||||||||||||||||||||
| 385 | =cut | ||||||||||||||||||||
| 386 | |||||||||||||||||||||
| 387 | 0 | ▶ | sub token_timer { shift->{token_timer} } | ||||||||||||||||||
|
Subroutine
token_timer: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 388 | |||||||||||||||||||||
| 389 | =head2 client_id | ||||||||||||||||||||
| 390 | |||||||||||||||||||||
| 391 | Accessor for Spotify App Client ID | ||||||||||||||||||||
| 392 | |||||||||||||||||||||
| 393 | =cut | ||||||||||||||||||||
| 394 | |||||||||||||||||||||
| 395 | 0 | ▶ | sub client_id { shift->{client_id} } | ||||||||||||||||||
|
Subroutine
client_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 396 | |||||||||||||||||||||
| 397 | =head2 client_secret | ||||||||||||||||||||
| 398 | |||||||||||||||||||||
| 399 | Accessor for Spotify App Client Secret | ||||||||||||||||||||
| 400 | |||||||||||||||||||||
| 401 | =cut | ||||||||||||||||||||
| 402 | |||||||||||||||||||||
| 403 | 0 | ▶ | sub client_secret { shift->{client_secret} } | ||||||||||||||||||
|
Subroutine
client_secret: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 404 | |||||||||||||||||||||
| 405 | =head2 redirect_uri | ||||||||||||||||||||
| 406 | |||||||||||||||||||||
| 407 | Accessor for Spotify App defined redirect URL | ||||||||||||||||||||
| 408 | |||||||||||||||||||||
| 409 | =cut | ||||||||||||||||||||
| 410 | |||||||||||||||||||||
| 411 | 0 | ▶ | sub redirect_uri { shift->{redirect_uri} //= URI->new('http://localhost/callback') } | ||||||||||||||||||
|
Subroutine
redirect_uri: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/3
+(shift())->{"redirect_uri"} //= "URI"->new("http://localhost/callback")
MC/DC0%0/2
+(shift())->{"redirect_uri"} //= "URI"->new("http://localhost/callback")
+(shift())->{"redirect_uri"}
"URI"->new("http://localhost/callback")
Truth table0%0/3
A //= B
A= +(shift())->{"redirect_uri"}B= "URI"->new("http://localhost/callback")
| |||||||||||||||||||||
| 412 | |||||||||||||||||||||
| 413 | =head2 base_uri | ||||||||||||||||||||
| 414 | |||||||||||||||||||||
| 415 | Accessor for Spotify Base URI | ||||||||||||||||||||
| 416 | |||||||||||||||||||||
| 417 | =cut | ||||||||||||||||||||
| 418 | |||||||||||||||||||||
| 419 | 0 | ▶ | sub base_uri { shift->{base_uri} //= URI->new('https://accounts.spotify.com/') } | ||||||||||||||||||
|
Subroutine
base_uri: not called CC=2 SCAR=17.9
Pod
documented
Condition0%0/3
+(shift())->{"base_uri"} //= "URI"->new("https://accounts.spotify.com/")
MC/DC0%0/2
+(shift())->{"base_uri"} //= "URI"->new("https://accounts.spotify.com/")
+(shift())->{"base_uri"}
"URI"->new("https://accounts.spotify.com/")
Truth table0%0/3
A //= B
A= +(shift())->{"base_uri"}B= "URI"->new("https://accounts.spotify.com/")
| |||||||||||||||||||||
| 420 | |||||||||||||||||||||
| 421 | 1 | 1; | |||||||||||||||||||
| 422 | |||||||||||||||||||||
| 423 | =head1 spotify-cli.pl | ||||||||||||||||||||
| 424 | |||||||||||||||||||||
| 425 | Located at F<bin/spotify-cli.pl>, This will be installed with the package, where it gives us a CLI for Spotify API. | ||||||||||||||||||||
| 426 | Have some predefined commands, while it supports all API calls. Can run in various modes, one of them being C<interactive>. | ||||||||||||||||||||
| 427 | Currently it is just a simple CLI tool with minimal functionality. | ||||||||||||||||||||
| 428 | Serves as implementation example for this library. | ||||||||||||||||||||
| 429 | |||||||||||||||||||||
| 430 | # For full information | ||||||||||||||||||||
| 431 | spotify-cli.pl -h | ||||||||||||||||||||
| 432 | |||||||||||||||||||||
| 433 | Independent CLI library will be implemented using L<Tickit> | ||||||||||||||||||||
| 434 | |||||||||||||||||||||
| 435 | =head1 crawl-api-doc.pl | ||||||||||||||||||||
| 436 | |||||||||||||||||||||
| 437 | Located at F<scripts/spotify-cli.pl>, this script is mainly used for ease of implementation and maintability. | ||||||||||||||||||||
| 438 | It will parse L<"Spotify API Documentation page"|https://developer.spotify.com/documentation/web-api/reference> and utilizes a L<Template> | ||||||||||||||||||||
| 439 | in order to create corresponding Classes for every Spotify API type and call, along with Responce Objects. | ||||||||||||||||||||
| 440 | these 4 templates are what currently availabe and can be extended: | ||||||||||||||||||||
| 441 | |||||||||||||||||||||
| 442 | =over 4 | ||||||||||||||||||||
| 443 | |||||||||||||||||||||
| 444 | =item SpotifyAPI_main_pm.tt2 | ||||||||||||||||||||
| 445 | |||||||||||||||||||||
| 446 | Located at F<scripts/SpotifyAPI_main_pm.tt2> | Template for all L<Net::Async::Spotify::API::*> | ||||||||||||||||||||
| 447 | Main base class for custom functionality. One time generation. | ||||||||||||||||||||
| 448 | |||||||||||||||||||||
| 449 | =item SpotifyAPI_pm.tt2 | ||||||||||||||||||||
| 450 | |||||||||||||||||||||
| 451 | Located at F<scripts/SpotifyAPI_pm.tt2> | Template for all L<Net::Async::Spotify::API::Generated::*> | ||||||||||||||||||||
| 452 | Class for all available Spotify API calls defined as methods. Updated on Spotify API new releases. | ||||||||||||||||||||
| 453 | |||||||||||||||||||||
| 454 | =item SpotifyObj_main_pm.tt2 | ||||||||||||||||||||
| 455 | |||||||||||||||||||||
| 456 | Located at F<scripts/SpotifyObj_main_pm.tt2> | Template for all <Net::Async::Spotify::Object::*> | ||||||||||||||||||||
| 457 | Main base class for custom functionality. One time generation. | ||||||||||||||||||||
| 458 | |||||||||||||||||||||
| 459 | =item SpotifyObj_pm.tt2 | ||||||||||||||||||||
| 460 | |||||||||||||||||||||
| 461 | Located at F<scripts/SpotifyObj_pm.tt2> | Template for all <Net::Async::Spotify::Object::Generated::*> | ||||||||||||||||||||
| 462 | Class for all available Spotify Response Objects, where fields are set to be methods. Updated on Spotify API new releases. | ||||||||||||||||||||
| 463 | |||||||||||||||||||||
| 464 | =back | ||||||||||||||||||||
| 465 | |||||||||||||||||||||
| 466 | It accepts a couple of options to determines what you want to do. | ||||||||||||||||||||
| 467 | |||||||||||||||||||||
| 468 | # To generate all of the above. | ||||||||||||||||||||
| 469 | perl scripts/crawl-api-doc.pl -e -o -i | ||||||||||||||||||||
| 470 | |||||||||||||||||||||
| 471 | # Can be also used to explore API, combined with jq | ||||||||||||||||||||
| 472 | perl scripts/crawl-api-doc.pl -j | jq . | ||||||||||||||||||||
| 473 | |||||||||||||||||||||
| 474 | # For more details and optionns | ||||||||||||||||||||
| 475 | perl scripts/crawl-api-doc.pl -h | ||||||||||||||||||||
| 476 | |||||||||||||||||||||
| 477 | Note that when Spotify releases changes on their API, all what it takes to update this package is: | ||||||||||||||||||||
| 478 | |||||||||||||||||||||
| 479 | - Spotify to update their documentation page. | ||||||||||||||||||||
| 480 | - run this script and release changes. | ||||||||||||||||||||
| 481 | |||||||||||||||||||||
| 482 | =cut | ||||||||||||||||||||