mirror of
https://github.com/ultrasonic/ultrasonic
synced 2025-03-10 00:20:04 +01:00
Add getMusicFolders api request.
Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
This commit is contained in:
parent
4dec26de9c
commit
429f302f7e
@ -9,6 +9,7 @@ import org.junit.Before
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.moire.ultrasonic.api.subsonic.models.License
|
||||
import org.moire.ultrasonic.api.subsonic.models.MusicFolder
|
||||
import org.moire.ultrasonic.api.subsonic.response.SubsonicResponse
|
||||
import org.moire.ultrasonic.api.subsonic.rules.MockWebServerRule
|
||||
import retrofit2.Response
|
||||
@ -47,8 +48,7 @@ class SubsonicAPITest {
|
||||
|
||||
assertResponseSuccessful(response)
|
||||
with(response.body()) {
|
||||
status `should be` SubsonicResponse.Status.OK
|
||||
version `should be` SubsonicAPIVersions.V1_13_0
|
||||
assertBaseResponseOk()
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,8 +65,7 @@ class SubsonicAPITest {
|
||||
|
||||
assertResponseSuccessful(response)
|
||||
with(response.body()) {
|
||||
status `should be` SubsonicResponse.Status.OK
|
||||
version `should be` SubsonicAPIVersions.V1_13_0
|
||||
assertBaseResponseOk()
|
||||
license `should equal` License(true, parseDate("2016-11-23T20:17:15.206Z"))
|
||||
}
|
||||
}
|
||||
@ -78,6 +77,26 @@ class SubsonicAPITest {
|
||||
response.license `should be` null
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should parse get music folders ok response`() {
|
||||
enqueueResponse("get_music_directories_ok.json")
|
||||
|
||||
val response = api.getApi().getMusicFolders().execute()
|
||||
|
||||
assertResponseSuccessful(response)
|
||||
with(response.body()) {
|
||||
assertBaseResponseOk()
|
||||
musicFolders `should equal` listOf(MusicFolder(0, "Music"), MusicFolder(2, "Test"))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should parse get music folders error response`() {
|
||||
val response = checkErrorCallParsed { api.getApi().getMusicFolders().execute() }
|
||||
|
||||
response.musicFolders `should be` null
|
||||
}
|
||||
|
||||
private fun enqueueResponse(resourceName: String) {
|
||||
mockWebServerRule.mockWebServer.enqueue(MockResponse()
|
||||
.setBody(loadJsonResponse(resourceName)))
|
||||
@ -113,4 +132,10 @@ class SubsonicAPITest {
|
||||
}
|
||||
return response.body()
|
||||
}
|
||||
|
||||
private fun SubsonicResponse.assertBaseResponseOk() {
|
||||
status `should be` SubsonicResponse.Status.OK
|
||||
version `should be` SubsonicAPIVersions.V1_13_0
|
||||
error `should be` null
|
||||
}
|
||||
}
|
@ -6,6 +6,9 @@
|
||||
"musicFolder" : [ {
|
||||
"id" : 0,
|
||||
"name" : "Music"
|
||||
}, {
|
||||
"id" : 2,
|
||||
"name" : "Test"
|
||||
} ]
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package org.moire.ultrasonic.api.subsonic
|
||||
|
||||
import org.moire.ultrasonic.api.subsonic.response.LicenseResponse
|
||||
import org.moire.ultrasonic.api.subsonic.response.MusicFoldersResponse
|
||||
import org.moire.ultrasonic.api.subsonic.response.SubsonicResponse
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.GET
|
||||
@ -14,4 +15,7 @@ interface SubsonicAPIDefinition {
|
||||
|
||||
@GET("getLicense.view")
|
||||
fun getLicense(): Call<LicenseResponse>
|
||||
|
||||
@GET("getMusicFolders.view")
|
||||
fun getMusicFolders(): Call<MusicFoldersResponse>
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
package org.moire.ultrasonic.api.subsonic.models
|
||||
|
||||
data class MusicFolder(val id: Long, val name: String)
|
@ -0,0 +1,33 @@
|
||||
package org.moire.ultrasonic.api.subsonic.response
|
||||
|
||||
import com.fasterxml.jackson.core.JsonParser
|
||||
import com.fasterxml.jackson.core.JsonToken
|
||||
import com.fasterxml.jackson.databind.DeserializationContext
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer
|
||||
import com.fasterxml.jackson.databind.JsonMappingException
|
||||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
|
||||
import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions
|
||||
import org.moire.ultrasonic.api.subsonic.SubsonicError
|
||||
import org.moire.ultrasonic.api.subsonic.models.MusicFolder
|
||||
|
||||
class MusicFoldersResponse(status: Status,
|
||||
version: SubsonicAPIVersions,
|
||||
error: SubsonicError?,
|
||||
@JsonDeserialize(using = MusicFoldersDeserializer::class)
|
||||
val musicFolders: List<MusicFolder>?):
|
||||
SubsonicResponse(status, version, error) {
|
||||
companion object {
|
||||
class MusicFoldersDeserializer(): JsonDeserializer<List<MusicFolder>>() {
|
||||
override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): List<MusicFolder> {
|
||||
p!!.nextToken()
|
||||
if (p.currentName == "musicFolder" && p.nextToken() == JsonToken.START_ARRAY) {
|
||||
val mfJavaType = ctxt!!.typeFactory
|
||||
.constructCollectionType(List::class.java, MusicFolder::class.java)
|
||||
return ctxt.readValue(p, mfJavaType)
|
||||
}
|
||||
|
||||
throw JsonMappingException(p, "Failed to parse music folders list")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user