blib/lib/WWW/Spotify.pm

filter: Statementstmt 62.1% 314 / 505 Branchbran 42.1% 97 / 230 Conditioncond 38.1% 29 / 76 MC/DCmcdc 23.4% 11 / 47 Subroutinesub 50.5% 44 / 87 Podpod 85.7% 60 / 70 totaltotal 54.6% 555 / 1015 CC 142 SCAR 22.8
CC
142
Cov
54%
send_get_request
23.3
next_result_set
20.0
previous_result_set
20.0
CRAP 9.7
↑ Summary
1 package WWW::Spotify;
2  
3 6 ▶use Moo 2.002004;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 our $VERSION = '0.016';
6  
7 6 ▶use Carp qw( carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Data::Dumper qw( Dumper );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use IO::CaptureOutput qw( capture );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use JSON::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use JSON::MaybeXS qw( decode_json encode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use MIME::Base64 qw( encode_base64 );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Types::Standard qw( Bool InstanceOf Int Str CodeRef );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use HTTP::Status qw( HTTP_OK is_success );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use URI::Escape qw( uri_escape );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 has 'oauth_authorize_url' => (
18 is => 'rw',
19 isa => Str,
20 default => 'https://accounts.spotify.com/authorize'
21 );
22  
23 6 has 'oauth_token_url' => (
24 is => 'rw',
25 isa => Str,
26 default => 'https://accounts.spotify.com/api/token'
27 );
28  
29 6 has 'oauth_redirect_uri' => (
30 is => 'rw',
31 isa => Str,
32 default => 'http://www.spotify.com'
33 );
34  
35 has 'oauth_client_id' => (
36 is => 'rw',
37 isa => Str,
38 6 ▶ default => $ENV{SPOTIFY_CLIENT_ID} || q{}
Condition50%1/2
$ENV{'SPOTIFY_CLIENT_ID'} || ''
l!l
06
MC/DC0%0/1
$ENV{'SPOTIFY_CLIENT_ID'} || ''
$ENV{'SPOTIFY_CLIENT_ID'}
Truth table50%1/2
A
A= $ENV{'SPOTIFY_CLIENT_ID'}
Aresult
11
00
39 );
40  
41 has 'oauth_client_secret' => (
42 is => 'rw',
43 isa => Str,
44 6 ▶ default => $ENV{SPOTIFY_CLIENT_SECRET} || q{}
Condition50%1/2
$ENV{'SPOTIFY_CLIENT_SECRET'} || ''
l!l
06
MC/DC0%0/1
$ENV{'SPOTIFY_CLIENT_SECRET'} || ''
$ENV{'SPOTIFY_CLIENT_SECRET'}
Truth table50%1/2
A
A= $ENV{'SPOTIFY_CLIENT_SECRET'}
Aresult
11
00
45 );
46  
47 6 has 'current_oath_code' => (
48 is => 'rw',
49 isa => Str,
50 default => q{}
51 );
52  
53 6 has 'current_access_token' => (
54 is => 'rw',
55 isa => Str,
56 default => q{}
57 );
58  
59 6 has 'refresh_token' => (
60 is => 'rw',
61 isa => Str,
62 default => q{}
63 );
64  
65 6 has 'result_format' => (
66 is => 'rw',
67 isa => Str,
68 default => 'json'
69 );
70  
71 6 has 'grab_response_header' => (
72 is => 'rw',
73 isa => Int,
74 default => 0
75 );
76  
77 6 has 'results' => (
78 is => 'rw',
79 isa => Int,
80 default => '15'
81 );
82  
83 6 has 'debug' => (
84 is => 'rw',
85 isa => Bool,
86 default => 0
87 );
88  
89 6 has 'uri_scheme' => (
90 is => 'rw',
91 isa => Str,
92 default => 'https'
93 );
94  
95 6 has 'current_client_credentials' => (
96 is => 'rw',
97 isa => Str,
98 default => q{}
99 );
100  
101 6 has 'force_client_auth' => (
102 is => 'rw',
103 isa => Bool,
104 default => 1
105 );
106  
107 6 has 'uri_hostname' => (
108 is => 'rw',
109 isa => Str,
110 default => 'api.spotify.com'
111 );
112  
113 6 has 'uri_domain_path' => (
114 is => 'rw',
115 isa => Str,
116 default => 'api'
117 );
118  
119 6 has 'call_type' => (
120 is => 'rw',
121 isa => Str
122 );
123  
124 6 has 'auto_json_decode' => (
125 is => 'rw',
126 isa => Int,
127 default => 0
128 );
129  
130 6 has 'last_result' => (
131 is => 'rw',
132 isa => Str,
133 default => q{}
134 );
135  
136 6 has 'last_error' => (
137 is => 'rw',
138 isa => Str,
139 default => q{}
140 );
141  
142 6 has 'response_headers' => (
143 is => 'rw',
144 isa => Str,
145 default => q{}
146 );
147  
148 6 has 'problem' => (
149 is => 'rw',
150 isa => Str,
151 default => q{}
152 );
153  
154 has 'ua' => (
155 is => 'ro',
156 isa => InstanceOf ['LWP::UserAgent'],
157 handles => { _mech => 'clone' },
158 lazy => 1,
159 default => sub {
160 0 ▶ require WWW::Mechanize;
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 0 WWW::Mechanize->new( autocheck => 0 );
162 },
163 6 );
164  
165 6 has 'response_status' => (
166 is => 'rw',
167 isa => Int
168 );
169  
170 6 has 'response_content_type' => (
171 is => 'rw',
172 isa => Str
173 );
174  
175 6 has 'custom_request_handler' => (
176 is => 'rw',
177 isa => CodeRef,
178 predicate => '_has_custom_request_handler',
179 );
180  
181 6 has 'custom_request_handler_result' => (
182 is => 'ro',
183 writer => '_set_custom_request_handler_result'
184 );
185  
186 6 has 'die_on_response_error' => (
187 is => 'rw',
188 isa => Bool,
189 default => 0
190 );
191  
192 6 has 'token_expires_at' => (
193 is => 'rw',
194 isa => Int,
195 default => 0
196 );
197  
198 6 my @api_call_options = (
199 {
200 path => '/v1/albums/{id}',
201 info => 'Get an album',
202 type => 'GET',
203 method => 'album'
204 },
205  
206 {
207 path => '/v1/audiobooks/{id}',
208 info => 'Get an audiobook',
209 type => 'GET',
210 method => 'get_audiobook',
211 params => ['market']
212 },
213  
214 {
215 path => '/v1/audiobooks',
216 info => 'Get several audiobooks',
217 type => 'GET',
218 method => 'get_several_audiobooks',
219 params => [ 'ids', 'market' ]
220 },
221  
222 {
223 path => '/v1/audiobooks/{id}/chapters',
224 info => 'Get Audiobook Chapters',
225 type => 'GET',
226 method => 'get_audiobook_chapters',
227 params => [ 'id', 'market', 'limit', 'offset' ]
228 },
229  
230 {
231 path => '/v1/me/audiobooks',
232 info => 'Get User\'s Saved Audiobooks',
233 type => 'GET',
234 method => 'get_users_saved_audiobooks',
235 params => [ 'limit', 'offset' ]
236 },
237  
238 {
239 path => '/v1/me/audiobooks',
240 info => 'Save Audiobooks for Current User',
241 type => 'PUT',
242 method => 'save_audiobooks_for_current_user',
243 params => ['ids']
244 },
245  
246 {
247 path => '/v1/me/audiobooks',
248 info => 'Remove User\'s Saved Audiobooks',
249 type => 'DELETE',
250 method => 'remove_users_saved_audiobooks',
251 params => ['ids']
252 },
253  
254 {
255 path => '/v1/me/audiobooks/contains',
256 info => 'Check User\'s Saved Audiobooks',
257 type => 'GET',
258 method => 'check_users_saved_audiobooks',
259 params => ['ids']
260 },
261  
262 {
263 path => '/v1/me/shows',
264 info => 'Get User\'s Saved Shows',
265 type => 'GET',
266 method => 'get_users_saved_shows',
267 params => [ 'limit', 'offset' ]
268 },
269  
270 {
271 path => '/v1/me/shows',
272 info => 'Save Shows for Current User',
273 type => 'PUT',
274 method => 'save_shows_for_current_user',
275 params => ['ids']
276 },
277  
278 {
279 path => '/v1/me/shows/contains',
280 info => 'Check User\'s Saved Shows',
281 type => 'GET',
282 method => 'check_users_saved_shows',
283 params => ['ids']
284 },
285  
286 {
287 path => '/v1/browse/categories',
288 info => 'Get Several Browse Categories',
289 type => 'GET',
290 method => 'get_categories',
291 params => [ 'country', 'locale', 'limit', 'offset' ]
292 },
293  
294 {
295 path => '/v1/browse/categories/{category_id}',
296 info => 'Get Single Browse Category',
297 type => 'GET',
298 method => 'get_category',
299 params => [ 'category_id', 'locale' ]
300 },
301  
302 {
303 path => '/v1/chapters/{id}',
304 info => 'Get a Chapter',
305 type => 'GET',
306 method => 'get_chapter',
307 params => [ 'id', 'market' ]
308 },
309  
310 {
311 path => '/v1/chapters',
312 info => 'Get Several Chapters',
313 type => 'GET',
314 method => 'get_several_chapters',
315 params => [ 'ids', 'market' ]
316 },
317  
318 {
319 path => '/v1/recommendations/available-genre-seeds',
320 info => 'Get Available Genre Seeds',
321 type => 'GET',
322 method => 'get_available_genre_seeds'
323 },
324  
325 {
326 path => '/v1/markets',
327 info => 'Get Available Markets',
328 type => 'GET',
329 method => 'get_available_markets'
330 },
331  
332 {
333 path => '/v1/shows/{id}',
334 info => 'Get a Show',
335 type => 'GET',
336 method => 'get_show',
337 params => ['market']
338 },
339  
340 {
341 path => '/v1/shows',
342 info => 'Get Several Shows',
343 type => 'GET',
344 method => 'get_several_shows',
345 params => [ 'ids', 'market' ]
346 },
347  
348 {
349 path => '/v1/shows/{id}/episodes',
350 info => 'Get Show Episodes',
351 type => 'GET',
352 method => 'get_show_episodes',
353 params => [ 'id', 'market', 'limit', 'offset' ]
354 },
355  
356 {
357 path => '/v1/albums?ids={ids}',
358 info => 'Get several albums',
359 type => 'GET',
360 method => 'albums',
361 params => [ 'limit', 'offset' ]
362 },
363  
364 {
365 path => '/v1/playlists/{playlist_id}',
366 info => 'Get a playlist',
367 type => 'GET',
368 method => 'get_playlist'
369 },
370  
371 {
372 path => '/v1/playlists/{playlist_id}/items',
373 info => 'Get playlist items',
374 type => 'GET',
375 method => 'get_playlist_items',
376 params => [ 'limit', 'offset', 'market', 'fields' ]
377 },
378  
379 {
380 path => '/v1/me/playlists',
381 info => 'Create a playlist for the current user',
382 type => 'POST',
383 method => 'create_playlist'
384 },
385  
386 {
387 path => '/v1/me/playlists',
388 info => 'Get current user\'s playlists',
389 type => 'GET',
390 method => 'get_current_user_playlists',
391 params => [ 'limit', 'offset' ]
392 },
393  
394 {
395 path => '/v1/playlists/{playlist_id}/items',
396 info => 'Add items to a playlist',
397 type => 'POST',
398 method => 'add_items_to_playlist'
399 },
400  
401 {
402 path => '/v1/playlists/{playlist_id}/followers',
403 info => 'Unfollow (remove) a playlist',
404 type => 'DELETE',
405 method => 'unfollow_playlist'
406 },
407  
408 {
409 path => '/v1/me/tracks',
410 info => 'Remove User\'s Saved Tracks',
411 type => 'DELETE',
412 method => 'remove_user_saved_tracks'
413 },
414  
415 {
416 path => '/v1/me/tracks/contains',
417 info => 'Check User\'s Saved Tracks',
418 type => 'GET',
419 method => 'check_users_saved_tracks'
420 },
421  
422 {
423 path => '/v1/audio-features',
424 info => 'Get Several Tracks\' Audio Features',
425 type => 'GET',
426 method => 'get_several_tracks_audio_features'
427 },
428  
429 {
430 path => '/v1/audio-features/{id}',
431 info => 'Get Track\'s Audio Features',
432 type => 'GET',
433 method => 'get_track_audio_features'
434 },
435  
436 {
437 path => '/v1/audio-analysis/{id}',
438 info => 'Get Track\'s Audio Analysis',
439 type => 'GET',
440 method => 'get_track_audio_analysis'
441 },
442  
443 {
444 path => '/v1/recommendations',
445 info => 'Get Recommendations',
446 type => 'GET',
447 method => 'get_recommendations',
448 params => [
449 'seed_artists', 'seed_genres', 'seed_tracks', 'limit', 'market'
450 ]
451 },
452  
453 {
454 path => '/v1/me/following',
455 info => 'Get Followed Artists',
456 type => 'GET',
457 method => 'get_followed_artists',
458 params => [ 'type', 'after', 'limit' ]
459 },
460  
461 {
462 path => '/v1/me/following',
463 info => 'Follow Artists or Users',
464 type => 'PUT',
465 method => 'follow_artists_or_users',
466 params => [ 'type', 'ids' ]
467 },
468  
469 {
470 path => '/v1/me/following',
471 info => 'Unfollow Artists or Users',
472 type => 'DELETE',
473 method => 'unfollow_artists_or_users',
474 params => [ 'type', 'ids' ]
475 },
476  
477 {
478 path => '/v1/me/following/contains',
479 info => 'Check if Current User Follows Artists or Users',
480 type => 'GET',
481 method => 'check_if_user_follows_artists_or_users',
482 params => [ 'type', 'ids' ]
483 },
484  
485 {
486 path => '/v1/playlists/{playlist_id}/followers/contains',
487 info => 'Check if Current User Follows Playlist',
488 type => 'GET',
489 method => 'check_if_user_follows_playlist',
490 params => [ 'playlist_id', 'ids' ]
491 },
492  
493 {
494 path => '/v1/albums/{id}/tracks',
495 info => q{Get an album's tracks},
496 type => 'GET',
497 method => 'albums_tracks'
498 },
499  
500 {
501 path => '/v1/artists/{id}',
502 info => 'Get an artist',
503 type => 'GET',
504 method => 'artist'
505 },
506  
507 {
508 path => '/v1/artists?ids={ids}',
509 info => 'Get several artists',
510 type => 'GET',
511 method => 'artists'
512 },
513  
514 {
515 path => '/v1/artists/{id}/albums',
516 info => q{Get an artist's albums},
517 type => 'GET',
518 method => 'artist_albums',
519 params => [ 'limit', 'offset', 'country', 'album_type' ]
520 },
521  
522 {
523 path => '/v1/artists/{id}/top-tracks?country={country}',
524 info => q{Get an artist's top tracks},
525 type => 'GET',
526 method => 'artist_top_tracks',
527 params => ['country']
528 },
529  
530 {
531 path => '/v1/artists/{id}/related-artists',
532 info => q{Get an artist's related artists},
533 type => 'GET',
534 method => 'artist_related_artists',
535 },
536  
537 # adding q and type to url unlike example since they are both required
538 {
539 path => '/v1/search?q={q}&type={type}',
540 info => 'Search for an item',
541 type => 'GET',
542 method => 'search',
543 params => [ 'limit', 'offset', 'q', 'type' ]
544 },
545  
546 {
547 path => '/v1/tracks/{id}',
548 info => 'Get a track',
549 type => 'GET',
550 method => 'track'
551 },
552  
553 {
554 path => '/v1/tracks?ids={ids}',
555 info => 'Get several tracks',
556 type => 'GET',
557 method => 'tracks'
558 },
559  
560 {
561 path => '/v1/users/{user_id}',
562 info => q{Get a user's profile},
563 type => 'GET',
564 method => 'user'
565 },
566  
567 {
568 path => '/v1/me',
569 info => q{Get current user's profile},
570 type => 'GET',
571 method => 'me'
572 },
573  
574 {
575 path => '/v1/users/{user_id}/playlists',
576 info => q{Get a list of a user's playlists},
577 type => 'GET',
578 method => 'user_playlist'
579 },
580  
581 {
582 path => '/v1/browse/featured-playlists',
583 info => 'Get a list of featured playlists',
584 type => 'GET',
585 method => 'browse_featured_playlists'
586 },
587  
588 {
589 path => '/v1/browse/new-releases',
590 info => 'Get a list of new releases',
591 type => 'GET',
592 method => 'browse_new_releases'
593 },
594  
595 # February 2026 consolidated library endpoints. These take Spotify
596 # URIs (spotify:track:{id}, spotify:show:{id}, ...) rather than bare
597 # ids, passed as a "uris" query parameter on every verb.
598 {
599 path => '/v1/me/library?uris={uris}',
600 info => 'Save Items to Library',
601 type => 'PUT',
602 method => 'save_library_items',
603 params => ['uris']
604 },
605  
606 {
607 path => '/v1/me/library?uris={uris}',
608 info => 'Remove Items from Library',
609 type => 'DELETE',
610 method => 'remove_library_items',
611 params => ['uris']
612 },
613  
614 {
615 path => '/v1/me/library/contains?uris={uris}',
616 info => 'Check Items in Library',
617 type => 'GET',
618 method => 'check_library_items',
619 params => ['uris']
620 },
621 );
622  
623 # Methods whose endpoints were removed or consolidated by Spotify's
624 # February 2026 API changes (plus the November 2024 deprecations). The
625 # methods are kept for backwards compatibility; calling one warns once
626 # per process and the request is still sent (Spotify will reject it).
627 6 my %method_deprecated = (
628 albums =>
629 'GET /v1/albums?ids= removed Feb 2026; fetch albums individually with album()',
630 artists =>
631 'GET /v1/artists?ids= removed Feb 2026; fetch artists individually with artist()',
632 tracks =>
633 'GET /v1/tracks?ids= removed Feb 2026; fetch tracks individually with track()',
634 get_several_shows =>
635 'GET /v1/shows removed Feb 2026; use get_show() per id',
636 get_several_audiobooks =>
637 'GET /v1/audiobooks removed Feb 2026; use get_audiobook() per id',
638 get_several_chapters =>
639 'GET /v1/chapters removed Feb 2026; use get_chapter() per id',
640 get_several_tracks_audio_features =>
641 'GET /v1/audio-features removed Feb 2026',
642 get_track_audio_features =>
643 'GET /v1/audio-features/{id} deprecated by Spotify (Nov 2024)',
644 get_track_audio_analysis =>
645 'GET /v1/audio-analysis/{id} deprecated by Spotify (Nov 2024)',
646 get_recommendations =>
647 'GET /v1/recommendations deprecated by Spotify (Nov 2024)',
648 get_available_genre_seeds =>
649 'GET /v1/recommendations/available-genre-seeds removed',
650 browse_featured_playlists =>
651 'GET /v1/browse/featured-playlists removed by Spotify (Nov 2024)',
652 browse_new_releases => 'GET /v1/browse/new-releases removed Feb 2026',
653 get_categories => 'GET /v1/browse/categories removed Feb 2026',
654 get_category => 'GET /v1/browse/categories/{id} removed Feb 2026',
655 artist_top_tracks => 'GET /v1/artists/{id}/top-tracks removed Feb 2026',
656 artist_related_artists =>
657 'GET /v1/artists/{id}/related-artists removed by Spotify (Nov 2024)',
658 user => 'GET /v1/users/{user_id} deprecated/removed Feb 2026',
659 remove_user_saved_tracks =>
660 'DELETE /v1/me/tracks removed Feb 2026; use remove_library_items()',
661 check_users_saved_tracks =>
662 'GET /v1/me/tracks/contains removed Feb 2026; use check_library_items()',
663 save_shows_for_current_user =>
664 'PUT /v1/me/shows removed Feb 2026; use save_library_items()',
665 check_users_saved_shows =>
666 'GET /v1/me/shows/contains removed Feb 2026; use check_library_items()',
667 save_audiobooks_for_current_user =>
668 'PUT /v1/me/audiobooks removed Feb 2026; use save_library_items()',
669 remove_users_saved_audiobooks =>
670 'DELETE /v1/me/audiobooks removed Feb 2026; use remove_library_items()',
671 check_users_saved_audiobooks =>
672 'GET /v1/me/audiobooks/contains removed Feb 2026; use check_library_items()',
673 follow_artists_or_users =>
674 'PUT /v1/me/following removed Feb 2026; use save_library_items()',
675 unfollow_artists_or_users =>
676 'DELETE /v1/me/following removed Feb 2026; use remove_library_items()',
677 check_if_user_follows_artists_or_users =>
678 'GET /v1/me/following/contains removed Feb 2026; use check_library_items()',
679 check_if_user_follows_playlist =>
680 'GET /v1/playlists/{id}/followers/contains removed Feb 2026; use check_library_items()',
681 );
682  
683 6 my %deprecation_warned;
684  
685 6 my %method_to_uri = ();
686  
687 6 foreach my $entry (@api_call_options) {
688 330 ▶ next if $entry->{method} eq q{};
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"method"} eq "" 0 330 330
689 330 $method_to_uri{ $entry->{method} } = $entry->{path};
690 }
691  
692 # _build_url: construct the request URL from an attributes hashref.
693 #
694 # For send_get_request the URL-building logic is richer (query_full_url
695 # passthrough, search substitution, extras appended as query params). That
696 # full logic stays in send_get_request. _build_url handles the simpler
697 # pattern shared by POST / PUT / DELETE.
698 sub _build_url {
699 18 ▶ my ( $self, $attributes ) = @_;
Subroutine _build_url: called CC=6 SCAR=18.6
700  
701 18 my $url = $self->uri_scheme() . '://' . $self->uri_hostname();
702 18 my $path = $method_to_uri{ $attributes->{method} };
703  
704 # Params consumed by a {placeholder} in the path are removed from
705 # %unused so the request body only carries what the URL did not.
706 18 ▶ my %unused = %{ $attributes->{params} || {} };
Branch: 1 branch
BranchTrueFalseTotal
$attributes->{"params"} or {} 0 18 18
707  
708 18 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 18 0 18
709 18 $path
710 8 ▶ =~ s/\{([^}]+)\}/my $v = delete $unused{$1}; defined $v ? $v : q{}/ge;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 8 0 8
711 18 $url .= $path;
712 }
713  
714 18 ▶ warn "$url\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 18 18
715 18 ▶ return wantarray ? ( $url, \%unused ) : $url;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 18 0 18
716 }
717  
718 # _send_request: shared machinery for every HTTP verb.
719 #
720 # Parameters:
721 # $verb - 'get' | 'post' | 'put' | 'delete'
722 # $url - fully-formed request URL
723 # $attributes - original attributes hashref (used for auth flag)
724 # $body - optional request body (undef for GET)
725 sub _send_request {
726 47 ▶ my ( $self, $verb, $url, $attributes, $body ) = @_;
Subroutine _send_request: called CC=12 SCAR=25.2
727  
728 47 ▶ my $method = $attributes->{method} // q{};
Condition50%1/2
$attributes->{"method"} // ""
l!l
470
MC/DC0%0/1
$attributes->{"method"} // ""
$attributes->{"method"}
Truth table50%1/2
A
A= $attributes->{"method"}
Aresult
11
00
729 47 ▶ if ( my $reason = $method_deprecated{$method} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $reason = $method_deprecated{$method}) 16 31 47
730 carp
731 "WWW::Spotify: $method() targets a removed/deprecated Spotify endpoint: $reason"
732 16 ▶ unless $deprecation_warned{$method}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $deprecation_warned{$method}++ 13 3 16
733 }
734  
735 47 local $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
736 47 my $mech = $self->_mech;
737  
738 47 ▶ if ( $attributes->{client_auth_required}
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{"client_auth_required"} or $self->force_client_auth != 0) 22 25 47
Condition100%3/3
$attributes->{"client_auth_required"} or $self->force_client_auth != 0
l!l&&r!l&&!r
18425
MC/DC100%2/2
$attributes->{"client_auth_required"} or $self->force_client_auth != 0
$attributes->{"client_auth_required"} $self->force_client_auth != 0
Truth table100%3/3
A or B
A= $attributes->{"client_auth_required"}B= $self->force_client_auth != 0
ABresult
1X1
011
000
739 || $self->force_client_auth() != 0 ) {
740 22 ▶ if ( $self->current_access_token() eq q{}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_access_token eq "" or time >= $self->token_expires_at) 1 21 22
Condition66%2/3
$self->current_access_token eq "" or time >= $self->token_expires_at
l!l&&r!l&&!r
0121
MC/DC50%1/2
$self->current_access_token eq "" or time >= $self->token_expires_at
$self->current_access_token eq "" time >= $self->token_expires_at
Truth table66%2/3
A or B
A= $self->current_access_token eq ""B= time >= $self->token_expires_at
ABresult
1X1
011
000
741 || time() >= $self->token_expires_at() ) {
742 1 ▶ warn "Needed to get access token\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
743 1 $self->current_access_token(q{});
744 1 $self->get_client_credentials();
745 }
746 $mech->add_header(
747 22 'Authorization' => 'Bearer ' . $self->current_access_token() );
748 }
749  
750 47 ▶ if ( defined $body ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $body) { } 18 29 47
751 18 $mech->add_header( 'Content-Type' => 'application/json' );
752 18 $mech->$verb( $url, Content => $body );
753 }
754 else {
755 29 $mech->$verb($url);
756 }
757  
758 47 ▶ if ( $self->grab_response_header() == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->grab_response_header == 1) 0 47 47
759 0 $self->_set_response_headers($mech);
760 }
761  
762 47 $self->response_status( $mech->status() );
763 47 $self->response_content_type( $mech->content_type() );
764  
765 47 ▶ if ( $self->_has_custom_request_handler() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_has_custom_request_handler) 0 47 47
766 0 $self->_set_custom_request_handler_result(
767 $self->custom_request_handler()->($mech) );
768 }
769  
770 47 return $mech;
771 }
772  
773 sub send_post_request {
774 7 ▶ my ( $self, $attributes ) = @_;
Subroutine send_post_request: called CC=7 SCAR=19.7
Pod undocumented
775  
776 7 $self->last_error(q{});
777  
778 7 my ( $url, $body_params ) = $self->_build_url($attributes);
779 7 ▶ my $body = %{$body_params} ? encode_json($body_params) : '';
Branch: 1 branch
BranchTrueFalseTotal
%{$body_params;} ? : 7 0 7
780 7 my $mech = $self->_send_request( 'post', $url, $attributes, $body );
781  
782 7 ▶ if ( $self->response_content_type() =~ /application\/json/i
Branch: 1 branch
BranchTrueFalseTotal
if ($self->response_content_type =~ m[application/json]i and $self->response_status != 200) 1 6 7
Condition66%2/3
$self->response_content_type =~ m[application/json]i and $self->response_status != 200
!ll&&!rl&&r
061
MC/DC50%1/2
$self->response_content_type =~ m[application/json]i and $self->response_status != 200
$self->response_content_type =~ m[application/json]i $self->response_status != 200
Truth table66%2/3
A and B
A= $self->response_content_type =~ m[application/json]iB= $self->response_status != 200
ABresult
0X0
100
111
783 && $self->response_status() != HTTP_OK ) {
784 1 ▶ warn "content type is ", $self->response_content_type(), "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
785 if $self->debug();
786 1 $self->last_error( "request failed, status("
787 . $self->response_status()
788 . ") examine last_result for details" );
789 }
790  
791 7 ▶ if ( $self->die_on_response_error() == 1 && $self->last_error ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->die_on_response_error == 1 and $self->last_error ne "") 1 6 7
Condition66%2/3
$self->die_on_response_error == 1 and $self->last_error ne ""
!ll&&!rl&&r
601
MC/DC50%1/2
$self->die_on_response_error == 1 and $self->last_error ne ""
$self->die_on_response_error == 1 $self->last_error ne ""
Truth table66%2/3
A and B
A= $self->die_on_response_error == 1B= $self->last_error ne ""
ABresult
0X0
100
111
792 1 die $self->last_error();
793 }
794  
795 6 return $self->format_results(
796 $mech->content, $mech->ct(),
797 $mech->status()
798 );
799 }
800  
801 sub send_delete_request {
802  
803 # Internal method used to send DELETE requests to the Spotify API.
804 6 ▶ my ( $self, $attributes ) = @_;
Subroutine send_delete_request: called CC=6 SCAR=21.0
Pod documented
805  
806 6 $self->last_error(q{});
807  
808 6 my ( $url, $body_params ) = $self->_build_url($attributes);
809 6 ▶ my $body = %{$body_params} ? encode_json($body_params) : '';
Branch: 1 branch
BranchTrueFalseTotal
%{$body_params;} ? : 3 3 6
810 6 my $mech = $self->_send_request( 'delete', $url, $attributes, $body );
811  
812 6 ▶ if ( !is_success( $self->response_status() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&is_success(scalar $self->response_status)) 0 6 6
813 0 ▶ warn "Delete request failed with status ", $self->response_status(),
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
814 "\n"
815 if $self->debug();
816 0 $self->last_error( "Delete request failed, status("
817 . $self->response_status()
818 . ") examine last_result for details" );
819 }
820  
821 6 ▶ if ( $self->die_on_response_error() == 1 && $self->last_error ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->die_on_response_error == 1 and $self->last_error ne "") 0 6 6
Condition33%1/3
$self->die_on_response_error == 1 and $self->last_error ne ""
!ll&&!rl&&r
600
MC/DC0%0/2
$self->die_on_response_error == 1 and $self->last_error ne ""
$self->die_on_response_error == 1 $self->last_error ne ""
Truth table33%1/3
A and B
A= $self->die_on_response_error == 1B= $self->last_error ne ""
ABresult
0X0
100
111
822 0 die $self->last_error();
823 }
824  
825 6 return $self->format_results(
826 $mech->content, $mech->ct(),
827 $mech->status()
828 );
829 }
830  
831 sub send_put_request {
832  
833 # Internal method used to send PUT requests to the Spotify API.
834 5 ▶ my ( $self, $attributes ) = @_;
Subroutine send_put_request: called CC=6 SCAR=21.0
Pod documented
835  
836 5 $self->last_error(q{});
837  
838 5 my ( $url, $body_params ) = $self->_build_url($attributes);
839 5 ▶ my $body = %{$body_params} ? encode_json($body_params) : '';
Branch: 1 branch
BranchTrueFalseTotal
%{$body_params;} ? : 4 1 5
840 5 my $mech = $self->_send_request( 'put', $url, $attributes, $body );
841  
842 5 ▶ if ( !is_success( $self->response_status() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&is_success(scalar $self->response_status)) 0 5 5
843 0 ▶ warn "Put request failed with status ", $self->response_status(), "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
844 if $self->debug();
845 0 $self->last_error( "Put request failed, status("
846 . $self->response_status()
847 . ") examine last_result for details" );
848 }
849  
850 5 ▶ if ( $self->die_on_response_error() == 1 && $self->last_error ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->die_on_response_error == 1 and $self->last_error ne "") 0 5 5
Condition33%1/3
$self->die_on_response_error == 1 and $self->last_error ne ""
!ll&&!rl&&r
500
MC/DC0%0/2
$self->die_on_response_error == 1 and $self->last_error ne ""
$self->die_on_response_error == 1 $self->last_error ne ""
Truth table33%1/3
A and B
A= $self->die_on_response_error == 1B= $self->last_error ne ""
ABresult
0X0
100
111
851 0 die $self->last_error();
852 }
853  
854 5 return $self->format_results(
855 $mech->content, $mech->ct(),
856 $mech->status()
857 );
858 }
859  
860 sub send_get_request {
861  
862 # need to build the URL here
863 29 ▶ my ( $self, $attributes ) = @_;
Subroutine send_get_request: called CC=21 SCAR=31.5
Pod undocumented
864  
865 29 my $uri_params = q{};
866  
867 # reset last error
868 29 $self->last_error(q{});
869  
870 29 ▶ if ( defined $attributes->{extras}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attributes->{"extras"} and ref $attributes->{"extras"} eq "HASH") 4 25 29
Condition66%2/3
defined $attributes->{"extras"} and ref $attributes->{"extras"} eq "HASH"
!ll&&!rl&&r
2504
MC/DC50%1/2
defined $attributes->{"extras"} and ref $attributes->{"extras"} eq "HASH"
defined $attributes->{"extras"} ref $attributes->{"extras"} eq "HASH"
Truth table66%2/3
A and B
A= defined $attributes->{"extras"}B= ref $attributes->{"extras"} eq "HASH"
ABresult
0X0
100
111
871 and ref $attributes->{extras} eq 'HASH' ) {
872 4 my @tmp = ();
873  
874 4 foreach my $key ( keys %{ $attributes->{extras} } ) {
875 8 push @tmp, "$key=" . uri_escape( $attributes->{extras}{$key} );
876 }
877 4 $uri_params = join( '&', @tmp );
878 }
879  
880 29 ▶ if ( exists $attributes->{format}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attributes->{"format"} and $attributes->{"format"} =~ /json|jsonp/) 0 29 29
Condition33%1/3
exists $attributes->{"format"} and $attributes->{"format"} =~ /json|jsonp/
!ll&&!rl&&r
2900
MC/DC0%0/2
exists $attributes->{"format"} and $attributes->{"format"} =~ /json|jsonp/
exists $attributes->{"format"} $attributes->{"format"} =~ /json|jsonp/
Truth table33%1/3
A and B
A= exists $attributes->{"format"}B= $attributes->{"format"} =~ /json|jsonp/
ABresult
0X0
100
111
881 && $attributes->{format} =~ /json|jsonp/ ) {
882 0 $self->result_format( $attributes->{format} );
883 0 delete $attributes->{format};
884 }
885  
886 29 my $url;
887 29 ▶ if ( $attributes->{method} eq 'query_full_url' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attributes->{'method'} eq 'query_full_url') { } 1 28 29
888 1 $url = $attributes->{url};
889 }
890 else {
891 28 $url = $self->uri_scheme() . '://' . $self->uri_hostname();
892  
893 28 my $path = $method_to_uri{ $attributes->{method} };
894 28 ▶ if ($path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path) 28 0 28
895  
896 28 ▶ warn "raw: $path" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 28 28
897  
898 28 ▶ if ( $path =~ /search/ && $attributes->{method} eq 'search' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /search/ and $attributes->{"method"} eq "search") 1 27 28
Condition66%2/3
$path =~ /search/ and $attributes->{"method"} eq "search"
!ll&&!rl&&r
2701
MC/DC50%1/2
$path =~ /search/ and $attributes->{"method"} eq "search"
$path =~ /search/ $attributes->{"method"} eq "search"
Truth table66%2/3
A and B
A= $path =~ /search/B= $attributes->{"method"} eq "search"
ABresult
0X0
100
111
899 1 $path =~ s/\{q\}/uri_escape( $attributes->{q} )/e;
900 1 $path =~ s/\{type\}/uri_escape( $attributes->{type} )/e;
901 }
902  
903 # Generic substitution for all remaining {placeholder} tokens
904 $path =~ s/\{([^}]+)\}/$attributes->{params}{$1}/g
905 28 ▶ if $attributes->{params};
Branch: 1 branch
BranchTrueFalseTotal
if $attributes->{"params"} 26 2 28
906  
907 28 ▶ warn "modified: $path\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 28 28
908 }
909  
910 28 $url .= $path;
911 }
912  
913 # append "extras" as query params if present
914 29 ▶ if ($uri_params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri_params) 4 25 29
915 4 ▶ my $start_with = $url =~ /\?/ ? '&' : '?';
Branch: 1 branch
BranchTrueFalseTotal
$url =~ /\?/ ? : 1 3 4
916 4 $url .= $start_with . $uri_params;
917 }
918  
919 29 ▶ warn "$url\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 29 29
920  
921 29 my $mech = $self->_send_request( 'get', $url, $attributes, undef );
922  
923 # the original code did not provide adequate built in validation
924 # of the response for an API call.
925 # Adding a new method (die_on_response_error) with a default of 0 to avoid
926 # breaking/changing existing code using older versions of this module.
927 29 ▶ if ( $self->response_content_type() =~ /application\/json/i
Branch: 1 branch
BranchTrueFalseTotal
if ($self->response_content_type =~ m[application/json]i and $self->response_status != 200) 1 28 29
Condition66%2/3
$self->response_content_type =~ m[application/json]i and $self->response_status != 200
!ll&&!rl&&r
0281
MC/DC50%1/2
$self->response_content_type =~ m[application/json]i and $self->response_status != 200
$self->response_content_type =~ m[application/json]i $self->response_status != 200
Truth table66%2/3
A and B
A= $self->response_content_type =~ m[application/json]iB= $self->response_status != 200
ABresult
0X0
100
111
928 && $self->response_status() != HTTP_OK ) {
929 1 ▶ warn "content type is ", $self->response_content_type(), "\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
930 if $self->debug();
931 1 $self->last_error( "request failed, status("
932 . $self->response_status()
933 . ") examine last_result for details" );
934 }
935  
936 29 ▶ if ( $self->die_on_response_error() == 1 && $self->last_error ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->die_on_response_error == 1 and $self->last_error ne "") 1 28 29
Condition66%2/3
$self->die_on_response_error == 1 and $self->last_error ne ""
!ll&&!rl&&r
2801
MC/DC50%1/2
$self->die_on_response_error == 1 and $self->last_error ne ""
$self->die_on_response_error == 1 $self->last_error ne ""
Truth table66%2/3
A and B
A= $self->die_on_response_error == 1B= $self->last_error ne ""
ABresult
0X0
100
111
937 1 die $self->last_error();
938 }
939  
940 28 return $self->format_results(
941 $mech->content, $mech->ct(),
942 $mech->status()
943 );
944 }
945  
946 sub _set_response_headers {
947 0 ▶ my $self = shift;
Subroutine _set_response_headers: not called CC=1 SCAR=6.9
948 0 my $mech = shift;
949  
950 0 my $hd;
951 0 ▶ capture { $mech->dump_headers(); } \$hd;
Subroutine __ANON__: not called CC=1 SCAR=6.9
952  
953 0 $self->response_headers($hd);
954 0 return;
955 }
956  
957 sub format_results {
958 45 ▶ my $self = shift;
Subroutine format_results: called CC=3 SCAR=12.3
Pod undocumented
959 45 my $content = shift;
960  
961 # want to store the result in case
962 # we want to interact with it via a helper method
963 45 $self->last_result($content);
964  
965 # FIX ME / TEST ME
966 # vefify both of these work and return the *same* perl hash
967  
968 # when / how should we check the status? Do we need to?
969 # if so then we need to create another method that will
970 # manage a Sucess vs. Fail request
971  
972 45 ▶ if ( $self->auto_json_decode && $self->result_format eq 'json' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto_json_decode and $self->result_format eq "json") 0 45 45
Condition33%1/3
$self->auto_json_decode and $self->result_format eq "json"
!ll&&!rl&&r
4500
MC/DC0%0/2
$self->auto_json_decode and $self->result_format eq "json"
$self->auto_json_decode $self->result_format eq "json"
Truth table33%1/3
A and B
A= $self->auto_json_decodeB= $self->result_format eq "json"
ABresult
0X0
100
111
973 0 return decode_json $content;
974 }
975  
976 # results are not altered in this case and would be
977 # json instead of a perl data structure
978  
979 45 return $content;
980 }
981  
982 sub get_oauth_authorize {
983 0 ▶ my $self = shift;
Subroutine get_oauth_authorize: not called CC=2 SCAR=17.9
Pod undocumented
984  
985 0 ▶ if ( $self->current_oath_code() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_oath_code) 0 0 0
986 0 return $self->current_oath_code();
987 }
988  
989 0 my $grant_type = 'authorization_code';
990 0 local $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
991 0 my $client_and_secret
992 = $self->oauth_client_id() . ':' . $self->oauth_client_secret();
993 0 my $encoded = encode_base64($client_and_secret);
994 0 chomp($encoded);
995 0 $encoded =~ s/\n//g;
996 0 my $url = $self->oauth_authorize_url();
997  
998 0 my @parts;
999  
1000 0 $parts[0] = 'response_type=code';
1001 0 $parts[1] = 'redirect_uri=' . $self->oauth_redirect_uri;
1002  
1003 0 my $params = join( '&', @parts );
1004 0 $url = $url . '?client_id=' . $self->oauth_client_id() . "&$params";
1005  
1006 0 $self->ua->get($url);
1007  
1008 0 return $self->ua->content;
1009 }
1010  
1011 sub get_client_credentials {
1012 2 ▶ my $self = shift;
Subroutine get_client_credentials: called CC=8 SCAR=21.9
Pod undocumented
1013 2 my $scope = shift;
1014  
1015 2 ▶ if ( $self->current_access_token() ne q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_access_token ne "") 0 2 2
1016 0 return $self->current_access_token();
1017 }
1018 2 ▶ if ( $self->oauth_client_id() eq q{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->oauth_client_id eq "") 0 2 2
1019 0 die "need to set the client oauth parameters\n";
1020 }
1021  
1022 2 my $grant_type = 'client_credentials';
1023 2 local $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
1024 2 my $mech = $self->_mech;
1025 2 my $client_and_secret
1026 = $self->oauth_client_id() . ':' . $self->oauth_client_secret();
1027 2 my $encoded = encode_base64($client_and_secret);
1028 2 my $url = $self->oauth_token_url();
1029  
1030 # $url .= "?grant_type=client_credentials";
1031 # my $url = $self->oauth_authorize_url();
1032 # grant_type=client_credentials
1033 2 my $extra = {
1034 grant_type => $grant_type
1035  
1036 #code => 'code',
1037 #redirect_uri => $self->oauth_redirect_uri
1038 };
1039 2 ▶ if ($scope) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scope) 0 2 2
1040 0 $extra->{scope} = $scope;
1041 }
1042  
1043 2 chomp($encoded);
1044 2 $encoded =~ s/\n//g;
1045 2 $mech->add_header( 'Authorization' => 'Basic ' . $encoded );
1046  
1047 2 $mech->post( $url, [$extra] );
1048 2 my $content = $mech->content();
1049  
1050 2 ▶ if ( $content =~ /access_token/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /access_token/) 2 0 2
1051 2 ▶ warn "setting access token\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 2 2
1052  
1053 2 my $result = decode_json $content;
1054  
1055 2 ▶ if ( $result->{'access_token'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"access_token"}) 2 0 2
1056 2 $self->current_access_token( $result->{'access_token'} );
1057 2 ▶ if ( $result->{'expires_in'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{"expires_in"}) 2 0 2
1058 2 $self->token_expires_at( time() + $result->{'expires_in'} );
1059 }
1060 }
1061 }
1062 }
1063  
1064 sub authorize_url {
1065 2 ▶ my ( $self, $args ) = @_;
Subroutine authorize_url: called CC=4 SCAR=13.9
Pod documented
1066 2 ▶ $args ||= {};
Condition100%2/2
$args ||= {}
l!l
11
MC/DC100%1/1
$args ||= {}
$args
Truth table100%2/2
A
A= $args
Aresult
11
00
1067  
1068 2 my @parts = (
1069 'client_id=' . uri_escape( $self->oauth_client_id() ),
1070 'response_type=code',
1071 'redirect_uri=' . uri_escape( $self->oauth_redirect_uri() ),
1072 );
1073 2 ▶ push @parts, 'scope=' . uri_escape( $args->{scope} ) if $args->{scope};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"scope"} 1 1 2
1074 2 ▶ push @parts, 'state=' . uri_escape( $args->{state} ) if $args->{state};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"state"} 1 1 2
1075  
1076 2 return $self->oauth_authorize_url() . '?' . join '&', @parts;
1077 }
1078  
1079 sub _request_token {
1080 4 ▶ my ( $self, $form ) = @_;
Subroutine _request_token: called CC=5 SCAR=16.1
1081  
1082 4 my $encoded = encode_base64(
1083 $self->oauth_client_id() . ':' . $self->oauth_client_secret(), q{} );
1084  
1085 4 my $mech = $self->_mech;
1086 4 $mech->add_header( 'Authorization' => 'Basic ' . $encoded );
1087 4 $mech->post( $self->oauth_token_url(), [$form] );
1088  
1089 4 my $result = eval { decode_json( $mech->content() ) };
1090  
1091 4 ▶ return 0 unless $result && $result->{access_token};
Branch: 1 branch
BranchTrueFalseTotal
unless $result and $result->{"access_token"} 1 3 4
Condition66%2/3
$result and $result->{"access_token"}
!ll&&!rl&&r
013
MC/DC50%1/2
$result and $result->{"access_token"}
$result $result->{"access_token"}
Truth table66%2/3
A and B
A= $resultB= $result->{"access_token"}
ABresult
0X0
100
111
1092  
1093 3 $self->current_access_token( $result->{access_token} );
1094 $self->token_expires_at( time() + $result->{expires_in} )
1095 3 ▶ if $result->{expires_in};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"expires_in"} 3 0 3
1096 $self->refresh_token( $result->{refresh_token} )
1097 3 ▶ if $result->{refresh_token};
Branch: 1 branch
BranchTrueFalseTotal
if $result->{"refresh_token"} 2 1 3
1098  
1099 3 return 1;
1100 }
1101  
1102 sub get_access_token {
1103 2 ▶ my ( $self, $code ) = @_;
Subroutine get_access_token: called CC=3 SCAR=12.5
Pod documented
1104  
1105 2 ▶ die "get_access_token requires an authorization code\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code and length $code 0 2 2
Condition33%1/3
defined $code and length $code
!ll&&!rl&&r
002
MC/DC0%0/2
defined $code and length $code
defined $code length $code
Truth table33%1/3
A and B
A= defined $codeB= length $code
ABresult
0X0
100
111
1106 unless defined $code && length $code;
1107  
1108 2 return $self->_request_token(
1109 {
1110 grant_type => 'authorization_code',
1111 code => $code,
1112 redirect_uri => $self->oauth_redirect_uri(),
1113 }
1114 );
1115 }
1116  
1117 sub refresh_access_token {
1118 3 ▶ my $self = shift;
Subroutine refresh_access_token: called CC=2 SCAR=6.9
Pod documented
1119  
1120 3 ▶ die "refresh_access_token requires a stored refresh token\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->refresh_token 1 2 3
1121 unless $self->refresh_token();
1122  
1123 2 return $self->_request_token(
1124 {
1125 grant_type => 'refresh_token',
1126 refresh_token => $self->refresh_token(),
1127 }
1128 );
1129 }
1130  
1131 sub get {
1132  
1133 # This seemed like a simple enough method
1134 # but everything I tried resulted in unacceptable
1135 # trade offs and explict defining of the structures
1136 # The new method, which I hope I remember when I
1137 # revisit it, was to use JSON::Path
1138 # It is an awesome module, but a little heavy
1139 # on dependencies. However I would not have been
1140 # able to do this in so few lines without it
1141  
1142 # Making a generalization here
1143 # if you use a * you are looking for an array
1144 # if you don't have an * you want the first 1 (or should I say you get the first 1)
1145  
1146 1 ▶ my ( $self, @return ) = @_;
Subroutine get: called CC=6 SCAR=19.2
Pod documented
1147  
1148 # my @return = @_;
1149  
1150 1 my @out;
1151  
1152 1 my $result = decode_json $self->last_result();
1153  
1154 1 my $search_ref = $result;
1155  
1156 1 ▶ warn Dumper($result) if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
1157  
1158 1 foreach my $key (@return) {
1159 1 my $type = 'value';
1160 1 ▶ if ( $key =~ /\*\]/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\*\]/) 0 1 1
1161 0 $type = 'values';
1162 }
1163  
1164 1 my $jpath = JSON::Path->new("\$.$key");
1165  
1166 1 my @t_arr = $jpath->$type($result);
1167  
1168 1 ▶ if ( $type eq 'value' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'value') { } 1 0 1
1169 1 push @out, $t_arr[0];
1170 }
1171 else {
1172 0 push @out, \@t_arr;
1173 }
1174 }
1175 1 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 1 1
1176 0 return @out;
1177 }
1178 else {
1179 1 return $out[0];
1180 }
1181  
1182 }
1183  
1184 sub build_url_base {
1185  
1186 # first the uri type
1187 0 ▶ my $self = shift;
Subroutine build_url_base: not called CC=3 SCAR=24.8
Pod undocumented
1188 0 ▶ my $call_type = shift || $self->call_type();
Condition0%0/3
shift() || $self->call_type
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->call_type
shift() $self->call_type
Truth table0%0/3
A || B
A= shift()B= $self->call_type
ABresult
1X1
011
000
1189  
1190 0 my $url = $self->uri_scheme();
1191  
1192 # the ://
1193 0 $url .= '://';
1194  
1195 # the domain
1196 0 $url .= $self->uri_hostname();
1197  
1198 # the path
1199 0 ▶ if ( $self->uri_domain_path() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->uri_domain_path) 0 0 0
1200 0 $url .= '/' . $self->uri_domain_path();
1201 }
1202  
1203 0 return $url;
1204 }
1205  
1206 #- may want to move this at some point
1207  
1208 sub query_full_url {
1209 0 ▶ my $self = shift;
Subroutine query_full_url: not called CC=2 SCAR=17.9
Pod documented
1210 0 my $url = shift;
1211 0 ▶ my $client_auth_required = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1212 0 return $self->send_get_request(
1213 {
1214 method => 'query_full_url',
1215 url => $url,
1216 client_auth_required => $client_auth_required
1217 }
1218 );
1219 }
1220  
1221 #-- spotify specific methods
1222  
1223 sub album {
1224 0 ▶ my $self = shift;
Subroutine album: not called CC=1 SCAR=6.9
Pod documented
1225 0 my $id = shift;
1226  
1227 0 return $self->send_get_request(
1228 {
1229 method => 'album',
1230 params => { 'id' => $id },
1231 client_auth_required => 1
1232 }
1233 );
1234 }
1235  
1236 sub albums {
1237 2 ▶ my $self = shift;
Subroutine albums: called CC=2 SCAR=6.9
Pod documented
1238 2 my $ids = shift;
1239  
1240 2 ▶ if ( ref($ids) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ids eq "ARRAY") 1 1 2
1241 1 $ids = join_ids($ids);
1242 }
1243  
1244 2 return $self->send_get_request(
1245 {
1246 method => 'albums',
1247 params => { 'ids' => $ids },
1248 client_auth_required => 1
1249 }
1250 );
1251  
1252 }
1253  
1254 sub join_ids {
1255 2 ▶ my $array = shift;
Subroutine join_ids: called CC=1 SCAR=0.0
Pod undocumented
1256 2 return join( ',', @$array );
1257 }
1258  
1259 sub albums_tracks {
1260 1 ▶ my $self = shift;
Subroutine albums_tracks: called CC=1 SCAR=0.0
Pod documented
1261 1 my $album_id = shift;
1262 1 my $extras = shift;
1263  
1264 1 return $self->send_get_request(
1265 {
1266 method => 'albums_tracks',
1267 params => { 'id' => $album_id },
1268 extras => $extras,
1269 client_auth_required => 1
1270 }
1271 );
1272  
1273 }
1274  
1275 sub artist {
1276 1 ▶ my $self = shift;
Subroutine artist: called CC=1 SCAR=0.0
Pod documented
1277 1 my $id = shift;
1278  
1279 1 return $self->send_get_request(
1280 {
1281 method => 'artist',
1282 params => { 'id' => $id },
1283 client_auth_required => 1
1284 }
1285 );
1286  
1287 }
1288  
1289 sub artists {
1290 1 ▶ my $self = shift;
Subroutine artists: called CC=2 SCAR=7.4
Pod documented
1291 1 my $artists = shift;
1292  
1293 1 ▶ if ( ref($artists) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $artists eq "ARRAY") 0 1 1
1294 0 $artists = join_ids($artists);
1295 }
1296  
1297 1 return $self->send_get_request(
1298 {
1299 method => 'artists',
1300 params => { 'ids' => $artists },
1301 client_auth_required => 1
1302 }
1303 );
1304  
1305 }
1306  
1307 sub artist_albums {
1308 1 ▶ my $self = shift;
Subroutine artist_albums: called CC=1 SCAR=0.0
Pod documented
1309 1 my $artist_id = shift;
1310 1 my $extras = shift;
1311  
1312 1 return $self->send_get_request(
1313 {
1314 method => 'artist_albums',
1315 params => { 'id' => $artist_id },
1316 extras => $extras,
1317 client_auth_required => 1
1318 }
1319 );
1320  
1321 }
1322  
1323 sub artist_top_tracks {
1324 1 ▶ my $self = shift;
Subroutine artist_top_tracks: called CC=1 SCAR=0.0
Pod documented
1325 1 my $artist_id = shift;
1326 1 my $country = shift;
1327  
1328 1 return $self->send_get_request(
1329 {
1330 method => 'artist_top_tracks',
1331 params => {
1332 'id' => $artist_id,
1333 'country' => $country,
1334 client_auth_required => 1
1335 }
1336 }
1337 );
1338  
1339 }
1340  
1341 sub artist_related_artists {
1342 1 ▶ my $self = shift;
Subroutine artist_related_artists: called CC=1 SCAR=0.0
Pod documented
1343 1 my $artist_id = shift;
1344 1 my $country = shift;
1345  
1346 1 return $self->send_get_request(
1347 {
1348 method => 'artist_related_artists',
1349 params => { 'id' => $artist_id }
1350  
1351 # 'country' => $country
1352 # }
1353 }
1354 );
1355  
1356 }
1357  
1358 sub me {
1359 0 ▶ my $self = shift;
Subroutine me: not called CC=1 SCAR=6.9
Pod undocumented
1360 0 return $self->send_get_request(
1361 {
1362 method => 'me',
1363 client_auth_required => 1
1364 }
1365 );
1366 }
1367  
1368 sub next_result_set {
1369 0 ▶ my $self = shift;
Subroutine next_result_set: not called CC=4 SCAR=30.0
Pod undocumented
1370 0 my $url = $self->get('next');
1371 0 ▶ return unless defined $url && $url ne 'null' && $url ne q{};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and $url ne "null" and $url ne "" 0 0 0
Condition0%0/6
defined $url and $url ne "null" and $url ne ""
!ll&&!rl&&r
000
defined $url and $url ne "null"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $url and $url ne "null" and $url ne ""
defined $url $url ne "null" $url ne ""
Truth table0%0/4
A and B and C
A= defined $urlB= $url ne "null"C= $url ne ""
ABCresult
0XX0
10X0
1100
1111
1372 0 return $self->query_full_url( $url, 1 );
1373 }
1374  
1375 sub previous_result_set {
1376 0 ▶ my $self = shift;
Subroutine previous_result_set: not called CC=4 SCAR=30.0
Pod undocumented
1377 0 my $url = $self->get('previous');
1378 0 ▶ return unless defined $url && $url ne 'null' && $url ne q{};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and $url ne "null" and $url ne "" 0 0 0
Condition0%0/6
defined $url and $url ne "null" and $url ne ""
!ll&&!rl&&r
000
defined $url and $url ne "null"
!ll&&!rl&&r
000
MC/DC0%0/3
defined $url and $url ne "null" and $url ne ""
defined $url $url ne "null" $url ne ""
Truth table0%0/4
A and B and C
A= defined $urlB= $url ne "null"C= $url ne ""
ABCresult
0XX0
10X0
1100
1111
1379 0 return $self->query_full_url( $url, 1 );
1380 }
1381  
1382 sub search {
1383 1 ▶ my $self = shift;
Subroutine search: called CC=1 SCAR=0.0
Pod documented
1384 1 my $q = shift;
1385 1 my $type = shift;
1386 1 my $extras = shift;
1387  
1388 # looks like search now requires auth
1389 # we will force authentication but need to
1390 # reset this to the previous value since not
1391 # all requests require auth
1392 1 my $old_force_client_auth = $self->force_client_auth();
1393 1 $self->force_client_auth(1);
1394  
1395 1 my $params = {
1396 method => 'search',
1397 q => $q,
1398 type => $type,
1399 extras => $extras
1400  
1401 };
1402  
1403 1 my $response = $self->send_get_request($params);
1404  
1405 # reset auth to what it was before to avoid overly chatty
1406 # requests
1407 1 $self->force_client_auth($old_force_client_auth);
1408 1 return $response;
1409 }
1410  
1411 sub track {
1412 1 ▶ my $self = shift;
Subroutine track: called CC=1 SCAR=0.0
Pod documented
1413 1 my $id = shift;
1414 1 return $self->send_get_request(
1415 {
1416 method => 'track',
1417 params => { 'id' => $id }
1418 }
1419 );
1420 }
1421  
1422 sub browse_featured_playlists {
1423 0 ▶ my $self = shift;
Subroutine browse_featured_playlists: not called CC=1 SCAR=6.9
Pod documented
1424 0 my $extras = shift;
1425  
1426 # locale
1427 # country
1428 # limit
1429 # offset
1430  
1431 0 return $self->send_get_request(
1432 {
1433 method => 'browse_featured_playlists',
1434 extras => $extras,
1435 client_auth_required => 1
1436 }
1437 );
1438 }
1439  
1440 sub browse_new_releases {
1441 0 ▶ my $self = shift;
Subroutine browse_new_releases: not called CC=1 SCAR=6.9
Pod documented
1442 0 my $extras = shift;
1443  
1444 # locale
1445 # country
1446 # limit
1447 # offset
1448  
1449 0 return $self->send_get_request(
1450 {
1451 method => 'browse_new_releases',
1452 extras => $extras,
1453 client_auth_required => 1
1454 }
1455 );
1456 }
1457  
1458 sub tracks {
1459 1 ▶ my $self = shift;
Subroutine tracks: called CC=2 SCAR=7.0
Pod documented
1460 1 my $tracks = shift;
1461  
1462 1 ▶ if ( ref($tracks) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $tracks eq "ARRAY") 1 0 1
1463 1 $tracks = join_ids($tracks);
1464 }
1465  
1466 1 return $self->send_get_request(
1467 {
1468 method => 'tracks',
1469 params => { 'ids' => $tracks }
1470 }
1471 );
1472  
1473 }
1474  
1475 sub user {
1476 1 ▶ my $self = shift;
Subroutine user: called CC=1 SCAR=0.0
Pod documented
1477 1 my $user_id = shift;
1478 1 return $self->send_get_request(
1479 {
1480 method => 'user',
1481 params => { 'user_id' => $user_id }
1482 }
1483 );
1484  
1485 }
1486  
1487 sub get_playlist {
1488 0 ▶ my ( $self, $playlist_id ) = @_;
Subroutine get_playlist: not called CC=1 SCAR=6.9
Pod documented
1489 0 return $self->send_get_request(
1490 {
1491 method => 'get_playlist',
1492 params => { 'playlist_id' => $playlist_id },
1493 client_auth_required => 1
1494 }
1495 );
1496 }
1497  
1498 sub get_playlist_items {
1499 1 ▶ my ( $self, $playlist_id, $extras ) = @_;
Subroutine get_playlist_items: called CC=1 SCAR=0.0
Pod documented
1500 1 return $self->send_get_request(
1501 {
1502 method => 'get_playlist_items',
1503 params => { 'playlist_id' => $playlist_id },
1504 client_auth_required => 1,
1505 extras => $extras
1506 }
1507 );
1508 }
1509  
1510 sub create_playlist {
1511 2 ▶ my ( $self, $name, $public, $description ) = @_;
Subroutine create_playlist: called CC=4 SCAR=13.9
Pod documented
1512  
1513 2 my %params = ( 'name' => $name );
1514 2 ▶ $params{public} = $public ? \1 : \0 if defined $public;
Branch: 2 branches
BranchTrueFalseTotal
if defined $public 2 0 2
$public ? : 1 1 2
1515 2 ▶ $params{description} = $description if defined $description;
Branch: 1 branch
BranchTrueFalseTotal
if defined $description 1 1 2
1516  
1517 2 return $self->send_post_request(
1518 {
1519 method => 'create_playlist',
1520 client_auth_required => 1,
1521 params => \%params
1522 }
1523 );
1524 }
1525  
1526 sub get_current_user_playlists {
1527 1 ▶ my ( $self, $extras ) = @_;
Subroutine get_current_user_playlists: called CC=1 SCAR=0.0
Pod documented
1528 1 return $self->send_get_request(
1529 {
1530 method => 'get_current_user_playlists',
1531 client_auth_required => 1,
1532 extras => $extras
1533 }
1534 );
1535 }
1536  
1537 sub add_items_to_playlist {
1538 3 ▶ my ( $self, $playlist_id, $uris, $position ) = @_;
Subroutine add_items_to_playlist: called CC=3 SCAR=11.0
Pod documented
1539  
1540 3 ▶ my %params = (
Branch: 1 branch
BranchTrueFalseTotal
ref $uris eq 'ARRAY' ? : 1 2 3
1541 'playlist_id' => $playlist_id,
1542 'uris' => ref $uris eq 'ARRAY' ? $uris : [$uris],
1543 );
1544 3 ▶ $params{position} = $position if defined $position;
Branch: 1 branch
BranchTrueFalseTotal
if defined $position 1 2 3
1545  
1546 3 return $self->send_post_request(
1547 {
1548 method => 'add_items_to_playlist',
1549 client_auth_required => 1,
1550 params => \%params
1551 }
1552 );
1553 }
1554  
1555 sub unfollow_playlist {
1556 2 ▶ my ( $self, $playlist_id ) = @_;
Subroutine unfollow_playlist: called CC=1 SCAR=0.0
Pod documented
1557 2 return $self->send_delete_request(
1558 {
1559 method => 'unfollow_playlist',
1560 client_auth_required => 1,
1561 params => { 'playlist_id' => $playlist_id }
1562 }
1563 );
1564 }
1565  
1566 sub remove_user_saved_tracks {
1567 0 ▶ my ( $self, $ids ) = @_;
Subroutine remove_user_saved_tracks: not called CC=2 SCAR=17.9
Pod documented
1568  
1569 0 ▶ if ( ref($ids) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ids eq "ARRAY") 0 0 0
1570 0 $ids = join_ids($ids);
1571 }
1572  
1573 0 return $self->send_delete_request(
1574 {
1575 method => 'remove_user_saved_tracks',
1576 params => { 'ids' => $ids }
1577 }
1578 );
1579 }
1580  
1581 sub check_users_saved_tracks {
1582 0 ▶ my ( $self, $ids ) = @_;
Subroutine check_users_saved_tracks: not called CC=2 SCAR=17.9
Pod documented
1583  
1584 0 ▶ if ( ref($ids) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ids eq "ARRAY") 0 0 0
1585 0 $ids = join_ids($ids);
1586 }
1587  
1588 0 return $self->send_get_request(
1589 {
1590 method => 'check_users_saved_tracks',
1591 params => { 'ids' => $ids },
1592 client_auth_required => 1
1593 }
1594 );
1595 }
1596  
1597 sub get_several_tracks_audio_features {
1598 0 ▶ my ( $self, $ids ) = @_;
Subroutine get_several_tracks_audio_features: not called CC=2 SCAR=17.9
Pod documented
1599  
1600 0 ▶ if ( ref($ids) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $ids eq "ARRAY") 0 0 0
1601 0 $ids = join_ids($ids);
1602 }
1603  
1604 0 return $self->send_get_request(
1605 {
1606 method => 'get_several_tracks_audio_features',
1607 params => { 'ids' => $ids },
1608 client_auth_required => 1
1609 }
1610 );
1611 }
1612  
1613 sub get_track_audio_features {
1614 0 ▶ my ( $self, $id ) = @_;
Subroutine get_track_audio_features: not called CC=1 SCAR=6.9
Pod documented
1615  
1616 0 return $self->send_get_request(
1617 {
1618 method => 'get_track_audio_features',
1619 params => { 'id' => $id },
1620 client_auth_required => 1
1621 }
1622 );
1623 }
1624  
1625 sub get_track_audio_analysis {
1626 0 ▶ my ( $self, $id ) = @_;
Subroutine get_track_audio_analysis: not called CC=1 SCAR=6.9
Pod documented
1627  
1628 0 return $self->send_get_request(
1629 {
1630 method => 'get_track_audio_analysis',
1631 params => { 'id' => $id },
1632 client_auth_required => 1
1633 }
1634 );
1635 }
1636  
1637 sub get_recommendations {
1638 0 ▶ my ( $self, %params ) = @_;
Subroutine get_recommendations: not called CC=1 SCAR=6.9
Pod documented
1639  
1640 0 return $self->send_get_request(
1641 {
1642 method => 'get_recommendations',
1643 params => \%params,
1644 client_auth_required => 1
1645 }
1646 );
1647 }
1648  
1649 sub get_followed_artists {
1650 0 ▶ my ( $self, %params ) = @_;
Subroutine get_followed_artists: not called CC=1 SCAR=6.9
Pod documented
1651  
1652 # Ensure 'type' is set to 'artist' as it's the only supported value
1653 0 $params{type} = 'artist';
1654  
1655 0 return $self->send_get_request(
1656 {
1657 method => 'get_followed_artists',
1658 params => \%params,
1659 client_auth_required => 1
1660 }
1661 );
1662 }
1663  
1664 sub follow_artists_or_users {
1665 0 ▶ my ( $self, $type, $ids ) = @_;
Subroutine follow_artists_or_users: not called CC=4 SCAR=30.0
Pod documented
1666  
1667 0 ▶ die "Type must be 'artist' or 'user'"
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "artist" or $type eq "user" 0 0 0
Condition0%0/3
$type eq "artist" or $type eq "user"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "artist" or $type eq "user"
$type eq "artist" $type eq "user"
Truth table0%0/3
A or B
A= $type eq "artist"B= $type eq "user"
ABresult
1X1
011
000
1668 unless $type eq 'artist' or $type eq 'user';
1669  
1670 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
1671  
1672 0 return $self->send_put_request(
1673 {
1674 method => 'follow_artists_or_users',
1675 params => {
1676 type => $type,
1677 ids => $id_list
1678 },
1679 client_auth_required => 1
1680 }
1681 );
1682 }
1683  
1684 sub unfollow_artists_or_users {
1685 0 ▶ my ( $self, $type, $ids ) = @_;
Subroutine unfollow_artists_or_users: not called CC=4 SCAR=30.0
Pod documented
1686  
1687 0 ▶ die "Type must be 'artist' or 'user'"
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "artist" or $type eq "user" 0 0 0
Condition0%0/3
$type eq "artist" or $type eq "user"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "artist" or $type eq "user"
$type eq "artist" $type eq "user"
Truth table0%0/3
A or B
A= $type eq "artist"B= $type eq "user"
ABresult
1X1
011
000
1688 unless $type eq 'artist' or $type eq 'user';
1689  
1690 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
1691  
1692 0 return $self->send_delete_request(
1693 {
1694 method => 'unfollow_artists_or_users',
1695 params => {
1696 type => $type,
1697 ids => $id_list
1698 },
1699 client_auth_required => 1
1700 }
1701 );
1702 }
1703  
1704 sub check_if_user_follows_artists_or_users {
1705 0 ▶ my ( $self, $type, $ids ) = @_;
Subroutine check_if_user_follows_artists_or_users: not called CC=4 SCAR=30.0
Pod documented
1706  
1707 0 ▶ die "Type must be 'artist' or 'user'"
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "artist" or $type eq "user" 0 0 0
Condition0%0/3
$type eq "artist" or $type eq "user"
l!l&&r!l&&!r
000
MC/DC0%0/2
$type eq "artist" or $type eq "user"
$type eq "artist" $type eq "user"
Truth table0%0/3
A or B
A= $type eq "artist"B= $type eq "user"
ABresult
1X1
011
000
1708 unless $type eq 'artist' or $type eq 'user';
1709  
1710 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
1711  
1712 0 return $self->send_get_request(
1713 {
1714 method => 'check_if_user_follows_artists_or_users',
1715 params => {
1716 type => $type,
1717 ids => $id_list
1718 },
1719 client_auth_required => 1
1720 }
1721 );
1722 }
1723  
1724 sub check_if_user_follows_playlist {
1725 0 ▶ my ( $self, $playlist_id, $ids ) = @_;
Subroutine check_if_user_follows_playlist: not called CC=4 SCAR=30.0
Pod documented
1726  
1727 0 ▶ die "playlist_id is required" unless $playlist_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $playlist_id 0 0 0
1728 0 ▶ die "ids is required" unless $ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids 0 0 0
1729  
1730 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
1731  
1732 0 return $self->send_get_request(
1733 {
1734 method => 'check_if_user_follows_playlist',
1735 params => {
1736 playlist_id => $playlist_id,
1737 ids => $id_list
1738 },
1739 client_auth_required => 1
1740 }
1741 );
1742 }
1743  
1744 # Spotify URIs contain ':' so they must be escaped before being placed
1745 # in the uris= query parameter. Commas separating multiple URIs are
1746 # escaped too (%2C), which the API accepts.
1747 sub _uris_param {
1748 3 ▶ my $uris = shift;
Subroutine _uris_param: called CC=2 SCAR=6.9
1749 3 ▶ $uris = join( ',', @{$uris} ) if ref $uris eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $uris eq "ARRAY" 2 1 3
1750 3 return uri_escape($uris);
1751 }
1752  
1753 sub save_library_items {
1754 1 ▶ my ( $self, $uris ) = @_;
Subroutine save_library_items: called CC=2 SCAR=7.1
Pod documented
1755  
1756 1 ▶ die "Spotify URIs are required" unless $uris;
Branch: 1 branch
BranchTrueFalseTotal
unless $uris 0 1 1
1757  
1758 1 return $self->send_put_request(
1759 {
1760 method => 'save_library_items',
1761 params => { uris => _uris_param($uris) },
1762 client_auth_required => 1
1763 }
1764 );
1765 }
1766  
1767 sub remove_library_items {
1768 1 ▶ my ( $self, $uris ) = @_;
Subroutine remove_library_items: called CC=2 SCAR=7.1
Pod documented
1769  
1770 1 ▶ die "Spotify URIs are required" unless $uris;
Branch: 1 branch
BranchTrueFalseTotal
unless $uris 0 1 1
1771  
1772 1 return $self->send_delete_request(
1773 {
1774 method => 'remove_library_items',
1775 params => { uris => _uris_param($uris) },
1776 client_auth_required => 1
1777 }
1778 );
1779 }
1780  
1781 sub check_library_items {
1782 1 ▶ my ( $self, $uris ) = @_;
Subroutine check_library_items: called CC=2 SCAR=7.1
Pod documented
1783  
1784 1 ▶ die "Spotify URIs are required" unless $uris;
Branch: 1 branch
BranchTrueFalseTotal
unless $uris 0 1 1
1785  
1786 1 return $self->send_get_request(
1787 {
1788 method => 'check_library_items',
1789 params => { uris => _uris_param($uris) },
1790 client_auth_required => 1
1791 }
1792 );
1793 }
1794  
1795 sub get_audiobook {
1796 0 ▶ my ( $self, $id, $market ) = @_;
Subroutine get_audiobook: not called CC=3 SCAR=24.8
Pod documented
1797  
1798 0 ▶ die "Audiobook ID is required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
1799  
1800 0 my $params = { id => $id };
1801 0 ▶ $params->{market} = $market if $market;
Branch: 1 branch
BranchTrueFalseTotal
if $market 0 0 0
1802  
1803 0 return $self->send_get_request(
1804 {
1805 method => 'get_audiobook',
1806 params => $params,
1807 client_auth_required => 1
1808 }
1809 );
1810 }
1811  
1812 sub get_several_audiobooks {
1813 0 ▶ my ( $self, $ids, $market ) = @_;
Subroutine get_several_audiobooks: not called CC=4 SCAR=30.0
Pod documented
1814  
1815 0 ▶ die "Audiobook IDs are required" unless $ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids 0 0 0
1816  
1817 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
1818  
1819 0 my $params = { ids => $id_list };
1820 0 ▶ $params->{market} = $market if $market;
Branch: 1 branch
BranchTrueFalseTotal
if $market 0 0 0
1821  
1822 0 return $self->send_get_request(
1823 {
1824 method => 'get_several_audiobooks',
1825 params => $params,
1826 client_auth_required => 1
1827 }
1828 );
1829 }
1830  
1831 sub get_audiobook_chapters {
1832 0 ▶ my ( $self, $id, %params ) = @_;
Subroutine get_audiobook_chapters: not called CC=2 SCAR=17.9
Pod documented
1833  
1834 0 ▶ die "Audiobook ID is required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
1835  
1836 0 $params{id} = $id;
1837  
1838 0 return $self->send_get_request(
1839 {
1840 method => 'get_audiobook_chapters',
1841 params => \%params,
1842 client_auth_required => 1
1843 }
1844 );
1845 }
1846  
1847 sub get_users_saved_audiobooks {
1848 0 ▶ my ( $self, $limit, $offset ) = @_;
Subroutine get_users_saved_audiobooks: not called CC=3 SCAR=24.8
Pod documented
1849  
1850 0 my $params = {};
1851 0 ▶ $params->{limit} = $limit if $limit;
Branch: 1 branch
BranchTrueFalseTotal
if $limit 0 0 0
1852 0 ▶ $params->{offset} = $offset if defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
if defined $offset 0 0 0
1853  
1854 0 return $self->send_get_request(
1855 {
1856 method => 'get_users_saved_audiobooks',
1857 params => $params,
1858 client_auth_required => 1
1859 }
1860 );
1861 }
1862  
1863 sub save_audiobooks_for_current_user {
1864 0 ▶ my ( $self, $ids ) = @_;
Subroutine save_audiobooks_for_current_user: not called CC=3 SCAR=24.8
Pod documented
1865  
1866 0 ▶ die "Audiobook IDs are required" unless $ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids 0 0 0
1867  
1868 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
1869  
1870 0 return $self->send_put_request(
1871 {
1872 method => 'save_audiobooks_for_current_user',
1873 params => { ids => $id_list },
1874 client_auth_required => 1
1875 }
1876 );
1877 }
1878  
1879 sub remove_users_saved_audiobooks {
1880 0 ▶ my ( $self, $ids ) = @_;
Subroutine remove_users_saved_audiobooks: not called CC=3 SCAR=24.8
Pod documented
1881  
1882 0 ▶ die "Audiobook IDs are required" unless $ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids 0 0 0
1883  
1884 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
1885  
1886 0 return $self->send_delete_request(
1887 {
1888 method => 'remove_users_saved_audiobooks',
1889 params => { ids => $id_list },
1890 client_auth_required => 1
1891 }
1892 );
1893 }
1894  
1895 sub check_users_saved_audiobooks {
1896 0 ▶ my ( $self, $ids ) = @_;
Subroutine check_users_saved_audiobooks: not called CC=3 SCAR=24.8
Pod documented
1897  
1898 0 ▶ die "Audiobook IDs are required" unless $ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids 0 0 0
1899  
1900 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
1901  
1902 0 return $self->send_get_request(
1903 {
1904 method => 'check_users_saved_audiobooks',
1905 params => { ids => $id_list },
1906 client_auth_required => 1
1907 }
1908 );
1909 }
1910  
1911 sub get_users_saved_shows {
1912 0 ▶ my ( $self, %params ) = @_;
Subroutine get_users_saved_shows: not called CC=1 SCAR=6.9
Pod documented
1913  
1914 0 return $self->send_get_request(
1915 {
1916 method => 'get_users_saved_shows',
1917 params => \%params,
1918 client_auth_required => 1
1919 }
1920 );
1921 }
1922  
1923 sub save_shows_for_current_user {
1924 0 ▶ my ( $self, $ids ) = @_;
Subroutine save_shows_for_current_user: not called CC=3 SCAR=24.8
Pod documented
1925  
1926 0 ▶ die "Show IDs are required" unless $ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids 0 0 0
1927  
1928 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
1929  
1930 0 return $self->send_put_request(
1931 {
1932 method => 'save_shows_for_current_user',
1933 params => { ids => $id_list },
1934 client_auth_required => 1
1935 }
1936 );
1937 }
1938  
1939 sub check_users_saved_shows {
1940 0 ▶ my ( $self, $ids ) = @_;
Subroutine check_users_saved_shows: not called CC=3 SCAR=24.8
Pod documented
1941  
1942 0 ▶ die "Show IDs are required" unless $ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids 0 0 0
1943  
1944 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
1945  
1946 0 return $self->send_get_request(
1947 {
1948 method => 'check_users_saved_shows',
1949 params => { ids => $id_list },
1950 client_auth_required => 1
1951 }
1952 );
1953 }
1954  
1955 sub get_categories {
1956 0 ▶ my ( $self, %params ) = @_;
Subroutine get_categories: not called CC=1 SCAR=6.9
Pod documented
1957  
1958 0 return $self->send_get_request(
1959 {
1960 method => 'get_categories',
1961 params => \%params,
1962 client_auth_required => 1
1963 }
1964 );
1965 }
1966  
1967 sub get_category {
1968 0 ▶ my ( $self, $category_id, %params ) = @_;
Subroutine get_category: not called CC=2 SCAR=17.9
Pod documented
1969  
1970 0 ▶ die "Category ID is required" unless $category_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $category_id 0 0 0
1971  
1972 0 $params{category_id} = $category_id;
1973  
1974 0 return $self->send_get_request(
1975 {
1976 method => 'get_category',
1977 params => \%params,
1978 client_auth_required => 1
1979 }
1980 );
1981 }
1982  
1983 sub get_chapter {
1984 0 ▶ my ( $self, $id, %params ) = @_;
Subroutine get_chapter: not called CC=2 SCAR=17.9
Pod documented
1985  
1986 0 ▶ die "Chapter ID is required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
1987  
1988 0 $params{id} = $id;
1989  
1990 0 return $self->send_get_request(
1991 {
1992 method => 'get_chapter',
1993 params => \%params,
1994 client_auth_required => 1
1995 }
1996 );
1997 }
1998  
1999 sub get_several_chapters {
2000 0 ▶ my ( $self, $ids, %params ) = @_;
Subroutine get_several_chapters: not called CC=3 SCAR=24.8
Pod documented
2001  
2002 0 ▶ die "Chapter IDs are required" unless $ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids 0 0 0
2003  
2004 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
2005  
2006 0 $params{ids} = $id_list;
2007  
2008 0 return $self->send_get_request(
2009 {
2010 method => 'get_several_chapters',
2011 params => \%params,
2012 client_auth_required => 1
2013 }
2014 );
2015 }
2016  
2017 sub get_available_genre_seeds {
2018 0 ▶ my ($self) = @_;
Subroutine get_available_genre_seeds: not called CC=1 SCAR=6.9
Pod documented
2019  
2020 0 return $self->send_get_request(
2021 {
2022 method => 'get_available_genre_seeds',
2023 client_auth_required => 1
2024 }
2025 );
2026 }
2027  
2028 sub get_available_markets {
2029 0 ▶ my ($self) = @_;
Subroutine get_available_markets: not called CC=1 SCAR=6.9
Pod documented
2030  
2031 0 return $self->send_get_request(
2032 {
2033 method => 'get_available_markets',
2034 client_auth_required => 1
2035 }
2036 );
2037 }
2038  
2039 sub get_show {
2040 0 ▶ my ( $self, $id, $market ) = @_;
Subroutine get_show: not called CC=3 SCAR=24.8
Pod documented
2041  
2042 0 ▶ die "Show ID is required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
2043  
2044 0 my $params = { id => $id };
2045 0 ▶ $params->{market} = $market if $market;
Branch: 1 branch
BranchTrueFalseTotal
if $market 0 0 0
2046  
2047 0 return $self->send_get_request(
2048 {
2049 method => 'get_show',
2050 params => $params,
2051 client_auth_required => 1
2052 }
2053 );
2054 }
2055  
2056 sub get_several_shows {
2057 0 ▶ my ( $self, $ids, $market ) = @_;
Subroutine get_several_shows: not called CC=4 SCAR=30.0
Pod documented
2058  
2059 0 ▶ die "Show IDs are required" unless $ids;
Branch: 1 branch
BranchTrueFalseTotal
unless $ids 0 0 0
2060  
2061 0 ▶ my $id_list = ref($ids) eq 'ARRAY' ? join( ',', @$ids ) : $ids;
Branch: 1 branch
BranchTrueFalseTotal
ref $ids eq 'ARRAY' ? : 0 0 0
2062  
2063 0 my $params = { ids => $id_list };
2064 0 ▶ $params->{market} = $market if $market;
Branch: 1 branch
BranchTrueFalseTotal
if $market 0 0 0
2065  
2066 0 return $self->send_get_request(
2067 {
2068 method => 'get_several_shows',
2069 params => $params,
2070 client_auth_required => 1
2071 }
2072 );
2073 }
2074  
2075 sub get_show_episodes {
2076 0 ▶ my ( $self, $id, %params ) = @_;
Subroutine get_show_episodes: not called CC=2 SCAR=17.9
Pod documented
2077  
2078 0 ▶ die "Show ID is required" unless $id;
Branch: 1 branch
BranchTrueFalseTotal
unless $id 0 0 0
2079  
2080 0 $params{id} = $id;
2081  
2082 0 return $self->send_get_request(
2083 {
2084 method => 'get_show_episodes',
2085 params => \%params,
2086 client_auth_required => 1
2087 }
2088 );
2089 }
2090  
2091 6 1;
2092  
2093 =pod
2094  
2095 =encoding UTF-8
2096  
2097 =head1 NAME
2098  
2099 WWW::Spotify - Spotify Web API Wrapper
2100  
2101 =head1 VERSION
2102  
2103 version 0.016
2104  
2105 =head1 SYNOPSIS
2106  
2107 use WWW::Spotify ();
2108  
2109 my $spotify = WWW::Spotify->new();
2110  
2111 my $result;
2112  
2113 $result = $spotify->album('0sNOF9WDwhWunNAHPD3Baj');
2114  
2115 # $result is a json structure, you can operate on it directly
2116 # or you can use the "get" method see below
2117  
2118 $result = $spotify->albums( '41MnTivkwTO3UUJ8DrqEJJ,6JWc4iAiJ9FjyK0B59ABb4,6UXCm6bOO4gFlDQZV5yL37' );
2119  
2120 $result = $spotify->albums_tracks( '6akEvsycLGftJxYudPjmqK',
2121 {
2122 limit => 1,
2123 offset => 1
2124  
2125 }
2126 );
2127  
2128 $result = $spotify->artist( '0LcJLqbBmaGUft1e9Mm8HV' );
2129  
2130 my $artists_multiple = '0oSGxfWSnnOXhD2fKuz2Gy,3dBVyJ7JuOMt4GE9607Qin';
2131  
2132 $result = $spotify->artists( $artists_multiple );
2133  
2134 $result = $spotify->artist_albums( '1vCWHaC5f2uS3yhpwWbIA6' ,
2135 { album_type => 'single',
2136 # country => 'US',
2137 limit => 2,
2138 offset => 0
2139 } );
2140  
2141 $result = $spotify->track( '0eGsygTp906u18L0Oimnem' );
2142  
2143 $result = $spotify->tracks( '0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G' );
2144  
2145 $result = $spotify->artist_top_tracks( '43ZHCT0cAZBISjO8DG9PnE', # artist id
2146 'SE' # country
2147 );
2148  
2149 $result = $spotify->search(
2150 'tania bowra' ,
2151 'artist' ,
2152 { limit => 15 , offset => 0 }
2153 );
2154  
2155 $result = $spotify->user( 'glennpmcdonald' );
2156  
2157 # public play interaction example
2158 # NEED TO SET YOUR o_auth client_id and secret for these to work
2159  
2160 $spotify->browse_featured_playlists( country => 'US' );
2161  
2162 my $link = $spotify->get('playlists.items[*].href');
2163  
2164 # $link is an arrayfef of the all the playlist urls
2165  
2166 foreach my $playlist (@{$link}) {
2167 # make sure the links look valid
2168 next if $playlist !~ /playlists/;
2169 $spotify->query_full_url($playlist,1);
2170 my $pl_name = $spotify->get('name');
2171 my $tracks = $spotify->get('tracks.items[*].track.id');
2172 foreach my $track (@{$tracks}) {
2173 print "$track\n";
2174 }
2175 }
2176  
2177 =head1 DESCRIPTION
2178  
2179 Wrapper for the Spotify Web API.
2180  
2181 https://developer.spotify.com/web-api/
2182  
2183 Have access to a JSON viewer to help develop and debug. The Chrome JSON viewer is
2184 very good and provides the exact path of the item within the JSON in the lower left
2185 of the screen as you mouse over an element.
2186  
2187 =head1 NAME
2188  
2189 WWW::Spotify - Spotify Web API Wrapper
2190  
2191 =head1 VERSION
2192  
2193 version 0.013
2194  
2195 =head1 CONSTRUCTOR ARGS
2196  
2197 =head2 ua
2198  
2199 You may provide your own user agent object to the constructor. This should be
2200 a L<LWP:UserAgent> or a subclass of it, like L<WWW::Mechanize>. If you are
2201 using L<WWW::Mechanize>, you may want to set autocheck off. To get extra
2202 debugging information, you can do something like this:
2203  
2204 use LWP::ConsoleLogger::Easy qw( debug_ua );
2205 use WWW::Mechanize ();
2206 use WWW::Spotify ();
2207  
2208 my $mech = WWW::Mechanize->new( autocheck => 0 );
2209 debug_ua( $mech );
2210 my $spotify = WWW::Spotify->new( ua => $mech )
2211  
2212 =head1 METHODS
2213  
2214 =head2 auto_json_decode
2215  
2216 When true results will be returned as JSON instead of a perl data structure
2217  
2218 $spotify->auto_json_decode(1);
2219  
2220 =head2 get
2221  
2222 Returns a specific item or array of items from the JSON result of the
2223 last action.
2224  
2225 $result = $spotify->search(
2226 'tania bowra' ,
2227 'artist' ,
2228 { limit => 15 , offset => 0 }
2229 );
2230  
2231 my $image_url = $spotify->get( 'artists.items[0].images[0].url' );
2232  
2233 JSON::Path is the underlying library that actually parses the JSON.
2234  
2235 =head2 query_full_url( $url , [needs o_auth] )
2236  
2237 Results from some calls (playlist for example) return full urls that can be in their entirety. This method allows you
2238 make a call to that url and use all of the o_auth and other features provided.
2239  
2240 $spotify->query_full_url( "https://api.spotify.com/v1/users/spotify/playlists/06U6mm6KPtPIg9D4YGNEnu" , 1 );
2241  
2242 =head2 album
2243  
2244 equivalent to /v1/albums/{id}
2245  
2246 $spotify->album('0sNOF9WDwhWunNAHPD3Baj');
2247  
2248 used album vs albums since it is a singular request
2249  
2250 =head2 albums
2251  
2252 equivalent to /v1/albums?ids={ids}
2253  
2254 $spotify->albums( '41MnTivkwTO3UUJ8DrqEJJ,6JWc4iAiJ9FjyK0B59ABb4,6UXCm6bOO4gFlDQZV5yL37' );
2255  
2256 or
2257  
2258 $spotify->albums( [ '41MnTivkwTO3UUJ8DrqEJJ',
2259 '6JWc4iAiJ9FjyK0B59ABb4',
2260 '6UXCm6bOO4gFlDQZV5yL37' ] );
2261  
2262 =head2 albums_tracks
2263  
2264 equivalent to /v1/albums/{id}/tracks
2265  
2266 $spotify->albums_tracks('6akEvsycLGftJxYudPjmqK',
2267 {
2268 limit => 1,
2269 offset => 1
2270  
2271 }
2272 );
2273  
2274 =head2 artist
2275  
2276 equivalent to /v1/artists/{id}
2277  
2278 $spotify->artist( '0LcJLqbBmaGUft1e9Mm8HV' );
2279  
2280 used artist vs artists since it is a singular request and avoids collision with "artists" method
2281  
2282 =head2 artists
2283  
2284 equivalent to /v1/artists?ids={ids}
2285  
2286 my $artists_multiple = '0oSGxfWSnnOXhD2fKuz2Gy,3dBVyJ7JuOMt4GE9607Qin';
2287  
2288 $spotify->artists( $artists_multiple );
2289  
2290 =head2 artist_albums
2291  
2292 equivalent to /v1/artists/{id}/albums
2293  
2294 $spotify->artist_albums( '1vCWHaC5f2uS3yhpwWbIA6' ,
2295 { album_type => 'single',
2296 # country => 'US',
2297 limit => 2,
2298 offset => 0
2299 } );
2300  
2301 =head2 artist_top_tracks
2302  
2303 equivalent to /v1/artists/{id}/top-tracks
2304  
2305 $spotify->artist_top_tracks( '43ZHCT0cAZBISjO8DG9PnE', # artist id
2306 'SE' # country
2307 );
2308  
2309 =head2 artist_related_artists
2310  
2311 equivalent to /v1/artists/{id}/related-artists
2312  
2313 $spotify->artist_related_artists( '43ZHCT0cAZBISjO8DG9PnE' );
2314  
2315 =head2 search
2316  
2317 equivalent to /v1/search?type=album (etc)
2318  
2319 $spotify->search(
2320 'tania bowra' ,
2321 'artist' ,
2322 { limit => 10 , offset => 0 }
2323 );
2324  
2325 Note: as of the February 2026 API changes the maximum C<limit> is 10
2326 (previously 50); use C<offset> to paginate.
2327  
2328 =head2 track
2329  
2330 equivalent to /v1/tracks/{id}
2331  
2332 $spotify->track( '0eGsygTp906u18L0Oimnem' );
2333  
2334 =head2 tracks
2335  
2336 equivalent to /v1/tracks?ids={ids}
2337  
2338 $spotify->tracks( '0eGsygTp906u18L0Oimnem,1lDWb6b6ieDQ2xT7ewTC3G' );
2339  
2340 =head2 browse_featured_playlists
2341  
2342 equivalent to /v1/browse/featured-playlists
2343  
2344 $spotify->browse_featured_playlists();
2345  
2346 requires OAuth
2347  
2348 =head2 browse_new_releases
2349  
2350 equivalent to /v1/browse/new-releases
2351  
2352 requires OAuth
2353  
2354 $spotify->browse_new_releases
2355  
2356 =head2 force_client_auth
2357  
2358 Boolean
2359  
2360 will pass authentication (OAuth) on all requests when set
2361  
2362 $spotify->force_client_auth(1);
2363  
2364 =head2 user
2365  
2366 equivalent to /v1/users/{user_id}
2367  
2368 $spotify->user('glennpmcdonald');
2369  
2370 =head2 get_playlist
2371  
2372 equivalent to GET /v1/playlists/{playlist_id}
2373  
2374 $spotify->get_playlist('37i9dQZF1DXcBWIGoYBM5M');
2375  
2376 This method retrieves a playlist owned by a Spotify user. The playlist must be public or owned by the authenticated user.
2377  
2378 =head2 get_playlist_items
2379  
2380 equivalent to /v1/playlists/{playlist_id}/items (renamed from /tracks in the
2381 February 2026 API changes)
2382  
2383 $spotify->get_playlist_items('37i9dQZF1DXcBWIGoYBM5M', { limit => 10, offset => 0 });
2384  
2385 =head2 create_playlist
2386  
2387 equivalent to POST /v1/me/playlists (replaced /v1/users/{user_id}/playlists
2388 in the February 2026 API changes) - creates a playlist for the
2389 authenticated user
2390  
2391 $spotify->create_playlist('My New Playlist', 1, 'A description of my playlist');
2392  
2393 =head2 get_current_user_playlists
2394  
2395 equivalent to /v1/me/playlists
2396  
2397 $spotify->get_current_user_playlists({ limit => 20, offset => 0 });
2398  
2399 =head2 add_items_to_playlist
2400  
2401 equivalent to /v1/playlists/{playlist_id}/items (renamed from /tracks in the
2402 February 2026 API changes)
2403  
2404 $spotify->add_items_to_playlist('playlist_id', ['spotify:track:4iV5W9uYEdYUVa79Axb7Rh', 'spotify:track:1301WleyT98MSxVHPZCA6M'], 0);
2405  
2406 =head2 unfollow_playlist
2407  
2408 equivalent to DELETE /v1/playlists/{playlist_id}/followers - removes the
2409 playlist from the authenticated user's library (Spotify has no hard
2410 playlist delete)
2411  
2412 $spotify->unfollow_playlist('playlist_id');
2413  
2414 =head2 remove_user_saved_tracks
2415  
2416 equivalent to /v1/me/tracks
2417  
2418 $spotify->remove_user_saved_tracks(['4iV5W9uYEdYUVa79Axb7Rh', '1301WleyT98MSxVHPZCA6M']);
2419  
2420 =head2 check_users_saved_tracks
2421  
2422 equivalent to /v1/me/tracks/contains
2423  
2424 $spotify->check_users_saved_tracks(['4iV5W9uYEdYUVa79Axb7Rh', '1301WleyT98MSxVHPZCA6M']);
2425  
2426 =head2 check_users_saved_shows
2427  
2428 equivalent to GET /v1/me/shows/contains
2429  
2430 $spotify->check_users_saved_shows(['5CfCWKI5pZ28U0uOzXkDHe', '5as3aKmN2k11yfDDDSrvaZ']);
2431  
2432 or
2433  
2434 $spotify->check_users_saved_shows('5CfCWKI5pZ28U0uOzXkDHe,5as3aKmN2k11yfDDDSrvaZ');
2435  
2436 This method checks if one or more shows are already saved in the current Spotify user's library.
2437  
2438 =head2 get_several_tracks_audio_features
2439  
2440 equivalent to /v1/audio-features
2441  
2442 $spotify->get_several_tracks_audio_features(['4iV5W9uYEdYUVa79Axb7Rh', '1301WleyT98MSxVHPZCA6M']);
2443  
2444 =head2 get_track_audio_features
2445  
2446 equivalent to /v1/audio-features/{id}
2447  
2448 $spotify->get_track_audio_features('4iV5W9uYEdYUVa79Axb7Rh');
2449  
2450 =head2 get_track_audio_analysis
2451  
2452 equivalent to /v1/audio-analysis/{id}
2453  
2454 $spotify->get_track_audio_analysis('4iV5W9uYEdYUVa79Axb7Rh');
2455  
2456 =head2 get_recommendations
2457  
2458 equivalent to /v1/recommendations
2459  
2460 $spotify->get_recommendations(
2461 seed_artists => '4NHQUGzhtTLFvgF5SZesLK',
2462 seed_genres => 'classical,country',
2463 seed_tracks => '0c6xIDDpzE81m2q797ordA',
2464 limit => 10,
2465 market => 'ES'
2466 );
2467  
2468 =head2 get_followed_artists
2469  
2470 equivalent to /v1/me/following
2471  
2472 $spotify->get_followed_artists(
2473 limit => 20,
2474 after => '0I2XqVXqHScXjHhk6AYYRe'
2475 );
2476  
2477 Note: This method always sets the 'type' parameter to 'artist' as it's the only supported value.
2478  
2479 =head2 follow_artists_or_users
2480  
2481 equivalent to PUT /v1/me/following
2482  
2483 $spotify->follow_artists_or_users('artist', ['2CIMQHirSU0MQqyYHq0eOx', '57dN52uHvrHOxijzpIgu3E']);
2484  
2485 or
2486  
2487 $spotify->follow_artists_or_users('user', '2CIMQHirSU0MQqyYHq0eOx,57dN52uHvrHOxijzpIgu3E');
2488  
2489 =head2 unfollow_artists_or_users
2490  
2491 equivalent to DELETE /v1/me/following
2492  
2493 $spotify->unfollow_artists_or_users('artist', ['2CIMQHirSU0MQqyYHq0eOx', '57dN52uHvrHOxijzpIgu3E']);
2494  
2495 or
2496  
2497 $spotify->unfollow_artists_or_users('user', '2CIMQHirSU0MQqyYHq0eOx,57dN52uHvrHOxijzpIgu3E');
2498  
2499 =head2 check_if_user_follows_artists_or_users
2500  
2501 equivalent to GET /v1/me/following/contains
2502  
2503 $spotify->check_if_user_follows_artists_or_users('artist', ['2CIMQHirSU0MQqyYHq0eOx', '57dN52uHvrHOxijzpIgu3E']);
2504  
2505 or
2506  
2507 $spotify->check_if_user_follows_artists_or_users('user', '2CIMQHirSU0MQqyYHq0eOx,57dN52uHvrHOxijzpIgu3E');
2508  
2509 =head2 check_if_user_follows_playlist
2510  
2511 equivalent to GET /v1/playlists/{playlist_id}/followers/contains
2512  
2513 $spotify->check_if_user_follows_playlist('3cEYpjA9oz9GiPac4AsH4n', 'jmperezperez');
2514  
2515 or
2516  
2517 $spotify->check_if_user_follows_playlist('3cEYpjA9oz9GiPac4AsH4n', ['jmperezperez']);
2518  
2519 =head2 save_library_items
2520  
2521 equivalent to PUT /v1/me/library (February 2026 consolidated library
2522 endpoint; replaces the removed PUT /v1/me/tracks, /v1/me/albums,
2523 /v1/me/episodes, /v1/me/shows, /v1/me/audiobooks, /v1/me/following and
2524 /v1/playlists/{id}/followers endpoints)
2525  
2526 Takes Spotify URIs (not bare ids), as a comma-separated string or an
2527 array reference. Maximum 40 URIs.
2528  
2529 $spotify->save_library_items( [ 'spotify:track:7a3LWj5xSFhFRYmztS8wgK',
2530 'spotify:album:4aawyAB9vmqN3uQ7FjRGTy' ] );
2531  
2532 =head2 remove_library_items
2533  
2534 equivalent to DELETE /v1/me/library (February 2026 consolidated library
2535 endpoint; replaces the removed per-type DELETE endpoints)
2536  
2537 $spotify->remove_library_items( 'spotify:track:7a3LWj5xSFhFRYmztS8wgK' );
2538  
2539 =head2 check_library_items
2540  
2541 equivalent to GET /v1/me/library/contains (February 2026 consolidated
2542 library endpoint; replaces the removed per-type */contains endpoints)
2543  
2544 $spotify->check_library_items( [ 'spotify:track:7a3LWj5xSFhFRYmztS8wgK' ] );
2545  
2546 =head2 DEPRECATED METHODS
2547  
2548 Spotify's November 2024 and February 2026 API changes removed or
2549 deprecated a number of endpoints. The corresponding methods are kept
2550 for backwards compatibility but warn once per process when called, and
2551 Spotify will reject the request:
2552  
2553 batch fetch (removed - fetch individually instead): C<albums>, C<artists>,
2554 C<tracks>, C<get_several_shows>, C<get_several_audiobooks>,
2555 C<get_several_chapters>, C<get_several_tracks_audio_features>
2556  
2557 browse/artist (removed): C<browse_featured_playlists>,
2558 C<browse_new_releases>, C<get_categories>, C<get_category>,
2559 C<artist_top_tracks>, C<artist_related_artists>
2560  
2561 audio/recommendations (deprecated): C<get_track_audio_features>,
2562 C<get_track_audio_analysis>, C<get_recommendations>,
2563 C<get_available_genre_seeds>
2564  
2565 library (consolidated into /v1/me/library - use C<save_library_items>,
2566 C<remove_library_items>, C<check_library_items>):
2567 C<remove_user_saved_tracks>, C<check_users_saved_tracks>,
2568 C<save_shows_for_current_user>, C<check_users_saved_shows>,
2569 C<save_audiobooks_for_current_user>, C<remove_users_saved_audiobooks>,
2570 C<check_users_saved_audiobooks>, C<follow_artists_or_users>,
2571 C<unfollow_artists_or_users>, C<check_if_user_follows_artists_or_users>,
2572 C<check_if_user_follows_playlist>
2573  
2574 other: C<user> (GET /v1/users/{user_id} deprecated/removed)
2575  
2576 =head2 get_audiobook
2577  
2578 equivalent to GET /v1/audiobooks/{id}
2579  
2580 $spotify->get_audiobook('7iHfbu1YPACw6oZPAFJtqe');
2581  
2582 or with market parameter:
2583  
2584 $spotify->get_audiobook('7iHfbu1YPACw6oZPAFJtqe', 'US');
2585  
2586 =head2 get_users_saved_audiobooks
2587  
2588 equivalent to GET /v1/me/audiobooks
2589  
2590 $spotify->get_users_saved_audiobooks(20, 0);
2591  
2592 =head2 remove_users_saved_audiobooks
2593  
2594 equivalent to DELETE /v1/me/audiobooks
2595  
2596 $spotify->remove_users_saved_audiobooks(['18yVqkdbdRvS24c0Ilj2ci', '1HGw3J3NxZO1TP1BTtVhpZ']);
2597  
2598 or
2599  
2600 $spotify->remove_users_saved_audiobooks('18yVqkdbdRvS24c0Ilj2ci,1HGw3J3NxZO1TP1BTtVhpZ');
2601  
2602 This method removes one or more audiobooks from the current user's library.
2603  
2604 =head2 get_available_genre_seeds
2605  
2606 equivalent to GET /v1/recommendations/available-genre-seeds
2607  
2608 $spotify->get_available_genre_seeds();
2609  
2610 This method retrieves a list of available genres seed parameter values for recommendations.
2611  
2612 =head2 get_available_markets
2613  
2614 equivalent to GET /v1/markets
2615  
2616 $spotify->get_available_markets();
2617  
2618 This method retrieves the list of markets where Spotify is available.
2619  
2620 =head2 get_show
2621  
2622 equivalent to GET /v1/shows/{id}
2623  
2624 $spotify->get_show('38bS44xjbVVZ3No3ByF1dJ', 'US');
2625  
2626 This method retrieves Spotify catalog information for a single show identified by its unique Spotify ID.
2627  
2628 =head2 get_several_shows
2629  
2630 equivalent to GET /v1/shows
2631  
2632 $spotify->get_several_shows(['5CfCWKI5pZ28U0uOzXkDHe', '5as3aKmN2k11yfDDDSrvaZ'], 'US');
2633  
2634 or
2635  
2636 $spotify->get_several_shows('5CfCWKI5pZ28U0uOzXkDHe,5as3aKmN2k11yfDDDSrvaZ', 'US');
2637  
2638 This method retrieves Spotify catalog information for several shows based on their Spotify IDs.
2639  
2640 =head2 get_show_episodes
2641  
2642 equivalent to GET /v1/shows/{id}/episodes
2643  
2644 $spotify->get_show_episodes('38bS44xjbVVZ3No3ByF1dJ', market => 'US', limit => 10, offset => 5);
2645  
2646 This method retrieves Spotify catalog information about a show's episodes. Optional parameters can be used to limit the number of episodes returned.
2647  
2648 =head2 get_audiobook_chapters
2649  
2650 equivalent to GET /v1/audiobooks/{id}/chapters
2651  
2652 $spotify->get_audiobook_chapters('3ZXb8FKZGU0EHALYX6uCzU', market => 'US', limit => 50, offset => 0);
2653  
2654 This method retrieves the chapters of an audiobook.
2655  
2656 =head2 get_several_audiobooks
2657  
2658 equivalent to GET /v1/audiobooks
2659  
2660 $spotify->get_several_audiobooks(['18yVqkdbdRvS24c0Ilj2ci', '1HGw3J3NxZO1TP1BTtVhpZ'], 'US');
2661  
2662 or
2663  
2664 $spotify->get_several_audiobooks('18yVqkdbdRvS24c0Ilj2ci,1HGw3J3NxZO1TP1BTtVhpZ', 'US');
2665  
2666 This method retrieves multiple audiobooks based on their Spotify IDs.
2667  
2668 =head2 send_delete_request
2669  
2670 Internal method used to send DELETE requests to the Spotify API.
2671  
2672 =head2 send_put_request
2673  
2674 Internal method used to send PUT requests to the Spotify API.
2675  
2676 =head2 check_users_saved_audiobooks
2677  
2678 equivalent to GET /v1/me/audiobooks/contains
2679  
2680 $spotify->check_users_saved_audiobooks(['18yVqkdbdRvS24c0Ilj2ci', '1HGw3J3NxZO1TP1BTtVhpZ']);
2681  
2682 or
2683  
2684 $spotify->check_users_saved_audiobooks('18yVqkdbdRvS24c0Ilj2ci,1HGw3J3NxZO1TP1BTtVhpZ');
2685  
2686 =head2 get_users_saved_shows
2687  
2688 equivalent to GET /v1/me/shows
2689  
2690 $spotify->get_users_saved_shows(limit => 20, offset => 0);
2691  
2692 This method retrieves a list of shows saved in the current Spotify user's library. Optional parameters can be used to limit the number of shows returned.
2693  
2694 =head2 save_shows_for_current_user
2695  
2696 equivalent to PUT /v1/me/shows
2697  
2698 $spotify->save_shows_for_current_user(['5CfCWKI5pZ28U0uOzXkDHe', '5as3aKmN2k11yfDDDSrvaZ']);
2699  
2700 or
2701  
2702 $spotify->save_shows_for_current_user('5CfCWKI5pZ28U0uOzXkDHe,5as3aKmN2k11yfDDDSrvaZ');
2703  
2704 This method saves one or more shows to the current user's library.
2705  
2706 =head2 get_categories
2707  
2708 equivalent to GET /v1/browse/categories
2709  
2710 $spotify->get_categories(
2711 country => 'US',
2712 locale => 'en_US',
2713 limit => 20,
2714 offset => 0
2715 );
2716  
2717 =head2 get_category
2718  
2719 equivalent to GET /v1/browse/categories/{category_id}
2720  
2721 $spotify->get_category('dinner', locale => 'en_US');
2722  
2723 =head2 get_chapter
2724  
2725 equivalent to GET /v1/chapters/{id}
2726  
2727 $spotify->get_chapter('0D5wENdkdwbqlrHoaJ9g29', market => 'US');
2728  
2729 =head2 get_several_chapters
2730  
2731 equivalent to GET /v1/chapters
2732  
2733 $spotify->get_several_chapters(['0IsXVP0JmcB2adSE338GkK', '3ZXb8FKZGU0EHALYX6uCzU', '0D5wENdkdwbqlrHoaJ9g29'], market => 'US');
2734  
2735 or
2736  
2737 $spotify->get_several_chapters('0IsXVP0JmcB2adSE338GkK,3ZXb8FKZGU0EHALYX6uCzU,0D5wENdkdwbqlrHoaJ9g29', market => 'US');
2738  
2739 =head2 save_audiobooks_for_current_user
2740  
2741 equivalent to PUT /v1/me/audiobooks
2742  
2743 $spotify->save_audiobooks_for_current_user(['18yVqkdbdRvS24c0Ilj2ci', '1HGw3J3NxZO1TP1BTtVhpZ']);
2744  
2745 or
2746  
2747 $spotify->save_audiobooks_for_current_user('18yVqkdbdRvS24c0Ilj2ci,1HGw3J3NxZO1TP1BTtVhpZ');
2748  
2749 This method saves one or more audiobooks to the current user's library.
2750  
2751 =head2 oauth_client_id
2752  
2753 needed for requests that require OAuth, see Spotify API documentation for more information
2754  
2755 $spotify->oauth_client_id('2xfjijkcjidjkfdi');
2756  
2757 Can also be set via environment variable, SPOTIFY_CLIENT_ID
2758  
2759 =head2 oauth_client_secret
2760  
2761 needed for requests that require OAuth, see Spotify API documentation for more information
2762  
2763 $spotify->oauth_client_secret('2xfjijkcjidjkfdi');
2764  
2765 Can also be set via environment variable, SPOTIFY_CLIENT_SECRET
2766  
2767 =head2 authorize_url
2768  
2769 builds the URL to send a user to for the OAuth authorization-code flow.
2770 Uses C<oauth_client_id> and C<oauth_redirect_uri>; C<scope> and C<state>
2771 are optional
2772  
2773 my $url = $spotify->authorize_url({
2774 scope => 'user-read-private playlist-modify-private',
2775 state => $random_string,
2776 });
2777  
2778 Open the URL in a browser; after login Spotify redirects to
2779 C<oauth_redirect_uri> with a C<code> query parameter.
2780  
2781 =head2 get_access_token
2782  
2783 exchanges an authorization code (from the C<authorize_url> redirect) for
2784 a user access token. On success stores C<current_access_token>,
2785 C<refresh_token>, and C<token_expires_at>, and returns true
2786  
2787 $spotify->get_access_token($code);
2788  
2789 =head2 refresh_access_token
2790  
2791 fetches a new access token using the stored C<refresh_token> (set by
2792 C<get_access_token>). Dies if no refresh token is stored; returns true
2793 on success
2794  
2795 $spotify->refresh_access_token();
2796  
2797 =head2 refresh_token
2798  
2799 the OAuth refresh token, set automatically by C<get_access_token>. Can
2800 be set manually to restore a persisted session
2801  
2802 $spotify->refresh_token($saved_refresh_token);
2803  
2804 =head2 response_status
2805  
2806 returns the response code for the last request made
2807  
2808 my $status = $spotify->response_status();
2809  
2810 =head2 response_content_type
2811  
2812 returns the response type for the last request made, helpful to verify JSON
2813  
2814 my $content_type = $spotify->response_content_type();
2815  
2816 =head2 custom_request_handler
2817  
2818 pass a callback subroutine to this method that will be run at the end of the
2819 request prior to die_on_response_error, if enabled
2820  
2821 # $m is the WWW::Mechanize object
2822 $spotify->custom_request_handler(
2823 sub { my $m = shift;
2824 if ($m->status() == 401) {
2825 return 1;
2826 }
2827 }
2828 );
2829  
2830 =head2 custom_request_handler_result
2831  
2832 returns the result of the most recent execution of the custom_request_handler callback
2833 this allows you to determine the success/failure criteria of your callback
2834  
2835 my $callback_result = $spotify->custom_request_handler_result();
2836  
2837 =head2 die_on_response_error
2838  
2839 Boolean - default 0
2840  
2841 added to provide minimal automated checking of responses
2842  
2843 $spotify->die_on_response_error(1);
2844  
2845 eval {
2846 # run assuming you do NOT have proper authentication setup
2847 $result = $spotify->album('0sNOF9WDwhWunNAHPD3Baj');
2848 };
2849  
2850 if ($@) {
2851 warn $spotify->last_error();
2852 }
2853  
2854 =head2 last_error
2855  
2856 returns last_error (if applicable) from the most recent request.
2857 reset to empty string on each request
2858  
2859 print $spotify->last_error() , "\n";
2860  
2861 =head1 THANKS
2862  
2863 Paul Lamere at The Echo Nest / Spotify
2864  
2865 All the great Perl community members that keep Perl fun
2866  
2867 Olaf Alders for all his help and support in maintaining this module
2868  
2869 =head1 AUTHOR
2870  
2871 Aaron Johnson <aaronjjohnson@gmail.com>
2872  
2873 =head1 COPYRIGHT AND LICENSE
2874  
2875 This software is copyright (c) 2024 by Aaron Johnson.
2876  
2877 This is free software; you can redistribute it and/or modify it under
2878 the same terms as the Perl 5 programming language system itself.
2879  
2880 =cut
2881  
2882 __END__
↑ Summary