Skip to content

Commit

Permalink
fix: new update yandex api
Browse files Browse the repository at this point in the history
  • Loading branch information
pank-su committed Dec 13, 2023
1 parent 44925a3 commit 8d10e19
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 14 deletions.
47 changes: 43 additions & 4 deletions library/src/commonMain/kotlin/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ import io.ktor.client.request.forms.*
import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import kotlinx.serialization.json.Json
import model.BasicResponse
import model.PermissionAlerts
import model.Result
import model.*
import model.account.Experiments
import model.account.PromoCodeStatus
import model.account.Status
Expand Down Expand Up @@ -53,6 +51,47 @@ class Client {
}
}

private suspend inline fun <reified T> requestPrimitive(
vararg components: String,
method: HttpMethod = HttpMethod.Get,
body: HashMap<String, String> = hashMapOf()
): ResultPrimitive<T> {
return httpClient.request(baseUrl) {
requestSettings()
this.method = method
url {
appendPathSegments(components.toList())
}
headers {

append("X-Yandex-Music-Client", "YandexMusicAndroid/24023231")
append("USER_AGENT", "Yandex-Music-API")
append("Accept-Language", language)

if (token != "") {
append(HttpHeaders.Authorization, "OAuth $token")
}
}
if (method == HttpMethod.Post) {
headers {
append(HttpHeaders.ContentType, "form-encoded")
}
formData {
parameters {
body.forEach {
append(it.key, it.value)
}
}
}
}
headers {
remove(HttpHeaders.ContentType)
remove(HttpHeaders.ContentLength)
}
}.body<BasicResponsePrimitive<T>>().result.apply { client = this@Client }
}


private suspend inline fun <reified T : Result> request(
components: List<String>,
method: HttpMethod = HttpMethod.Get,
Expand Down Expand Up @@ -149,7 +188,7 @@ class Client {

suspend fun permissionAlerts() = request<PermissionAlerts>("permission-alerts")

suspend fun accountExperiments() = request<Experiments>("account", "experiments")
suspend fun accountExperiments() = requestPrimitive<HashMap<String, String>>("account", "experiments")

suspend fun consumePromoCode(code: String) = requestForm<PromoCodeStatus>(
"account",
Expand Down
31 changes: 31 additions & 0 deletions library/src/commonMain/kotlin/model/BasicResponse.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ open class Result {
@Transient var client: Client? = null
}

@Serializable
open class ResultPrimitive<T>() {
var value: T? = null
@Transient
var invocationInfo: InvocationInfo? = null
@Transient
var client: Client? = null
}



@Serializable
Expand All @@ -29,4 +38,26 @@ data class BasicResponse<T>(
this.invocationInfo = this@BasicResponse.invocationInfo
}
}
}

@Serializable
data class BasicResponsePrimitive<T>(
val invocationInfo: InvocationInfo,
@SerialName("result") private val _result: T? = null,
@SerialName("error") private val _error: Error? = null
) {
val result: ResultPrimitive<T>
get() {
if (_result == null) {
errorTypeToException[_error?.type]!!(_error?.message ?: "Неизвестна ошибка")
throw Throwable()
}

val result = ResultPrimitive<T>()

return result.apply {
value = this@BasicResponsePrimitive._result
this.invocationInfo = this@BasicResponsePrimitive.invocationInfo
}
}
}
43 changes: 39 additions & 4 deletions library/src/commonMain/kotlin/model/album/Album.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,28 @@ enum class AlbumType {
Single,

@SerialName("compilation")
Compilation
Compilation,

@SerialName("podcast")
Podcast,

@SerialName("audiobook")
AudioBook
}

@Serializable
enum class MetaType {
@SerialName("music")
Music
Music,

@SerialName("podcast")
Podcast,

@SerialName("podcast-episode")
PodcastEpisode,

@SerialName("audiobook")
AudioBook
}

@Serializable
Expand Down Expand Up @@ -69,7 +84,27 @@ enum class Genre {
SoundTrack,

@SerialName("rusbards")
Rusbards
Rusbards,

@SerialName("ruspop")
RusPop,

@SerialName("dance")
Dance,

@SerialName("podcasts")
Podcasts,

edmgenre, breakbeatgenre,

@SerialName("dnb")
DrumAndBase,

@SerialName("soviet")
Soviet,

@SerialName("fairytales")
FairyTales
}

@Serializable
Expand All @@ -82,7 +117,7 @@ data class Album(
val releaseDate: Instant? = null,
val coverUri: String,
val ogImage: String,
val genre: Genre,
val genre: Genre? = null,
val trackCount: Int,
val likesCount: Int,
val recent: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ enum class GeneratedPlaylistType {
podcasts,

@JsonNames("missed_likes", "missedLikes")
MissedLikes
MissedLikes,

rewind2023
}

@Serializable
Expand Down
5 changes: 3 additions & 2 deletions library/src/commonTest/kotlin/AdsTest.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import dsl.client
import io.getenv
import kotlinx.coroutines.test.runTest
Expand All @@ -12,6 +11,8 @@ class AdsTest {
if (token == "") return@runTest
val ad = client { }.ads()

client { this.token = token }.ads()
client {
this.token = token
}.ads()
}
}
1 change: 0 additions & 1 deletion library/src/commonTest/kotlin/ConsumePromocodeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class ConsumePromoCodeTest {
this.token = token
}


println(client.consumePromoCode("test"))
}
}
4 changes: 3 additions & 1 deletion library/src/commonTest/kotlin/ExperimentsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import kotlin.test.Test
class ExperimentsTest {
@Test
fun gettingTest() = runTest{
client { }.accountExperiments()
client { }.accountExperiments().value?.forEach {
println("${it.key}: ${it.value}")
}
val token = getenv("token") ?: return@runTest
if (token == "") return@runTest
client {
Expand Down
5 changes: 4 additions & 1 deletion library/src/commonTest/kotlin/FeedTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dsl.client
import io.getenv
import kotlinx.coroutines.test.runTest
import model.playlist.CoverSize
import kotlin.test.Test

class FeedTest {
Expand All @@ -15,6 +16,8 @@ class FeedTest {
}
println(client.feed())


client.feed().generatedPlaylists.forEach {
println(it.data.getUrlBackgroundImage(CoverSize.`400x400`))
}
}
}

0 comments on commit 8d10e19

Please sign in to comment.