@@ -9,13 +9,13 @@ import com.adamratzman.spotify.models.CursorBasedPagingObject
9
9
import com.adamratzman.spotify.models.NeedsApi
10
10
import com.adamratzman.spotify.models.PagingObject
11
11
import com.adamratzman.spotify.models.instantiatePagingObjects
12
- import kotlin.reflect.KClass
13
12
import kotlinx.serialization.KSerializer
14
13
import kotlinx.serialization.json.Json
15
14
import kotlinx.serialization.json.JsonElement
16
15
import kotlinx.serialization.json.JsonObject
17
16
import kotlinx.serialization.map
18
17
import kotlinx.serialization.serializer
18
+ import kotlin.reflect.KClass
19
19
20
20
@Suppress(" EXPERIMENTAL_API_USAGE" )
21
21
internal val stableJson =
@@ -102,6 +102,17 @@ internal fun <T : Any> String.toPagingObject(
102
102
}
103
103
}
104
104
105
+ internal fun <T : Any > initPagingObject (tClazz : KClass <T >, pagingObject : AbstractPagingObject <T >, endpoint : SpotifyEndpoint ) {
106
+ pagingObject.apply {
107
+ this .endpoint = endpoint
108
+ this .itemClazz = tClazz
109
+ this .items.map { obj ->
110
+ if (obj is NeedsApi ) obj.api = endpoint.api
111
+ if (obj is AbstractPagingObject <* >) obj.endpoint = endpoint
112
+ }
113
+ }
114
+ }
115
+
105
116
internal inline fun <reified T : Any > String.toPagingObject (
106
117
tSerializer : KSerializer <T >,
107
118
innerObjectName : String? = null,
@@ -124,26 +135,14 @@ internal fun <T : Any> String.toCursorBasedPagingObject(
124
135
val map = this .parseJson { json.parse((String .serializer() to CursorBasedPagingObject .serializer(tSerializer)).map, this ) }
125
136
return (map[innerObjectName] ? : if (arbitraryInnerNameAllowed) map.keys.firstOrNull()?.let { map[it] }
126
137
? : error(" " ) else error(" " ))
127
- .apply {
128
- this .endpoint = endpoint
129
- this .itemClazz = tClazz
130
- this .items.map { obj ->
131
- if (obj is NeedsApi ) obj.api = endpoint.api
132
- if (obj is AbstractPagingObject <* >) obj.endpoint = endpoint
133
- }
134
- }
138
+ .apply { initPagingObject(tClazz, this , endpoint) }
135
139
}
136
140
return try {
137
141
val pagingObject = this .parseJson { json.parse(CursorBasedPagingObject .serializer(tSerializer), this ) }
138
142
139
- pagingObject.apply {
140
- this .endpoint = endpoint
141
- this .itemClazz = tClazz
142
- this .items.map { obj ->
143
- if (obj is NeedsApi ) obj.api = endpoint.api
144
- if (obj is AbstractPagingObject <* >) obj.endpoint = endpoint
145
- }
146
- }
143
+ initPagingObject(tClazz, pagingObject, endpoint)
144
+
145
+ pagingObject
147
146
} catch (jde: SpotifyException .ParseException ) {
148
147
if (! arbitraryInnerNameAllowed && jde.message?.contains(" unable to parse" , true ) == true ) {
149
148
toCursorBasedPagingObject(
0 commit comments