Skip to content

Commit 7a19e57

Browse files
committed
condense platform-specific logic into one file per platform
- change retryIfInternalServerError to retryOnInternalServerErrorTimes to allow multiple times/infinite retry on internal server error - remove redundant base64 code Signed-off-by: Adam Ratzman <[email protected]>
1 parent fc6678a commit 7a19e57

File tree

57 files changed

+180
-481
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+180
-481
lines changed

src/androidMain/kotlin/com/adamratzman/spotify/SpotifyLogger.kt

Lines changed: 0 additions & 33 deletions
This file was deleted.

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/androidMain/kotlin/com/adamratzman/spotify/utils/Concurrency.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/androidMain/kotlin/com/adamratzman/spotify/utils/Misc.kt

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/androidMain/kotlin/com/adamratzman/spotify/utils/Platform.kt

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/androidMain/kotlin/com/adamratzman/spotify/utils/IO.kt renamed to src/androidMain/kotlin/com/adamratzman/spotify/utils/PlatformUtils.kt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
/* Spotify Web API, Kotlin Wrapper; MIT License, 2017-2021; Original author: Adam Ratzman */
21
package com.adamratzman.spotify.utils
32

3+
import android.annotation.SuppressLint
44
import android.graphics.Bitmap
55
import android.graphics.BitmapFactory
66
import android.util.Base64
77
import java.io.ByteArrayOutputStream
88
import java.net.URL
9+
import java.net.URLEncoder
10+
import java.text.SimpleDateFormat
11+
import java.util.Date
12+
13+
internal actual fun String.encodeUrl() = URLEncoder.encode(this, "UTF-8")!!
14+
15+
@SuppressLint("SimpleDateFormat")
16+
internal actual fun formatDate(format: String, date: Long): String {
17+
return SimpleDateFormat(format).format(Date(date))
18+
}
919

1020
internal actual fun encodeBufferedImageToBase64String(image: BufferedImage): String {
1121
val byteArrayOutputStream = ByteArrayOutputStream()
@@ -23,3 +33,16 @@ internal actual fun convertUrlPathToBufferedImage(url: String): BufferedImage {
2333
BitmapFactory.decodeStream(inputStream)
2434
}
2535
}
36+
37+
/**
38+
* Actual platform that this program is run on.
39+
*/
40+
public actual val currentApiPlatform: Platform = Platform.ANDROID
41+
42+
public actual typealias ConcurrentHashMap<K, V> = java.util.concurrent.ConcurrentHashMap<K, V>
43+
44+
public actual typealias BufferedImage = Bitmap // TODO
45+
46+
public actual typealias File = java.io.File
47+
48+
public actual fun <K, V> ConcurrentHashMap<K, V>.asList(): List<Pair<K, V>> = toList()

src/androidMain/kotlin/com/adamratzman/spotify/utils/Types.kt

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/commonMain/kotlin/com.adamratzman.spotify/Builder.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,8 @@ public class SpotifyUserAuthorization(
863863
* @param requiredScopes Scopes that your application requires to function (only applicable to [SpotifyClientApi] and [SpotifyImplicitGrantApi]).
864864
* @param proxyBaseUrl Provide if you have a proxy base URL that you would like to use instead of the Spotify API base
865865
* (https://api.spotify.com/v1).
866-
* @param retryIfInternalServerError Whether to retry once if an internal server error (500..599) has been received
866+
* @param retryOnInternalServerErrorTimes Whether and how often to retry once if an internal server error (500..599) has been received. Set to 0
867+
* to avoid retrying at all, or set to null to keep retrying until success.
867868
*
868869
*/
869870
public data class SpotifyApiOptions(
@@ -881,5 +882,5 @@ public data class SpotifyApiOptions(
881882
public var onTokenRefresh: (suspend (GenericSpotifyApi) -> Unit)? = null,
882883
public var requiredScopes: List<SpotifyScope>? = null,
883884
public var proxyBaseUrl: String? = null,
884-
public var retryIfInternalServerError: Boolean = true
885+
public var retryOnInternalServerErrorTimes: Int? = 5,
885886
)

src/commonMain/kotlin/com.adamratzman.spotify/SpotifyApi.kt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ import com.adamratzman.spotify.http.HttpRequestMethod
3030
import com.adamratzman.spotify.http.HttpResponse
3131
import com.adamratzman.spotify.http.SpotifyEndpoint
3232
import com.adamratzman.spotify.http.SpotifyRequest
33-
import com.adamratzman.spotify.http.base64ByteEncode
3433
import com.adamratzman.spotify.models.AuthenticationError
3534
import com.adamratzman.spotify.models.Token
3635
import com.adamratzman.spotify.models.TokenValidityResponse
3736
import com.adamratzman.spotify.models.serialization.toObject
3837
import com.adamratzman.spotify.utils.asList
39-
import kotlin.jvm.JvmOverloads
38+
import com.adamratzman.spotify.utils.base64ByteEncode
4039
import kotlinx.serialization.json.Json
40+
import kotlin.jvm.JvmOverloads
4141

4242
/**
4343
* Represents an instance of the Spotify API client, with common
@@ -69,7 +69,6 @@ public sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B
6969

7070
field = value
7171
}
72-
public val logger: SpotifyLogger = SpotifyLogger(spotifyApiOptions.enableLogger)
7372
public val expireTime: Long get() = token.expiresAt
7473
public var runExecutableFunctions: Boolean = true
7574

@@ -150,15 +149,6 @@ public sealed class SpotifyApi<T : SpotifyApi<T, B>, B : ISpotifyApiBuilder<T, B
150149
endpoints.forEach { it.cache.clear() }
151150
}
152151

153-
/**
154-
* Allows enabling and disabling the logger
155-
*
156-
* @param enable Whether to enable the logger
157-
*/
158-
public fun useLogger(enable: Boolean) {
159-
logger.enabled = enable
160-
}
161-
162152
/**
163153
* Create a Spotify authorization URL from which client access can be obtained
164154
*
@@ -622,8 +612,7 @@ public open class SpotifyClientApi(
622612
).execute()
623613
}
624614

625-
if (response.responseCode / 200 == 1) {
626-
api.logger.logInfo("Successfully refreshed the Spotify token")
615+
if (response.responseCode in 200..399) {
627616
response.body.toObject(Token.serializer(), api, api.spotifyApiOptions.json)
628617
} else throw BadRequestException(
629618
response.body.toObject(

src/commonMain/kotlin/com.adamratzman.spotify/SpotifyLogger.kt

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)