Skip to content

Commit 06ee91c

Browse files
committed
lint
Signed-off-by: Adam Ratzman <[email protected]>
1 parent 837e10c commit 06ee91c

File tree

7 files changed

+182
-114
lines changed

7 files changed

+182
-114
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ If you have a question, you can:
8282
| Images (Playlist covers) | :heavy_check_mark: | :heavy_check_mark: | Unsupported | Unsupported |
8383
| getSpotifyPkceCodeChallenge | :heavy_check_mark: | :heavy_check_mark: | Unsupported | Unsupported |
8484
| Edit client playlist | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Unsupported |
85+
| Remove playlist tracks | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | Unsupported |
8586

8687
Please feel free to open an issue/discussion on GitHub or Discord if you need access to one of these features
8788
or have an interest in implementing one, as direction can be provided.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
22
package com.adamratzman.spotify.utils
33

4-
public actual typealias TimeUnit = java.util.concurrent.TimeUnit
4+
public actual typealias TimeUnit = java.util.concurrent.TimeUnit

src/commonMain/kotlin/com.adamratzman.spotify/http/Endpoints.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import com.adamratzman.spotify.models.ErrorResponse
1010
import com.adamratzman.spotify.models.serialization.toObject
1111
import com.adamratzman.spotify.utils.ConcurrentHashMap
1212
import com.adamratzman.spotify.utils.getCurrentTimeMs
13+
import kotlin.math.ceil
1314
import kotlinx.coroutines.TimeoutCancellationException
1415
import kotlinx.coroutines.withTimeout
1516
import kotlinx.serialization.Serializable
1617
import kotlinx.serialization.Transient
17-
import kotlin.math.ceil
1818

1919
public abstract class SpotifyEndpoint(public val api: GenericSpotifyApi) {
2020
public val cache: SpotifyCache = SpotifyCache()

src/commonMain/kotlin/com.adamratzman.spotify/models/serialization/SerializationUtils.kt

Lines changed: 98 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,33 @@ import kotlin.reflect.KClass
1414
import kotlinx.serialization.KSerializer
1515
import kotlinx.serialization.builtins.MapSerializer
1616
import kotlinx.serialization.builtins.serializer
17-
import kotlinx.serialization.decodeFromString
18-
import kotlinx.serialization.encodeToString
1917
import kotlinx.serialization.json.Json
2018
import kotlinx.serialization.json.JsonElement
2119
import kotlinx.serialization.json.JsonObject
2220

2321
internal val nonstrictJson =
24-
Json {
25-
isLenient = true
26-
ignoreUnknownKeys = true
27-
allowSpecialFloatingPointValues = true
28-
useArrayPolymorphism = true
29-
}
22+
Json {
23+
isLenient = true
24+
ignoreUnknownKeys = true
25+
allowSpecialFloatingPointValues = true
26+
useArrayPolymorphism = true
27+
}
3028

31-
internal inline fun <reified T : Any> String.toObjectNullable(serializer: KSerializer<T>, api: GenericSpotifyApi?, json: Json): T? = try {
29+
internal inline fun <reified T : Any> String.toObjectNullable(
30+
serializer: KSerializer<T>,
31+
api: GenericSpotifyApi?,
32+
json: Json
33+
): T? = try {
3234
toObject(serializer, api, json)
3335
} catch (e: Exception) {
3436
null
3537
}
3638

37-
internal inline fun <reified T : Any> String.toObject(serializer: KSerializer<T>, api: GenericSpotifyApi?, json: Json): T {
39+
internal inline fun <reified T : Any> String.toObject(
40+
serializer: KSerializer<T>,
41+
api: GenericSpotifyApi?,
42+
json: Json
43+
): T {
3844
return this.parseJson {
3945
val obj = json.decodeFromString(serializer, this)
4046
api?.let {
@@ -46,7 +52,11 @@ internal inline fun <reified T : Any> String.toObject(serializer: KSerializer<T>
4652
}
4753
}
4854

49-
internal inline fun <reified T> String.toList(serializer: KSerializer<List<T>>, api: GenericSpotifyApi?, json: Json): List<T> {
55+
internal inline fun <reified T> String.toList(
56+
serializer: KSerializer<List<T>>,
57+
api: GenericSpotifyApi?,
58+
json: Json
59+
): List<T> {
5060
return this.parseJson {
5161
json.decodeFromString(serializer, this).apply {
5262
if (api != null) {
@@ -70,22 +80,23 @@ internal fun <T : Any> String.toPagingObject(
7080
): NullablePagingObject<T> {
7181
if (innerObjectName != null || (arbitraryInnerNameAllowed && !skipInnerNameFirstIfPossible)) {
7282
val jsonObjectRoot = (json.parseToJsonElement(this) as JsonObject)
73-
val jsonElement = innerObjectName?.let { jsonObjectRoot[it] } ?: jsonObjectRoot.keys.firstOrNull()?.let { jsonObjectRoot[it] }
74-
?: throw SpotifyException.ParseException("Json element was null for class $tClazz (json $this)")
83+
val jsonElement =
84+
innerObjectName?.let { jsonObjectRoot[it] } ?: jsonObjectRoot.keys.firstOrNull()?.let { jsonObjectRoot[it] }
85+
?: throw SpotifyException.ParseException("Json element was null for class $tClazz (json $this)")
7586
val objectString = jsonElement.toString()
7687

7788
val map = objectString.parseJson {
78-
json.decodeFromString(NullablePagingObject.serializer(tSerializer),this)
89+
json.decodeFromString(NullablePagingObject.serializer(tSerializer), this)
7990
}
8091

8192
return map.apply {
82-
this.endpoint = endpoint
83-
this.itemClazz = tClazz
84-
this.items.map { obj ->
85-
if (obj is NeedsApi) obj.api = endpoint.api
86-
if (obj is PagingObjectBase<*, *>) obj.endpoint = endpoint
87-
}
88-
}
93+
this.endpoint = endpoint
94+
this.itemClazz = tClazz
95+
this.items.map { obj ->
96+
if (obj is NeedsApi) obj.api = endpoint.api
97+
if (obj is PagingObjectBase<*, *>) obj.endpoint = endpoint
98+
}
99+
}
89100
}
90101

91102
return try {
@@ -102,19 +113,23 @@ internal fun <T : Any> String.toPagingObject(
102113
} catch (jde: SpotifyException.ParseException) {
103114
if (arbitraryInnerNameAllowed && jde.message?.contains("unable to parse", true) == true) {
104115
toPagingObject(
105-
tClazz,
106-
tSerializer,
107-
innerObjectName,
108-
endpoint,
109-
json,
110-
arbitraryInnerNameAllowed = true,
111-
skipInnerNameFirstIfPossible = false
116+
tClazz,
117+
tSerializer,
118+
innerObjectName,
119+
endpoint,
120+
json,
121+
arbitraryInnerNameAllowed = true,
122+
skipInnerNameFirstIfPossible = false
112123
)
113124
} else throw jde
114125
}
115126
}
116127

117-
internal fun <T : Any> initPagingObject(tClazz: KClass<T>, pagingObject: PagingObjectBase<T, *>, endpoint: SpotifyEndpoint) {
128+
internal fun <T : Any> initPagingObject(
129+
tClazz: KClass<T>,
130+
pagingObject: PagingObjectBase<T, *>,
131+
endpoint: SpotifyEndpoint
132+
) {
118133
pagingObject.apply {
119134
this.endpoint = endpoint
120135
this.itemClazz = tClazz
@@ -132,7 +147,14 @@ internal inline fun <reified T : Any> String.toPagingObject(
132147
json: Json,
133148
arbitraryInnerNameAllowed: Boolean = false,
134149
skipInnerNameFirstIfPossible: Boolean = true
135-
): PagingObject<T> = toNullablePagingObject(tSerializer, innerObjectName, endpoint, json, arbitraryInnerNameAllowed, skipInnerNameFirstIfPossible).toPagingObject()
150+
): PagingObject<T> = toNullablePagingObject(
151+
tSerializer,
152+
innerObjectName,
153+
endpoint,
154+
json,
155+
arbitraryInnerNameAllowed,
156+
skipInnerNameFirstIfPossible
157+
).toPagingObject()
136158

137159
internal inline fun <reified T : Any> String.toNullablePagingObject(
138160
tSerializer: KSerializer<T>,
@@ -141,7 +163,15 @@ internal inline fun <reified T : Any> String.toNullablePagingObject(
141163
json: Json,
142164
arbitraryInnerNameAllowed: Boolean = false,
143165
skipInnerNameFirstIfPossible: Boolean = true
144-
): NullablePagingObject<T> = toPagingObject(T::class, tSerializer, innerObjectName, endpoint, json, arbitraryInnerNameAllowed, skipInnerNameFirstIfPossible)
166+
): NullablePagingObject<T> = toPagingObject(
167+
T::class,
168+
tSerializer,
169+
innerObjectName,
170+
endpoint,
171+
json,
172+
arbitraryInnerNameAllowed,
173+
skipInnerNameFirstIfPossible
174+
)
145175

146176
internal fun <T : Any> String.toCursorBasedPagingObject(
147177
tClazz: KClass<T>,
@@ -158,25 +188,26 @@ internal fun <T : Any> String.toCursorBasedPagingObject(
158188
json.decodeFromString(MapSerializer(t.first, t.second), this)
159189
}
160190
return (map[innerObjectName] ?: if (arbitraryInnerNameAllowed) map.keys.firstOrNull()?.let { map[it] }
161-
?: error("") else error(""))
162-
.apply { initPagingObject(tClazz, this, endpoint) }
191+
?: error("") else error(""))
192+
.apply { initPagingObject(tClazz, this, endpoint) }
163193
}
164194
return try {
165-
val pagingObject = this.parseJson { json.decodeFromString(CursorBasedPagingObject.serializer(tSerializer), this) }
195+
val pagingObject =
196+
this.parseJson { json.decodeFromString(CursorBasedPagingObject.serializer(tSerializer), this) }
166197

167198
initPagingObject(tClazz, pagingObject, endpoint)
168199

169200
pagingObject
170201
} catch (jde: SpotifyException.ParseException) {
171202
if (!arbitraryInnerNameAllowed && jde.message?.contains("unable to parse", true) == true) {
172203
toCursorBasedPagingObject(
173-
tClazz,
174-
tSerializer,
175-
innerObjectName,
176-
endpoint,
177-
json,
178-
arbitraryInnerNameAllowed = true,
179-
skipInnerNameFirstIfPossible = false
204+
tClazz,
205+
tSerializer,
206+
innerObjectName,
207+
endpoint,
208+
json,
209+
arbitraryInnerNameAllowed = true,
210+
skipInnerNameFirstIfPossible = false
180211
)
181212
} else throw jde
182213
}
@@ -190,7 +221,15 @@ internal inline fun <reified T : Any> String.toCursorBasedPagingObject(
190221
arbitraryInnerNameAllowed: Boolean = false,
191222
skipInnerNameFirstIfPossible: Boolean = true
192223
): CursorBasedPagingObject<T> =
193-
toCursorBasedPagingObject(T::class, tSerializer, innerObjectName, endpoint, json, arbitraryInnerNameAllowed, skipInnerNameFirstIfPossible)
224+
toCursorBasedPagingObject(
225+
T::class,
226+
tSerializer,
227+
innerObjectName,
228+
endpoint,
229+
json,
230+
arbitraryInnerNameAllowed,
231+
skipInnerNameFirstIfPossible
232+
)
194233

195234
internal inline fun <reified T> String.toInnerObject(serializer: KSerializer<T>, innerName: String, json: Json): T {
196235
val map = this.parseJson {
@@ -200,7 +239,11 @@ internal inline fun <reified T> String.toInnerObject(serializer: KSerializer<T>,
200239
return (map[innerName] ?: error("Inner object with name $innerName doesn't exist in $map"))
201240
}
202241

203-
internal inline fun <reified T> String.toInnerArray(serializer: KSerializer<List<T>>, innerName: String, json: Json): List<T> {
242+
internal inline fun <reified T> String.toInnerArray(
243+
serializer: KSerializer<List<T>>,
244+
innerName: String,
245+
json: Json
246+
): List<T> {
204247
val map = this.parseJson {
205248
val t = (String.serializer() to serializer)
206249
json.decodeFromString(MapSerializer(t.first, t.second), this)
@@ -210,17 +253,20 @@ internal inline fun <reified T> String.toInnerArray(serializer: KSerializer<List
210253

211254
internal fun Map<String, JsonElement>.toJson() = JsonObject(this).toString()
212255

213-
internal fun <A, B> createMapSerializer(aSerializer: KSerializer<A>, bSerializer: KSerializer<B>): KSerializer<Map<A, B>> {
256+
internal fun <A, B> createMapSerializer(
257+
aSerializer: KSerializer<A>,
258+
bSerializer: KSerializer<B>
259+
): KSerializer<Map<A, B>> {
214260
val t = (aSerializer to bSerializer)
215261
return MapSerializer(t.first, t.second)
216262
}
217263

218264
internal fun <T> String.parseJson(producer: String.() -> T): T =
219-
try {
220-
producer(this)
221-
} catch (e: Exception) {
222-
throw SpotifyException.ParseException(
223-
"Unable to parse $this (${e.message})",
224-
e
225-
)
226-
}
265+
try {
266+
producer(this)
267+
} catch (e: Exception) {
268+
throw SpotifyException.ParseException(
269+
"Unable to parse $this (${e.message})",
270+
e
271+
)
272+
}

src/commonTest/kotlin/com.adamratzman/spotify/Common.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
22
package com.adamratzman.spotify
33

4-
import com.autodesk.coroutineworker.CoroutineWorker
54
import kotlin.coroutines.CoroutineContext
5+
import kotlin.native.concurrent.ThreadLocal
66
import kotlin.test.assertTrue
77
import kotlinx.coroutines.CoroutineScope
8-
import kotlinx.coroutines.CoroutineStart.LAZY
9-
import kotlinx.coroutines.Deferred
10-
import kotlinx.coroutines.Dispatchers
11-
import kotlinx.coroutines.GlobalScope
12-
import kotlinx.coroutines.MainScope
13-
import kotlinx.coroutines.async
14-
import kotlin.native.concurrent.ThreadLocal
158

169
val clientId = getEnvironmentVariable("SPOTIFY_CLIENT_ID")
1710
val clientSecret = getEnvironmentVariable("SPOTIFY_CLIENT_SECRET")
1811
val redirectUri = getEnvironmentVariable("SPOTIFY_REDIRECT_URI")
1912
val tokenString = getEnvironmentVariable("SPOTIFY_TOKEN_STRING")
2013

21-
2214
// https://github.com/Kotlin/kotlinx.coroutines/issues/1996#issuecomment-728562784
2315
expect fun runBlockingTest(block: suspend CoroutineScope.() -> Unit)
2416
expect val testCoroutineContext: CoroutineContext

0 commit comments

Comments
 (0)