dev.arbjerg.lavalink.internal.LavalinkRestClient.kt Maven / Gradle / Ivy
package dev.arbjerg.lavalink.internal
import dev.arbjerg.lavalink.client.LavalinkNode
import dev.arbjerg.lavalink.client.http.HttpBuilder
import dev.arbjerg.lavalink.internal.error.RestException
import dev.arbjerg.lavalink.protocol.v4.*
import kotlinx.serialization.encodeToString
import okhttp3.Call
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.Response
import reactor.core.publisher.Mono
import java.io.IOException
import java.net.URLEncoder
import java.nio.charset.StandardCharsets
class LavalinkRestClient(val node: LavalinkNode) {
fun getPlayers(): Mono {
return newRequest {
path("/v4/sessions/${node.sessionId}/players")
}.toMono()
}
fun getPlayer(guildId: Long): Mono {
return newRequest {
path("/v4/sessions/${node.sessionId}/players/$guildId")
}.toMono()
}
fun updatePlayer(player: PlayerUpdate, guildId: Long, noReplace: Boolean = false): Mono {
return newRequest {
path("/v4/sessions/${node.sessionId}/players/$guildId?noReplace=$noReplace")
patch(json.encodeToString(player).toRequestBody("application/json".toMediaType()))
}.toMono()
}
fun destroyPlayer(guildId: Long): Mono {
return newRequest {
path("/v4/sessions/${node.sessionId}/players/$guildId")
delete()
}.toMono()
}
fun loadItem(identifier: String): Mono {
val encId = URLEncoder.encode(identifier, StandardCharsets.UTF_8)
return newRequest {
path("/v4/loadtracks?identifier=$encId")
}.toMono()
}
fun decodeTrack(encoded: String): Mono