Skip to content

Commit ee2a10b

Browse files
committed
add documentation, update to current version
Signed-off-by: Adam Ratzman <[email protected]>
1 parent d6ba637 commit ee2a10b

File tree

20 files changed

+85
-37
lines changed

20 files changed

+85
-37
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,36 @@ APIs available only in `SpotifyClientApi` and `SpotifyImplicitGrantApi` instance
391391
## Platform-specific wrappers and information
392392

393393
### Java
394-
This library has first-class support for Java! You have
395-
394+
This library has first-class support for Java! You have two choices when using this library: async-only with Kotlin
395+
suspend functions (using SpotifyContinuation), or by using the `SpotifyRestAction` class. Using `SpotifyRestAction`s are
396+
recommended.
397+
398+
#### What is the SpotifyRestAction class and how do I use it?
399+
Abstracting requests into a `SpotifyRestAction` class allows for a lot of flexibility in sending and receiving requests.
400+
This class includes options for asynchronous and blocking execution in all endpoints. However,
401+
due to this, you **must** call one of the provided methods in order for the call
402+
to execute! The `SpotifyRestAction` provides many methods and fields for use, including blocking and asynchronous ones. For example,
403+
- `hasRun()` tells you whether the rest action has been *started*
404+
- `hasCompleted()` tells you whether this rest action has been fully executed and *completed*
405+
- `complete()` blocks the current thread and returns the result
406+
- `suspendComplete(context: CoroutineContext = Dispatchers.Default)` switches to given context, invokes the supplier, and synchronously retrieves the result.
407+
- `suspendQueue()` suspends the coroutine, invokes the supplier asynchronously, and resumes with the result
408+
- `queue()` executes and immediately returns
409+
- `queue(consumer: (T) -> Unit)` executes the provided callback as soon as the request
410+
is asynchronously evaluated
411+
- `queueAfter(quantity: Int, timeUnit: TimeUnit, consumer: (T) -> Unit)` executes the
412+
provided callback after the provided time. As long as supplier execution is less than the provided
413+
time, this will likely be accurate within a few milliseconds.
414+
- `asFuture()` transforms the supplier into a `CompletableFuture` (only JVM)
415+
416+
Here's an example of how easy it is to use `spotify-web-api-kotlin` with a `SpotifyRestAction`:
417+
```java
418+
var api = SpotifyApiBuilderKt.spotifyAppApi(Const.clientId, Const.clientSecret).buildRestAction(true).complete();
419+
var album = api.getAlbums().getAlbumRestAction("spotify:album:0b23AHutIA1BOW0u1dZ6wM", null).complete();
420+
System.out.println("Album name is: " + album.getName());
421+
```
396422

423+
#### Integrating with Kotlin suspend functions via Java `Continuation`s
397424
Unfortunately, coroutines don't play very nicely with Java code. Fortunately, however, we provide a wrapper around Kotlin's
398425
`Continuation` class that allows you to directly implement `onSuccess` and `onFailure` handlers on API methods.
399426

publish_all.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gradle publishMacosX64PublicationToNexusRepository publishLinuxX64PublicationToNexusRepository publishKotlinMultiplatformPublicationToNexusRepository publishTvosX64PublicationToNexusRepository publishTvosArm64PublicationToNexusRepository publishIosX64PublicationToNexusRepository publishIosArm64PublicationToNexusRepository publishJsPublicationToNexusRepository publishJvmPublicationToNexusRepository publishAndroidPublicationToNexusRepository

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import android.app.Activity
55
import android.content.Context
66
import android.util.Log
77
import android.widget.Toast
8-
import com.soywiz.korio.async.runBlockingNoJs
98
import kotlinx.coroutines.CoroutineScope
9+
import kotlinx.coroutines.runBlocking
1010
import java.net.URLEncoder
1111

1212
internal actual fun String.encodeUrl() = URLEncoder.encode(this, "UTF-8")!!
@@ -36,6 +36,6 @@ internal fun logToConsole(message: String) {
3636
Log.i("spotify-web-api-kotlin", message)
3737
}
3838

39-
public actual fun <T> runBlockingOnJvmAndNative(block: suspend CoroutineScope.() -> T): T {
40-
return runBlockingNoJs { block() }
39+
public actual fun <T> runBlockingOnJvmAndNative(block: suspend () -> T): T {
40+
return runBlocking { block() }
4141
}

src/androidTest/kotlin/com/adamratzman/spotify/CommonImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ public actual fun Exception.stackTrace() {
3131
public actual val testCoroutineContext: CoroutineContext =
3232
Executors.newSingleThreadExecutor().asCoroutineDispatcher()
3333

34-
public actual fun runBlockingTest(block: suspend CoroutineScope.() -> Unit): Unit = runBlocking(testCoroutineContext) { this.block() }
34+
public actual fun runBlockingTest(block: suspend () -> Unit): Unit = runBlocking(testCoroutineContext) { this.block() }

src/commonMain/kotlin/com.adamratzman.spotify/endpoints/client/ClientFollowingApi.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class ClientFollowingApi(api: GenericSpotifyApi) : FollowingApi(api) {
8989
* @throws [BadRequestException] if the playlist is not found
9090
* @return Whether the current user is following [playlistId]
9191
*/
92-
public fun isFollowingPlaylistRestAction(playlistId: String) =
92+
public fun isFollowingPlaylistRestAction(playlistId: String): SpotifyRestAction<Boolean> =
9393
SpotifyRestAction { isFollowingPlaylist(playlistId) }
9494

9595
/**

src/commonMain/kotlin/com.adamratzman.spotify/utils/Utils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ internal fun formatDate(date: Long): String {
3535
return Instant.fromEpochMilliseconds(date).toString()
3636
}
3737

38-
public expect fun <T> runBlockingOnJvmAndNative(block: suspend CoroutineScope.() -> T): T
38+
public expect fun <T> runBlockingOnJvmAndNative(block: suspend () -> T): T

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ val redirectUri = getEnvironmentVariable("SPOTIFY_REDIRECT_URI")
1212
val tokenString = getEnvironmentVariable("SPOTIFY_TOKEN_STRING")
1313

1414
// https://github.com/Kotlin/kotlinx.coroutines/issues/1996#issuecomment-728562784
15-
expect fun runBlockingTest(block: suspend CoroutineScope.() -> Unit)
15+
expect fun runBlockingTest(block: suspend () -> Unit)
1616
expect val testCoroutineContext: CoroutineContext
1717

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

4-
import com.soywiz.korio.async.runBlockingNoJs
54
import io.ktor.http.encodeURLQueryComponent
6-
import kotlinx.coroutines.CoroutineScope
75

86
internal actual fun String.encodeUrl() = encodeURLQueryComponent()
97

@@ -14,8 +12,4 @@ public actual val currentApiPlatform: Platform = Platform.NATIVE
1412

1513
public actual typealias ConcurrentHashMap<K, V> = HashMap<K, V>
1614

17-
public actual fun <K, V> ConcurrentHashMap<K, V>.asList(): List<Pair<K, V>> = toList()
18-
19-
public actual fun <T> runBlockingOnJvmAndNative(block: suspend CoroutineScope.() -> T): T {
20-
return runBlockingNoJs { block() }
21-
}
15+
public actual fun <K, V> ConcurrentHashMap<K, V>.asList(): List<Pair<K, V>> = toList()

src/desktopTest/kotlin/com.adamratzman.spotify/CommonImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ actual fun getEnvironmentVariable(name: String): String? {
1515
actual fun Exception.stackTrace() = printStackTrace()
1616

1717
actual val testCoroutineContext: CoroutineContext = MainScope().coroutineContext
18-
actual fun runBlockingTest(block: suspend CoroutineScope.() -> Unit) =
19-
runBlocking { this.block() }
18+
actual fun runBlockingTest(block: suspend () -> Unit) =
19+
runBlocking { block() }
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.adamratzman.spotify.utils
2+
3+
import kotlinx.coroutines.runBlocking
4+
5+
public actual fun <T> runBlockingOnJvmAndNative(block: suspend () -> T): T {
6+
return runBlocking { block() }
7+
}

0 commit comments

Comments
 (0)