@@ -46,6 +46,7 @@ internal val base = "https://api.spotify.com/v1"
46
46
* @property token The access token associated with this API instance
47
47
* @property useCache Whether to use the built-in cache to avoid making unnecessary calls to
48
48
* the Spotify API
49
+ * @property cacheLimit The maximum amount of cached requests allowed at one time. Null means no limit
49
50
*
50
51
* @property search Provides access to the Spotify [search endpoint](https://developer.spotify.com/documentation/web-api/reference/search/search/)
51
52
* @property albums Provides access to Spotify [album endpoints](https://developer.spotify.com/documentation/web-api/reference/albums/)
@@ -62,6 +63,7 @@ abstract class SpotifyAPI internal constructor(
62
63
val clientSecret : String ,
63
64
var token : Token ,
64
65
useCache : Boolean ,
66
+ var cacheLimit : Int? ,
65
67
var automaticRefresh : Boolean ,
66
68
var retryWhenRateLimited : Boolean ,
67
69
enableLogger : Boolean
@@ -73,6 +75,11 @@ abstract class SpotifyAPI internal constructor(
73
75
field = value
74
76
}
75
77
78
+ /* *
79
+ * Obtain a map of all currently-cached requests
80
+ */
81
+ fun getCache () = endpoints.map { it.cache.cachedRequests.toList() }.flatten().toMap()
82
+
76
83
val expireTime: Long get() = System .currentTimeMillis() + token.expiresIn * 1000
77
84
78
85
val executor: ScheduledExecutorService = Executors .newScheduledThreadPool(0 )
@@ -99,10 +106,15 @@ abstract class SpotifyAPI internal constructor(
99
106
*/
100
107
abstract fun refreshToken (): Token
101
108
109
+ /* *
110
+ * A list of all endpoints included in this api type
111
+ */
112
+ abstract val endpoints: List <SpotifyEndpoint >
113
+
102
114
/* *
103
115
* If the cache is enabled, clear all stored queries in the cache
104
116
*/
105
- abstract fun clearCache ()
117
+ fun clearCache () = clearCaches( * endpoints.toTypedArray() )
106
118
107
119
/* *
108
120
* Return a new [SpotifyApiBuilder] with the parameters provided to this api instance
@@ -114,7 +126,7 @@ abstract class SpotifyAPI internal constructor(
114
126
*/
115
127
abstract fun getApiBuilderDsl (): SpotifyApiBuilderDsl
116
128
117
- internal fun clearAllCaches (vararg endpoints : SpotifyEndpoint ) {
129
+ private fun clearCaches (vararg endpoints : SpotifyEndpoint ) {
118
130
endpoints.forEach { it.cache.clear() }
119
131
}
120
132
@@ -150,10 +162,11 @@ class SpotifyAppAPI internal constructor(
150
162
clientSecret : String ,
151
163
token : Token ,
152
164
useCache : Boolean ,
165
+ cacheLimit : Int? ,
153
166
automaticRefresh : Boolean ,
154
167
retryWhenRateLimited : Boolean ,
155
168
enableLogger : Boolean
156
- ) : SpotifyAPI(clientId, clientSecret, token, useCache, automaticRefresh, retryWhenRateLimited, enableLogger) {
169
+ ) : SpotifyAPI(clientId, clientSecret, token, useCache, cacheLimit, automaticRefresh, retryWhenRateLimited, enableLogger) {
157
170
158
171
override val search: SearchAPI = SearchAPI (this )
159
172
override val albums: AlbumAPI = AlbumAPI (this )
@@ -189,16 +202,17 @@ class SpotifyAppAPI internal constructor(
189
202
throw BadRequestException (" Either the client id or the client secret is not set" )
190
203
}
191
204
192
- override fun clearCache () = clearAllCaches(
193
- search,
194
- albums,
195
- browse,
196
- artists,
197
- playlists,
198
- users,
199
- tracks,
200
- following
201
- )
205
+ override val endpoints: List <SpotifyEndpoint >
206
+ get() = listOf (
207
+ search,
208
+ albums,
209
+ browse,
210
+ artists,
211
+ playlists,
212
+ users,
213
+ tracks,
214
+ following
215
+ )
202
216
203
217
override fun getApiBuilder () = SpotifyApiBuilder (clientId, clientSecret, useCache)
204
218
@@ -223,9 +237,10 @@ class SpotifyClientAPI internal constructor(
223
237
automaticRefresh : Boolean ,
224
238
var redirectUri : String ,
225
239
useCache : Boolean ,
240
+ cacheLimit : Int? ,
226
241
retryWhenRateLimited : Boolean ,
227
242
enableLogger : Boolean
228
- ) : SpotifyAPI(clientId, clientSecret, token, useCache, automaticRefresh, retryWhenRateLimited, enableLogger) {
243
+ ) : SpotifyAPI(clientId, clientSecret, token, useCache, cacheLimit, automaticRefresh, retryWhenRateLimited, enableLogger) {
229
244
override val search: SearchAPI = SearchAPI (this )
230
245
override val albums: AlbumAPI = AlbumAPI (this )
231
246
override val browse: BrowseAPI = BrowseAPI (this )
@@ -316,19 +331,20 @@ class SpotifyClientAPI internal constructor(
316
331
} else throw BadRequestException (response.body.toObject<AuthenticationError >(this ))
317
332
}
318
333
319
- override fun clearCache () = clearAllCaches(
320
- search,
321
- albums,
322
- browse,
323
- artists,
324
- playlists,
325
- users,
326
- tracks,
327
- following,
328
- personalization,
329
- library,
330
- player
331
- )
334
+ override val endpoints: List <SpotifyEndpoint >
335
+ get() = listOf (
336
+ search,
337
+ albums,
338
+ browse,
339
+ artists,
340
+ playlists,
341
+ users,
342
+ tracks,
343
+ following,
344
+ personalization,
345
+ library,
346
+ player
347
+ )
332
348
333
349
override fun getApiBuilder () = SpotifyApiBuilder (clientId, clientSecret, redirectUri, useCache = useCache)
334
350
0 commit comments