Skip to content

Commit eeef00d

Browse files
committed
model fixes
Signed-off-by: Adam Ratzman <adam@adamratzman.com>
1 parent 04f29fe commit eeef00d

File tree

3 files changed

+55
-30
lines changed

3 files changed

+55
-30
lines changed

src/main/kotlin/com/adamratzman/spotify/endpoints/public/SearchAPI.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import com.adamratzman.spotify.utils.Market
1010
import com.adamratzman.spotify.utils.PagingObject
1111
import com.adamratzman.spotify.utils.Playlist
1212
import com.adamratzman.spotify.utils.SimpleAlbum
13+
import com.adamratzman.spotify.utils.SimplePlaylist
1314
import com.adamratzman.spotify.utils.SimpleTrack
1415
import com.adamratzman.spotify.utils.SpotifyEndpoint
16+
import com.adamratzman.spotify.utils.Track
1517
import com.adamratzman.spotify.utils.encode
1618
import com.adamratzman.spotify.utils.toPagingObject
1719
import java.util.function.Supplier
@@ -41,12 +43,12 @@ class SearchAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
4143
limit: Int? = null,
4244
offset: Int? = null,
4345
market: Market? = null
44-
): SpotifyRestPagingAction<Playlist, PagingObject<Playlist>> {
46+
): SpotifyRestPagingAction<SimplePlaylist, PagingObject<SimplePlaylist>> {
4547
return toPagingObjectAction(Supplier {
4648
get(build(SearchType.PLAYLIST, query, limit, offset, market)).toPagingObject(
4749
"playlists",
4850
this,
49-
Playlist.serializer()
51+
SimplePlaylist.serializer()
5052
)
5153
})
5254
}
@@ -122,12 +124,12 @@ class SearchAPI(api: SpotifyAPI) : SpotifyEndpoint(api) {
122124
limit: Int? = null,
123125
offset: Int? = null,
124126
market: Market? = null
125-
): SpotifyRestPagingAction<SimpleTrack, PagingObject<SimpleTrack>> {
127+
): SpotifyRestPagingAction<Track, PagingObject<Track>> {
126128
return toPagingObjectAction(Supplier {
127129
get(build(SearchType.TRACK, query, limit, offset, market)).toPagingObject(
128130
"tracks",
129131
this,
130-
SimpleTrack.serializer()
132+
Track.serializer()
131133
)
132134
})
133135
}

src/main/kotlin/com/adamratzman/spotify/utils/Helpers.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.adamratzman.spotify.main.SpotifyException
77
import com.adamratzman.spotify.main.SpotifyRestAction
88
import kotlinx.serialization.KSerializer
99
import kotlinx.serialization.Serializable
10+
import kotlinx.serialization.Transient
1011
import kotlinx.serialization.json.JSON
1112
import org.json.JSONArray
1213
import org.json.JSONObject
@@ -27,7 +28,7 @@ class CursorBasedPagingObject<T>(
2728
total: Int,
2829
endpoint: SpotifyEndpoint,
2930
serializer: KSerializer<T>
30-
) : PagingObject<T>(href, items, limit, next, 0, null, total, endpoint,serializer)
31+
) : PagingObject<T>(href, items, limit, next, 0, null, total, endpoint, serializer)
3132

3233
@Serializable
3334
open class PagingObject<T>(
@@ -123,7 +124,7 @@ data class LinkedResult<out T>(val href: String, val items: List<T>) {
123124
}
124125

125126
@Serializable
126-
abstract class RelinkingAvailableResponse(val linkedTrack: LinkedTrack?) : Linkable() {
127+
abstract class RelinkingAvailableResponse(@Transient val linkedTrack: LinkedTrack?=null) : Linkable() {
127128
fun isRelinked() = linkedTrack != null
128129
}
129130

@@ -141,15 +142,20 @@ internal fun <T> String.toObjectNullable(o: SpotifyAPI?, serializer: KSerializer
141142

142143
@Suppress("UNCHECKED_CAST")
143144
internal fun <T> String.toObject(o: SpotifyAPI?, serializer: KSerializer<T>): T {
144-
val obj = (JSON.parse(serializer, this) as? T) ?: throw SpotifyException(
145-
"Unable to parse $this",
146-
IllegalArgumentException("$serializer not found")
147-
)
148-
o?.let {
149-
if (obj is Linkable) obj.api = o
150-
obj.instantiatePagingObjects(o)
145+
try {
146+
val obj = (JSON.parse(serializer, this) as? T) ?: throw SpotifyException(
147+
"Unable to parse $this",
148+
IllegalArgumentException("$serializer not found")
149+
)
150+
o?.let {
151+
if (obj is Linkable) obj.api = o
152+
obj.instantiatePagingObjects(o)
153+
}
154+
return obj
155+
}catch (e:java.lang.Exception){
156+
println(this)
157+
throw e
151158
}
152-
return obj
153159
}
154160

155161
internal fun Any.instantiatePagingObjects(spotifyAPI: SpotifyAPI) = when {

src/main/kotlin/com/adamratzman/spotify/utils/ResultObjects.kt

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.adamratzman.spotify.utils
33

44
import com.adamratzman.spotify.main.SpotifyAPI
55
import com.adamratzman.spotify.main.SpotifyRestAction
6+
import kotlinx.serialization.Optional
67
import kotlinx.serialization.Serializable
78

89
/**
@@ -36,12 +37,17 @@ data class RecommendationSeed(
3637
@Serializable
3738
data class SpotifyCategory(val href: String, val icons: List<SpotifyImage>, val id: String, val name: String)
3839

40+
@Serializable
3941
data class SpotifyCopyright(val text: String, val type: String)
4042

43+
@Serializable
4144
data class PlaylistTrackInfo(val href: String, val total: Int)
4245

4346
/**
44-
* @param href Will always be null, per the Spotify documentation, until the Web API is updated to support this.
47+
* @param href Will always be null, per the Spotify documentation,
48+
* until the Web API is updated to support this.
49+
*
50+
* @param total -1 if the user object does not contain followers, otherwise the amount of followers the user has
4551
*/
4652
@Serializable
4753
data class Followers(val href: String?, val total: Int)
@@ -66,16 +72,16 @@ data class SpotifyUserInformation(
6672
data class SpotifyPublicUser(
6773
val display_name: String,
6874
val external_urls: HashMap<String, String>,
69-
val followers: Followers,
75+
@Optional val followers: Followers = Followers(null,-1),
7076
val href: String,
7177
val id: String,
72-
val images: List<SpotifyImage>,
78+
@Optional val images: List<SpotifyImage> = listOf(),
7379
val type: String,
7480
val uri: String
7581
)
7682

7783
@Serializable
78-
data class SpotifyImage(val height: Int, val url: String, val width: Int)
84+
data class SpotifyImage(val height: Int?, val url: String, val width: Int?)
7985

8086
abstract class Linkable {
8187
@Transient
@@ -131,13 +137,14 @@ data class SimpleTrack(
131137
val external_urls: HashMap<String, String>,
132138
val href: String,
133139
val id: String,
134-
val is_playable: Boolean?,
140+
@Optional val is_playable: Boolean = true,
135141
private val linked_from: LinkedTrack?,
136142
val name: String,
137143
val preview_url: String,
138144
val track_number: Int,
139145
val type: String,
140-
val uri: String
146+
val uri: String,
147+
val is_local:Boolean
141148
) : RelinkingAvailableResponse(linked_from) {
142149
fun toFullTrack(market: Market? = null) = api.tracks.getTrack(id, market)
143150
}
@@ -146,36 +153,40 @@ data class SimpleTrack(
146153
data class Track(
147154
val album: SimpleAlbum,
148155
val artists: List<SimpleArtist>,
149-
val available_markets: List<String>,
156+
@Optional val available_markets: List<String>? = null,
157+
@Optional val is_playable: Boolean = true,
150158
val disc_number: Int,
151159
val duration_ms: Int,
152160
val explicit: Boolean,
153161
val external_ids: HashMap<String, String>,
154162
val external_urls: HashMap<String, String>,
155163
val href: String,
156164
val id: String,
157-
val is_playable: Boolean?,
158-
private val linked_from: LinkedTrack?,
165+
@Optional private val linked_from: LinkedTrack?=null,
159166
val name: String,
160167
val popularity: Int,
161168
val preview_url: String,
162169
val track_number: Int,
163170
val type: String,
164-
val uri: String
171+
val uri: String,
172+
val is_local:Boolean
165173
) : RelinkingAvailableResponse(linked_from)
166174

167175
@Serializable
168176
data class SimpleAlbum(
169177
val album_type: String,
170178
val artists: List<SimpleArtist>,
171-
val available_markets: List<String>,
179+
@Optional val available_markets: List<String>? = null,
172180
val external_urls: HashMap<String, String>,
173181
val href: String,
174182
val id: String,
175183
val images: List<SpotifyImage>,
176184
val name: String,
177185
val type: String,
178-
val uri: String
186+
val uri: String,
187+
val release_date: String,
188+
val release_date_precision: String,
189+
val total_tracks:Int
179190
) : Linkable() {
180191
fun toFullAlbum(market: Market? = null) = api.albums.getAlbum(id, market)
181192
}
@@ -211,6 +222,7 @@ data class SimplePlaylist(
211222
val images: List<SpotifyImage>,
212223
val name: String,
213224
val owner: SpotifyPublicUser,
225+
@Optional val primary_color: String? = null,
214226
val public: Boolean?,
215227
val snapshot_id: String,
216228
val tracks: PlaylistTrackInfo,
@@ -234,6 +246,7 @@ data class Playlist(
234246
val followers: Followers,
235247
val href: String,
236248
val id: String,
249+
@Optional val primary_color: String? = null,
237250
val images: List<SpotifyImage>,
238251
val name: String,
239252
val owner: SpotifyPublicUser,
@@ -257,9 +270,11 @@ data class AudioAnalysis(
257270
val tatums: List<AudioTatum>,
258271
val track: TrackAnalysis
259272
)
260-
273+
@Serializable
261274
data class AudioBar(val start: Float, val duration: Float, val confidence: Float)
275+
@Serializable
262276
data class AudioBeat(val start: Float, val duration: Float, val confidence: Float)
277+
@Serializable
263278
data class AudioTatum(val start: Float, val duration: Float, val confidence: Float)
264279

265280
@Serializable
@@ -297,7 +312,7 @@ data class AudioSegment(
297312
val loudness_start: Float,
298313
val loudness_max_time: Float,
299314
val loudness_max: Float,
300-
val loudness_end: Float,
315+
@Optional val loudness_end: Float?=null,
301316
val pitches: List<Float>,
302317
val timbre: List<Float>
303318
)
@@ -327,9 +342,9 @@ data class TrackAnalysis(
327342
val echoprintstring: String,
328343
val echoprint_version: Float,
329344
val synchstring: String,
330-
val synch_version: Int,
345+
val synch_version: Float,
331346
val rhythmstring: String,
332-
val rhythm_version: Int
347+
val rhythm_version: Float
333348
)
334349

335350
@Serializable
@@ -382,6 +397,7 @@ data class CurrentlyPlayingContext(
382397
val context: Context
383398
)
384399

400+
@Serializable
385401
data class Context(val external_urls: HashMap<String, String>)
386402

387403
@Serializable
@@ -393,6 +409,7 @@ data class CurrentlyPlayingObject(
393409
val item: Track
394410
)
395411

412+
@Serializable
396413
data class PlayHistoryContext(
397414
val type: String,
398415
val href: String,

0 commit comments

Comments
 (0)