spotify 0.2.1

spotify-dart Pub Build Status

A dart library for interfacing with the Spotify API.

Usage #

A simple usage example:

import 'package:spotify/spotify_io.dart';

main() async {
  var credentials = new SpotifyApiCredentials(clientId, clientSecret);
  var spotify = new SpotifyApi(credentials);

  var artist = await spotify.artists.get('0OdUWJ0sBjDrqHygGUXeCF');
}

For usage in the browser use #

import 'package:spotify/spotify_browser.dart';

Client Credentials Flow #

SpotifyApi getSpotifyApi() {
  final credentials = new SpotifyApiCredentials(clientId, clientSecret);
  return new SpotifyApi(credentials);
}

Authorization Code #

SpotifyApi getSpotifyApi() async {
  final credentials = new SpotifyApiCredentials(clientId, clientSecret);
  grant = SpotifyApi.authorizationCodeGrant(credentials);
  // *** from dart-lang/oauth2 package
  // `redirect` is an imaginary function that redirects the resource
  // owner's browser.
  await redirect(grant.getAuthorizationUrl(redirectUrl));
  // Another imaginary function that listens for a request to `redirectUrl`.
  final requestUri = await listen(redirectUrl);
  // ***
  return SpotifyApi.fromAuthCodeGrant(WelcomeScreen.grant, requestUri);
}

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Development #

Generating JSON Serializers #

Run pub run build_runner build to generate JSON serilizers via json_serializable.

Changelog #

0.2.1 #

  • add oauth2 package for refresh flow
  • fix pagination on several places
  • add playlists/removeTrack
  • add playlists/addTrack
  • add createPlaylist
  • fix my playlists
  • refactor SpotifyApi classes
  • upgrade json_serializable dependencies

0.2.0 #

  • add token setter method for implicit grant
  • add related artists
  • add recently played artists
  • add top tracks
  • add categories
  • add tracksByPlaylist
  • fix currently playing

Please don't mind the weird commit history, I goofed and used rebase first and merge on the later pull requests.

0.1.6+3 #

  • fix typo in tracks object
  • move album model switch to model

0.1.5+1 #

  • add external_ids and external_urls

0.1.4 #

  • add related-artists to the artists endpoint

0.1.3+2 #

  • use full Track object in search endpoint

0.1.3+1 #

  • eliminate duplicate code (serializers)

0.1.3 #

  • add currently playing endpoint

0.1.2 #

  • make pub analyzer happy

0.1.1 #

  • add search endpoint

0.0.9 - 0.1.0 #

  • support Dart 2 with switch to json_serializable

0.0.8 #

  • add missing _putImpl implementation to SpotifyMock

0.0.7 #

  • add user's saved tracks endpoints

0.0.6 #

  • add pagination support

0.0.5 #

  • add user and playlist endpoints

0.0.4 #

  • fix typo in artists endpoint
  • add basic unit tests

0.0.3 #

  • add support for albums and artists
  • refactor code a little

0.0.1 #

  • Initial version

example/example.dart

// Copyright (c) 2017, rinukkusu. All rights reserved. Use of this source code
// is governed by a BSD-style license that can be found in the LICENSE file.

import 'dart:io';
import 'dart:convert';
import '../lib/spotify_io.dart';

main() async {
  var keyJson = await new File('example/.apikeys').readAsString();
  var keyMap = json.decode(keyJson);

  var credentials = new SpotifyApiCredentials(keyMap['id'], keyMap['secret']);
  var spotify = SpotifyApi(credentials);

  print("Artists:");
  var artists = await spotify.artists.list(['0OdUWJ0sBjDrqHygGUXeCF']);
  artists.forEach((x) => print(x.name));

  print("\nAlbum:");
  var album = await spotify.albums.get('2Hog1V8mdTWKhCYqI5paph');
  print(album.name);

  print("\nAlbum Tracks:");
  var tracks = await spotify.albums.getTracks(album.id).all();
  tracks.forEach((track) {
    print(track.name);
  });

  print("\nFeatured Playlist:");
  var featuredPlaylists = await spotify.playlists.featured.all();
  featuredPlaylists.forEach((playlist) {
    print(playlist.name);
  });

  print("\nSearching for \'Metallica\':");
  var search = await spotify.search
      .get("metallica")
      .first(2)
      .catchError((err) => print((err as SpotifyException).message));
  if (search == null) {
    return;
  }
  search.forEach((pages) {
    pages.items.forEach((item) {
      if (item is PlaylistSimple) {
        print('Playlist: \n'
            'id: ${item.id}\n'
            'name: ${item.name}:\n'
            'collaborative: ${item.collaborative}\n'
            'href: ${item.href}\n'
            'trackslink: ${item.tracksLink.href}\n'
            'owner: ${item.owner}\n'
            'public: ${item.owner}\n'
            'snapshotId: ${item.snapshotId}\n'
            'type: ${item.type}\n'
            'uri: ${item.uri}\n'
            'images: ${item.images.length}\n'
            '-------------------------------');
      }
      if (item is Artist) {
        print('Artist: \n'
            'id: ${item.id}\n'
            'name: ${item.name}\n'
            'href: ${item.href}\n'
            'type: ${item.type}\n'
            'uri: ${item.uri}\n'
            '-------------------------------');
      }
      if (item is TrackSimple) {
        print('Track:\n'
            'id: ${item.id}\n'
            'name: ${item.name}\n'
            'href: ${item.href}\n'
            'type: ${item.type}\n'
            'uri: ${item.uri}\n'
            'isPlayable: ${item.isPlayable}\n'
            'artists: ${item.artists.length}\n'
            'availableMarkets: ${item.availableMarkets.length}\n'
            'discNumber: ${item.discNumber}\n'
            'trackNumber: ${item.trackNumber}\n'
            'explicit: ${item.explicit}\n'
            '-------------------------------');
      }
      if (item is AlbumSimple) {
        print('Album:\n'
            'id: ${item.id}\n'
            'name: ${item.name}\n'
            'href: ${item.href}\n'
            'type: ${item.type}\n'
            'uri: ${item.uri}\n'
            'albumType: ${item.albumType}\n'
            'artists: ${item.artists.length}\n'
            'availableMarkets: ${item.availableMarkets.length}\n'
            'images: ${item.images.length}\n'
            '-------------------------------');
      }
    });
  });

  var relatedArtists = await spotify.artists.relatedArtists('0OdUWJ0sBjDrqHygGUXeCF');
  print('related Artists: ${relatedArtists.length}');
  exit(0);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  spotify: ^0.2.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:spotify/spotify_browser.dart';
import 'package:spotify/spotify_io.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
76
Health:
Code health derived from static analysis. [more]
75
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
77
Learn more about scoring.

We analyzed this package on Feb 17, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health suggestions

Fix lib/src/models/track.dart. (-11.78 points)

Analysis of lib/src/models/track.dart reported 25 hints, including:

line 8 col 11: Use ; instead of {} for empty constructor bodies.

line 11 col 3: Prefer using /// for doc comments.

line 17 col 3: Prefer using /// for doc comments.

line 23 col 3: Prefer using /// for doc comments.

line 30 col 3: Prefer using /// for doc comments.

Fix lib/src/endpoints/endpoint_paging.dart. (-8.63 points)

Analysis of lib/src/endpoints/endpoint_paging.dart reported 18 hints, including:

line 10 col 12: Don't explicitly initialize variables to null.

line 11 col 11: Don't explicitly initialize variables to null.

line 12 col 7: Unnecessary new keyword.

line 16 col 12: Don't explicitly initialize variables to null.

line 17 col 11: Don't explicitly initialize variables to null.

Fix lib/src/models/audio_feature.dart. (-7.24 points)

Analysis of lib/src/models/audio_feature.dart reported 15 hints, including:

line 8 col 18: Use ; instead of {} for empty constructor bodies.

line 12 col 3: Prefer using /// for doc comments.

line 18 col 3: Prefer using /// for doc comments.

line 25 col 3: Prefer using /// for doc comments.

line 39 col 28: Unnecessary new keyword.

Fix additional 20 files with analysis or formatting issues. (-23.29 points)

Additional issues in the following files:

  • lib/src/models/album.dart (10 hints)
  • lib/src/spotify_base.dart (8 hints)
  • lib/src/models/artist.dart (4 hints)
  • lib/src/models/playlist.dart (4 hints)
  • lib/src/spotify_mock.dart (4 hints)
  • lib/src/endpoints/endpoint_base.dart (2 hints)
  • lib/src/models/external_objects.dart (2 hints)
  • lib/src/models/followers.dart (2 hints)
  • lib/src/models/paging.dart (2 hints)
  • lib/src/models/user.dart (2 hints)
  • lib/src/endpoints/playlists.dart (1 hint)
  • lib/src/endpoints/tracks.dart (1 hint)
  • lib/src/endpoints/users.dart (1 hint)
  • lib/src/models/category.dart (1 hint)
  • lib/src/models/error.dart (1 hint)
  • lib/src/models/image.dart (1 hint)
  • lib/src/models/player.dart (1 hint)
  • lib/src/endpoints/artists.dart (Run dartfmt to format lib/src/endpoints/artists.dart.)
  • lib/src/spotify_browser.dart (Run dartfmt to format lib/src/spotify_browser.dart.)
  • lib/src/spotify_io.dart (Run dartfmt to format lib/src/spotify_io.dart.)

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.0 <3.0.0
http ^0.12.0 0.12.0+4
json_annotation ^3.0.1 3.0.1
oauth2 ^1.5.0 1.5.0
Transitive dependencies
async 2.4.0
charcode 1.1.3
collection 1.14.12
http_parser 3.1.3
meta 1.1.8
path 1.6.4
pedantic 1.9.0
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
build_runner ^1.1.1
json_serializable ^3.2.5
test ^1.5.1