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

Implements markets endpoint #161

Merged
merged 4 commits into from
May 5, 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
1 change: 1 addition & 0 deletions lib/spotify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ part 'src/endpoints/categories.dart';
part 'src/endpoints/endpoint_base.dart';
part 'src/endpoints/endpoint_paging.dart';
part 'src/endpoints/episodes.dart';
part 'src/endpoints/markets.dart';
part 'src/endpoints/me.dart';
part 'src/endpoints/player.dart';
part 'src/endpoints/playlists.dart';
Expand Down
20 changes: 20 additions & 0 deletions lib/src/endpoints/markets.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
part of spotify;

/// Endpoint for `v1/markets`
class Markets extends EndpointBase {
@override
String get _path => 'v1/markets';

Markets(SpotifyApiBase api) : super(api);

/// Get the list of markets where Spotify is available.
/// A result of `null` means that the country is not mapped
/// in this library.
Future<Iterable<Market?>> get availableMarkets async {
var jsonString = await _api._get(_path);
var map = json.decode(jsonString);
var markets = map['markets'] as Iterable<dynamic>;
var marketsMap = Market.values.asNameMap();
return markets.map((market) => marketsMap[market]);
}
}
1 change: 1 addition & 0 deletions lib/src/models/_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ part 'device.dart';
part 'error.dart';
part 'followers.dart';
part 'image.dart';
part 'market.dart';
part 'paging.dart';
part 'player.dart';
part 'playlist.dart';
Expand Down
254 changes: 254 additions & 0 deletions lib/src/models/market.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
// Copyright (c) 2019-2021, Denis Portnov. All rights reserved.
part of spotify.models;

enum Market {
AD,
AE,
AF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deletions lib/src/spotify_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ abstract class SpotifyApiBase {
Episodes get episodes => _episodes;
late RecommendationsEndpoint _recommendations;
RecommendationsEndpoint get recommendations => _recommendations;
late Markets _markets;
Markets get markets => _markets;
late Users _users;
Users get users => _users;
late Search _search;
Expand Down Expand Up @@ -53,6 +55,7 @@ abstract class SpotifyApiBase {
_episodes = Episodes(this);
_playlists = Playlists(this);
_recommendations = RecommendationsEndpoint(this);
_markets = Markets(this);
_player = PlayerEndpoint(this);
_me = Me(this, _player);
_users = Users(this);
Expand Down
7 changes: 7 additions & 0 deletions test/data/v1/markets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"markets": [
"CA",
"BR",
"IT"
]
}
11 changes: 11 additions & 0 deletions test/spotify_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,17 @@ Future main() async {
expect(result.isExpired, true);
});
});

group('markets', () {
test('getAvailableMarkets', () async {
var result = await spotify.markets.availableMarkets;
expect(result.isEmpty, false);
expect(result.first, Market.CA);
expect(result.elementAt(1), Market.BR);
expect(result.last, Market.IT);
});
});

group('Errors', () {
test('apiRateErrorSuccess', () async {
spotify.mockHttpErrors = List.generate(
Expand Down