Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Market integration #164

Merged
merged 3 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/src/endpoints/artists.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ class Artists extends EndpointPaging {

/// Returns related artists based on the artist with its [artistId]
@Deprecated('Use relatedArtists instead')
Future<Iterable<Artist>> getRelatedArtists(String artistId) async => relatedArtists(artistId);
Future<Iterable<Artist>> getRelatedArtists(String artistId) async =>
relatedArtists(artistId);

/// Retrieves multiple artists with [artistIds]
Future<Iterable<Artist>> list(Iterable<String> artistIds) async {
Expand Down Expand Up @@ -63,13 +64,14 @@ class Artists extends EndpointPaging {
/// the album is available!
Pages<Album> albums(
String artistId, {
String? country,
Market? country,
List<String>? includeGroups,
}) {
final _includeGroups =
includeGroups == null ? null : includeGroups.join(',');
final query =
_buildQuery({'include_groups': _includeGroups, 'country': country});
final _includeGroups = includeGroups?.join(',');
final query = _buildQuery({
'include_groups': _includeGroups,
'country': country?.name,
});
return _getPages(
'$_path/$artistId/albums?$query', (json) => Album.fromJson(json));
}
Expand Down
16 changes: 9 additions & 7 deletions lib/src/endpoints/episodes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ class Episodes extends EndpointBase {

Episodes(SpotifyApiBase api) : super(api);

Future<EpisodeFull> get(String id, [String? market]) async {
Future<EpisodeFull> get(String id, [Market? market]) async {
assert(id.isNotEmpty, 'No episode id was provided');
var jsonString =
await _api._get('$_path/$id?' + _buildQuery({'market': market}));
var jsonString = await _api
._get('$_path/$id?' + _buildQuery({'market': market?.name}));
return EpisodeFull.fromJson(jsonDecode(jsonString));
}

Future<Iterable<EpisodeFull>> list(List<String> ids,
[String? market]) async {
Future<Iterable<EpisodeFull>> list(List<String> ids, [Market? market]) async {
assert(ids.isNotEmpty, 'No episode id\'s were provided');
var jsonString = await _api._get(
'$_path?' + _buildQuery({'ids': ids.join(','), 'market': market}));
var jsonString = await _api._get('$_path?' +
_buildQuery({
'ids': ids.join(','),
'market': market?.name,
}));
var episodesJson = jsonDecode(jsonString)['episodes'] as Iterable<dynamic>;
return episodesJson.map((json) => EpisodeFull.fromJson(json));
}
Expand Down
13 changes: 8 additions & 5 deletions lib/src/endpoints/me.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ class Me extends _MeEndpointBase {
/// Use [state] to toggle the shuffle. `true` to turn shuffle on and `false`
/// to turn it off respectively.
/// Returns the current player state by making another request.
/// See [player(String market)];
/// See [player];
@Deprecated('Use [spotify.player.shuffle()]')
Future<PlaybackState?> shuffle(bool state, [String? deviceId]) async =>
_player.shuffle(state, deviceId: deviceId);

@Deprecated('Use [spotify.player.playbackState()]')
Future<PlaybackState> player([String? market]) async =>
_player.playbackState(market);
_player.playbackState(Market.values.asNameMap()[market]);

/// Get the current user's top tracks, spanning over a [timeRange].
/// The [timeRange]'s default is [TimeRange.mediumTerm].
Expand Down Expand Up @@ -167,10 +167,13 @@ class Me extends _MeEndpointBase {
/// [ids] - the ids of the shows to remove
/// [market] - An ISO 3166-1 alpha-2 country code. If a country code is
/// specified, only content that is available in that market will be returned.
Future<void> removeShows(List<String> ids, [String market = '']) async {
Future<void> removeShows(List<String> ids, [Market? market]) async {
assert(ids.isNotEmpty, 'No show ids were provided for removing');
var query = _buildQuery({'ids': ids.join(','), 'market': market});
await _api._delete('$_path/shows?' + query);
var queryMap = {
'ids': ids.join(','),
'market': market?.name,
};
await _api._delete('$_path/shows?' + _buildQuery(queryMap));
}

/// Check if passed albums (ids) are saved by current user.
Expand Down
8 changes: 4 additions & 4 deletions lib/src/endpoints/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class PlayerEndpoint extends _MeEndpointBase {
/// and active device.
@Deprecated('Use [playbackState] instead')
Future<PlaybackState> player([String? market]) async {
return playbackState(market);
return playbackState(Market.values.asNameMap()[market]);
}

/// Returns the current playback state, including progress, track
/// and active device.
Future<PlaybackState> playbackState([String? market]) async {
var jsonString =
await _api._get('$_path?' + _buildQuery({'market': market}));
Future<PlaybackState> playbackState([Market? market]) async {
var jsonString = await _api
._get('$_path?' + _buildQuery({'market': market?.name}));
final map = json.decode(jsonString);
return PlaybackState.fromJson(map);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/endpoints/recommendations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RecommendationsEndpoint extends EndpointBase {
Iterable<String>? seedGenres,
Iterable<String>? seedTracks,
int limit = 20,
String? market,
Market? market,
Map<String, num>? max,
Map<String, num>? min,
Map<String, num>? target}) async {
Expand All @@ -33,7 +33,7 @@ class RecommendationsEndpoint extends EndpointBase {
'seed_genres': seedGenres,
'seed_tracks': seedTracks
}.forEach((key, list) => _addList(parameters, key, list!));
if (market != null) parameters['market'] = market;
if (market != null) parameters['market'] = market.name;
[min, max, target].forEach((map) => _addTunableTrackMap(parameters, map));
final pathQuery = Uri(path: _path, queryParameters: parameters)
.toString()
Expand Down
13 changes: 6 additions & 7 deletions lib/src/endpoints/search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ class Search extends EndpointPaging {
BundledPages get(
String searchQuery, {
Iterable<SearchType> types = SearchType.all,
String market = '',
Market? market,
}) {
var type = types.map((type) => type._key).join(',');

var queryMap = {'q': searchQuery, 'type': type};
if (market.isNotEmpty) {
queryMap.addAll({'market': market});
}

var query = _buildQuery(queryMap);
var query = _buildQuery({
'q': searchQuery,
'type': type,
'market': market?.name,
});

return _getBundledPages('$_path?$query', {
'playlists': (json) => PlaylistSimple.fromJson(json),
Expand Down
12 changes: 7 additions & 5 deletions lib/src/endpoints/shows.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ class Shows extends EndpointPaging {
/// [market]: An ISO 3166-1 alpha-2 country code or the string 'from_token'.
/// If a country code is specified, only artists, albums, and tracks with
/// content that is playable in that market is returned.
Future<Show> get(String showId, {String market = ''}) async {
Future<Show> get(String showId, {Market? market}) async {
var jsonString;
if (market.isNotEmpty) {
var queryMap = {'market': market};
if (market != null) {
var queryMap = {'market': market.name};
var query = _buildQuery(queryMap);
jsonString = await _get('$_path/$showId?$query');
} else {
Expand All @@ -44,8 +44,10 @@ class Shows extends EndpointPaging {
/// [market]: An ISO 3166-1 alpha-2 country code or the string 'from_token'.
/// If a country code is specified, only artists, albums, and tracks with
/// content that is playable in that market is returned.
Pages<Episode> episodes(String showId, {String? market}) {
var query = _buildQuery({'market': market});
Pages<Episode> episodes(String showId, [Market? market]) {
var query = _buildQuery({
'market': market?.name,
});
var queryString = query.isNotEmpty ? '?$query' : '';

return _getPages('$_path/$showId/episodes$queryString',
Expand Down
1 change: 1 addition & 0 deletions lib/src/models/market.dart
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ enum Market {
VU,
WF,
WS,
XK,
YE,
YT,
ZA,
Expand Down
2 changes: 1 addition & 1 deletion test/data/v1/episodes/5Xt5DXGzch68nYYamXrNxZ.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"show": {
"available_markets": [
"string"
"US"
],
"copyrights": [
{
Expand Down
185 changes: 183 additions & 2 deletions test/data/v1/markets.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,188 @@
{
"markets": [
"CA",
"AD",
"AE",
"AG",
"AL",
"AM",
"AO",
"AR",
"AT",
"AU",
"AZ",
"BA",
"BB",
"BD",
"BE",
"BF",
"BG",
"BH",
"BI",
"BJ",
"BN",
"BO",
"BR",
"IT"
"BS",
"BT",
"BW",
"BY",
"BZ",
"CA",
"CD",
"CG",
"CH",
"CI",
"CL",
"CM",
"CO",
"CR",
"CV",
"CW",
"CY",
"CZ",
"DE",
"DJ",
"DK",
"DM",
"DO",
"DZ",
"EC",
"EE",
"EG",
"ES",
"ET",
"FI",
"FJ",
"FM",
"FR",
"GA",
"GB",
"GD",
"GE",
"GH",
"GM",
"GN",
"GQ",
"GR",
"GT",
"GW",
"GY",
"HK",
"HN",
"HR",
"HT",
"HU",
"ID",
"IE",
"IL",
"IN",
"IQ",
"IS",
"IT",
"JM",
"JO",
"JP",
"KE",
"KG",
"KH",
"KI",
"KM",
"KN",
"KR",
"KW",
"KZ",
"LA",
"LB",
"LC",
"LI",
"LK",
"LR",
"LS",
"LT",
"LU",
"LV",
"LY",
"MA",
"MC",
"MD",
"ME",
"MG",
"MH",
"MK",
"ML",
"MN",
"MO",
"MR",
"MT",
"MU",
"MV",
"MW",
"MX",
"MY",
"MZ",
"NA",
"NE",
"NG",
"NI",
"NL",
"NO",
"NP",
"NR",
"NZ",
"OM",
"PA",
"PE",
"PG",
"PH",
"PK",
"PL",
"PS",
"PT",
"PW",
"PY",
"QA",
"RO",
"RS",
"RW",
"SA",
"SB",
"SC",
"SE",
"SG",
"SI",
"SK",
"SL",
"SM",
"SN",
"SR",
"ST",
"SV",
"SZ",
"TD",
"TG",
"TH",
"TJ",
"TL",
"TN",
"TO",
"TR",
"TT",
"TV",
"TW",
"TZ",
"UA",
"UG",
"US",
"UY",
"UZ",
"VC",
"VE",
"VN",
"VU",
"WS",
"XK",
"ZA",
"ZM",
"ZW"
]
}
2 changes: 1 addition & 1 deletion test/data/v1/me/episodes.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"show": {
"available_markets": [
"string"
"US"
],
"copyrights": [
{
Expand Down
Loading