-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathspotify_base.dart
244 lines (217 loc) · 8.1 KB
/
spotify_base.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// 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.
part of spotify;
abstract class SpotifyApiBase {
static const String _baseUrl = 'https://api.spotify.com';
static const String _tokenUrl = 'https://accounts.spotify.com/api/token';
static const String _authorizationUrl =
'https://accounts.spotify.com/authorize';
bool _shouldWait = false;
late FutureOr<oauth2.Client> _client;
late Artists _artists;
Artists get artists => _artists;
late Albums _albums;
Albums get albums => _albums;
late Browse _browse;
Browse get browse => _browse;
late Tracks _tracks;
Tracks get tracks => _tracks;
late Playlists _playlists;
Playlists get playlists => _playlists;
late Episodes _episodes;
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;
Search get search => _search;
late AudioFeatures _audioFeatures;
AudioFeatures get audioFeatures => _audioFeatures;
late AudioAnalysisEndpoint _audioAnalysis;
AudioAnalysisEndpoint get audioAnalysis => _audioAnalysis;
late Categories _categories;
Categories get categories => _categories;
late Me _me;
Me get me => _me;
late PlayerEndpoint _player;
PlayerEndpoint get player => _player;
late Shows _shows;
Shows get shows => _shows;
FutureOr<oauth2.Client> get client => _client;
SpotifyApiBase.fromClient(FutureOr<http.BaseClient> client) {
_client = client as FutureOr<oauth2.Client>;
_artists = Artists(this);
_albums = Albums(this);
_browse = Browse(this);
_tracks = Tracks(this);
_episodes = Episodes(this);
_playlists = Playlists(this);
_recommendations = RecommendationsEndpoint(this);
_markets = Markets(this);
_player = PlayerEndpoint(this);
_me = Me(this, _player);
_users = Users(this);
_search = Search(this);
_audioFeatures = AudioFeatures(this);
_audioAnalysis = AudioAnalysisEndpoint(this);
_categories = Categories(this);
_shows = Shows(this);
}
SpotifyApiBase(SpotifyApiCredentials credentials,
[http.Client? httpClient, Function(SpotifyApiCredentials)? callBack])
: this.fromClient(_getOauth2Client(credentials, httpClient, callBack));
SpotifyApiBase.fromAuthCodeGrant(
oauth2.AuthorizationCodeGrant grant, String responseUri)
: this.fromClient(grant.handleAuthorizationResponse(
Uri.parse(responseUri).queryParameters));
SpotifyApiBase._withAccessToken(String accessToken)
: this.fromClient(oauth2.Client(oauth2.Credentials(accessToken)));
static Future<SpotifyApi> _asyncFromCredentials(
SpotifyApiCredentials credentials, [
http.Client? httpClient,
Function(SpotifyApiCredentials)? callBack,
]) async {
final client = await _getOauth2Client(
credentials,
httpClient,
callBack,
);
return SpotifyApi.fromClient(client);
}
static oauth2.AuthorizationCodeGrant authorizationCodeGrant(
SpotifyApiCredentials credentials, http.Client httpClient,
[Function(SpotifyApiCredentials)? callBack]) {
return oauth2.AuthorizationCodeGrant(
credentials.clientId!,
Uri.parse(SpotifyApiBase._authorizationUrl),
Uri.parse(SpotifyApiBase._tokenUrl),
secret: credentials.clientSecret,
httpClient: httpClient,
onCredentialsRefreshed: callBack != null
? (oauth2.Credentials cred) {
final newCredentials = SpotifyApiCredentials(
credentials.clientId, credentials.clientSecret,
accessToken: cred.accessToken,
expiration: cred.expiration,
refreshToken: cred.refreshToken,
scopes: cred.scopes);
callBack(newCredentials);
}
: null);
}
static FutureOr<oauth2.Client> _getOauth2Client(
SpotifyApiCredentials credentials, http.Client? httpClient,
[Function(SpotifyApiCredentials)? callBack]) async {
if (credentials.fullyQualified) {
var oauthCredentials = credentials._toOauth2Credentials();
void credentialRefreshedWrapperCallback(oauth2.Credentials cred) {
callBack?.call(
SpotifyApiCredentials(
credentials.clientId,
credentials.clientSecret,
accessToken: cred.accessToken,
expiration: cred.expiration,
refreshToken: cred.refreshToken,
scopes: cred.scopes,
),
);
}
if (oauthCredentials.isExpired) {
oauthCredentials = await oauthCredentials.refresh(
identifier: credentials.clientId,
secret: credentials.clientSecret,
httpClient: httpClient,
);
credentialRefreshedWrapperCallback(oauthCredentials);
}
return oauth2.Client(
oauthCredentials,
identifier: credentials.clientId,
onCredentialsRefreshed: credentialRefreshedWrapperCallback,
secret: credentials.clientSecret,
);
}
return oauth2.clientCredentialsGrant(
Uri.parse(SpotifyApiBase._tokenUrl),
credentials.clientId,
credentials.clientSecret,
httpClient: httpClient,
);
}
Future<String> _get(String path) {
return _getImpl('$_baseUrl/$path', const {});
}
Future<String> _post(String path, [String body = '']) {
return _postImpl('$_baseUrl/$path', const {}, body);
}
Future<String> _delete(String path, [String body = '']) {
return _deleteImpl('$_baseUrl/$path', const {}, body);
}
Future<String> _put(String path, [String body = '']) {
return _putImpl('$_baseUrl/$path', const {}, body);
}
Future<String> _getImpl(String url, Map<String, String> headers) async {
return await _requestWrapper(() async =>
await (await _client).get(Uri.parse(url), headers: headers));
}
Future<String> _postImpl(
String url, Map<String, String> headers, dynamic body) async {
return await _requestWrapper(() async => await (await _client)
.post(Uri.parse(url), headers: headers, body: body));
}
Future<String> _deleteImpl(
String url, Map<String, String> headers, body) async {
return await _requestWrapper(() async {
final request = http.Request('DELETE', Uri.parse(url));
request.headers.addAll(headers);
request.body = body;
return await http.Response.fromStream(
await (await _client).send(request));
});
}
Future<String> _putImpl(
String url, Map<String, String> headers, dynamic body) async {
return await _requestWrapper(() async => await (await _client)
.put(Uri.parse(url), headers: headers, body: body));
}
Future<String> _requestWrapper(Future<http.Response> Function() request,
{retryLimit = 5}) async {
for (var i = 0; i < retryLimit; i++) {
while (_shouldWait) {
await Future.delayed(Duration(milliseconds: 500));
}
try {
return handleErrors(await request());
} on ApiRateException catch (ex) {
if (i == retryLimit - 1) rethrow;
print(
'Spotify API rate exceeded. waiting for ${ex.retryAfter} seconds');
_shouldWait = true;
unawaited(Future.delayed(Duration(seconds: ex.retryAfter.toInt()))
.then((v) => _shouldWait = false));
}
}
throw SpotifyException('Could not complete request');
}
Future<SpotifyApiCredentials> getCredentials() async {
return SpotifyApiCredentials._fromClient(await _client);
}
String handleErrors(http.Response response) {
final responseBody = utf8.decode(response.bodyBytes);
if (response.statusCode >= 400) {
final jsonMap = json.decode(responseBody);
final error = SpotifyError.fromJson(jsonMap['error']);
if (response.statusCode == 429) {
throw ApiRateException.fromSpotify(
error, num.parse(response.headers['retry-after']!));
}
throw SpotifyException.fromSpotify(
error,
);
}
return responseBody;
}
}