Skip to content

Commit

Permalink
v0.1.2 - Fix Player Endpoints (#8)
Browse files Browse the repository at this point in the history
* Hotfix KtifyBuilder URL generation

* Edit version in README

* Fix endpoints and KtifyBuilder authorisation code

* Edit README version
  • Loading branch information
warriorzz authored May 8, 2024
1 parent d5f340c commit 78124af
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repositories {
mavenCentral()
}
dependencies {
implementation("ee.bjarn", "ktify", "0.1.1-hotfix")
implementation("ee.bjarn", "ktify", "0.1.2")
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

group = "ee.bjarn"
version = "0.1.1-hotfix"
version = "0.1.2"

repositories {
mavenCentral()
Expand Down
15 changes: 8 additions & 7 deletions src/main/kotlin/ee/bjarn/ktify/Ktify.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.serialization.json.JsonObject
import mu.KotlinLogging
import java.net.URLEncoder
import java.util.UUID
import kotlin.io.encoding.Base64

/**
* The main wrapper class
Expand Down Expand Up @@ -112,14 +113,14 @@ class KtifyBuilder(
* @param authorizationCode returned by the request to the user
* @return The [Ktify] instance
*/
@OptIn(InternalAPI::class)
@OptIn(InternalAPI::class, kotlin.io.encoding.ExperimentalEncodingApi::class)
suspend fun build(authorizationCode: String): Ktify {
val clientCredentialsResponse: ClientCredentialsResponse =
ktifyHttpClient.post("https://accounts.spotify.com/api/token") {
header("Content-Type", "application/x-www-form-urlencoded")
body =
"grant_type=authorization_code&client_id=$clientId&client_secret=$clientSecret&redirect_uri=$redirectUri&code=$authorizationCode"
}.body()
val clientCredentialsResponse: ClientCredentialsResponse = ktifyHttpClient.post("https://accounts.spotify.com/api/token") {
header("Content-Type", "application/x-www-form-urlencoded")
header("Authorization", "Basic ${Base64.encode("$clientId:$clientSecret".encodeToByteArray())}")
body =
"grant_type=authorization_code&redirect_uri=${URLEncoder.encode(redirectUri, "UTF-8")}&code=$authorizationCode"
}.body()
return Ktify(
ClientCredentials(
clientId,
Expand Down
38 changes: 34 additions & 4 deletions src/main/kotlin/ee/bjarn/ktify/model/player/CurrentPlayback.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ee.bjarn.ktify.model.player

import ee.bjarn.ktify.model.Episode
import ee.bjarn.ktify.model.Track
import ee.bjarn.ktify.model.track.TrackActions
import ee.bjarn.ktify.model.util.Context
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.PrimitiveKind
Expand Down Expand Up @@ -32,6 +31,8 @@ sealed class CurrentPlayback {
@SerialName("repeat_state")
abstract val repeatState: String?
abstract val context: Context?

abstract val actions: CurrentPlaybackActions
}

@Serializable
Expand All @@ -49,8 +50,10 @@ class CurrentPlayingTrack(
@SerialName("repeat_state")
override val repeatState: String? = null,
override val context: Context?,
override val actions: CurrentPlaybackActions,
@SerialName("smart_shuffle")
val smartShuffle: Boolean?,
val item: Track? = null,
val actions: TrackActions,
val resuming: Boolean? = null
) : CurrentPlayback()

Expand All @@ -69,8 +72,8 @@ class CurrentPlayingEpisode(
@SerialName("repeat_state")
override val repeatState: String? = null,
override val context: Context?,
override val actions: CurrentPlaybackActions,
val item: Episode? = null,
val actions: TrackActions,
val resuming: Boolean? = null
) : CurrentPlayback()

Expand All @@ -90,9 +93,36 @@ class CurrentPlaybackNull(
override val shuffleState: Boolean? = null,
@SerialName("repeat_state")
override val repeatState: String? = null,
override val context: Context?
override val context: Context?,
override val actions: CurrentPlaybackActions,
) : CurrentPlayback()

@Serializable
data class CurrentPlaybackActions(
val disallows: Disallows
)

@Serializable
data class Disallows(
@SerialName("interrupting_playback")
val interruptingPlayback: Boolean? = null,
val pausing: Boolean? = null,
val resuming: Boolean? = null,
val seeking: Boolean? = null,
@SerialName("skipping_next")
val skippingNext: Boolean? = null,
@SerialName("skipping_prev")
val skippingPrev: Boolean? = null,
@SerialName("toggling_repeat_context")
val togglingRepeatContext: Boolean? = null,
@SerialName("toggling_shuffle")
val togglingShuffle: Boolean? = null,
@SerialName("toggling_repeat_track")
val togglingRepeatTrack: Boolean? = null,
@SerialName("transferring_playback")
val transferringPlayback: Boolean? = null,
)

object CurrentPlaybackSerializer : JsonContentPolymorphicSerializer<CurrentPlayback>(CurrentPlayback::class) {
override fun selectDeserializer(element: JsonElement): DeserializationStrategy<CurrentPlayback> {
return when (element.jsonObject["item"]?.jsonObject?.get("type")?.jsonPrimitive?.content) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/ee/bjarn/ktify/model/player/Devices.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ data class Device(
val name: String,
val type: String,
@SerialName("volume_percent")
val volumePercent: Int
val volumePercent: Int,
@SerialName("supports_volume")
val supportsVolume: Boolean,
)
13 changes: 0 additions & 13 deletions src/main/kotlin/ee/bjarn/ktify/model/track/Track.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,6 @@ data class SavedTrack(
val track: Track,
)

@Serializable
data class TrackActions(
@SerialName("is_playing")
val isPlaying: Boolean? = null,
val disallows: TrackActionsDisallows
)

@Serializable
data class TrackActionsDisallows(
val pausing: Boolean? = null,
val resuming: Boolean? = null
)

@Serializable
data class TrackRestriction(
val reason: RestrictionType
Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/ee/bjarn/ktify/model/util/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@ enum class ObjectType {

@SerialName("user")
USER,

@SerialName("collection")
COLLECTION,
}
1 change: 1 addition & 0 deletions src/main/kotlin/ee/bjarn/ktify/player/KtifyPlayer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class KtifyPlayer internal constructor(val ktify: Ktify) {
) {
method = HttpMethod.Put
url.takeFrom(ktify.requestHelper.baseUrl + "me/player/play")
header("Content-Type", "application/json")
if (deviceId != null) {
parameter("device_id", deviceId)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/ee/bjarn/ktify/utils/RequestHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal class RequestHelper(
}
if (requiresScope != null) { require(clientCredentials.scopes?.contains(requiresScope) ?: false) }
val responseData: HttpResponse =
makeRequest(requiresAuthentication = true, client = ktify.jsonLessHttpClient, builder = builder)
makeRequest(requiresAuthentication = true, builder = builder)
return responseData.status
}

Expand Down

0 comments on commit 78124af

Please sign in to comment.