mirror of
https://github.com/ultrasonic/ultrasonic
synced 2025-02-13 10:10:54 +01:00
Merge pull request #36 from ultrasonic/add-search3
Add new search3 api call.
This commit is contained in:
commit
35e74a758e
@ -0,0 +1,112 @@
|
||||
package org.moire.ultrasonic.api.subsonic
|
||||
|
||||
import org.amshove.kluent.`should equal to`
|
||||
import org.amshove.kluent.`should equal`
|
||||
import org.junit.Test
|
||||
import org.moire.ultrasonic.api.subsonic.models.Album
|
||||
import org.moire.ultrasonic.api.subsonic.models.Artist
|
||||
import org.moire.ultrasonic.api.subsonic.models.MusicDirectoryChild
|
||||
|
||||
/**
|
||||
* Integration test for [SubsonicAPIClient] for search3 call.
|
||||
*/
|
||||
class SubsonicApiSearchThreeTest : SubsonicAPIClientTest() {
|
||||
@Test
|
||||
fun `Should parse error response`() {
|
||||
checkErrorCallParsed(mockWebServerRule, {
|
||||
client.api.search3("some-query").execute()
|
||||
})
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should parse ok response`() {
|
||||
mockWebServerRule.enqueueResponse("search3_ok.json")
|
||||
|
||||
val response = client.api.search3("some-query").execute()
|
||||
|
||||
assertResponseSuccessful(response)
|
||||
with(response.body().searchResult) {
|
||||
artistList.size `should equal to` 1
|
||||
artistList[0] `should equal` Artist(id = 505, name = "The Prodigy", coverArt = "ar-505",
|
||||
albumCount = 5)
|
||||
albumList.size `should equal to` 1
|
||||
albumList[0] `should equal` Album(id = 855, name = "Always Outnumbered, Never Outgunned",
|
||||
artist = "The Prodigy", artistId = 505, coverArt = "al-855", songCount = 12,
|
||||
duration = 3313, created = parseDate("2016-10-23T20:57:27.000Z"),
|
||||
year = 2004, genre = "Electronic")
|
||||
songList.size `should equal to` 1
|
||||
songList[0] `should equal` MusicDirectoryChild(id = 5831, parent = 5766, isDir = false,
|
||||
title = "You'll Be Under My Wheels", album = "Need for Speed Most Wanted",
|
||||
artist = "The Prodigy", track = 17, year = 2005, genre = "Rap",
|
||||
coverArt = "5766", size = 5607024, contentType = "audio/mpeg",
|
||||
suffix = "mp3", duration = 233, bitRate = 192,
|
||||
path = "Compilations/Need for Speed Most Wanted/17 You'll Be Under My Wheels.mp3",
|
||||
isVideo = false, playCount = 0, discNumber = 1,
|
||||
created = parseDate("2016-10-23T20:09:02.000Z"), albumId = 568,
|
||||
artistId = 505, type = "music")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should pass query as request param`() {
|
||||
val query = "some-wip-query"
|
||||
|
||||
mockWebServerRule.assertRequestParam(responseResourceName = "search3_ok.json", apiRequest = {
|
||||
client.api.search3(query = query).execute()
|
||||
}, expectedParam = "query=$query")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should pass artist count as request param`() {
|
||||
val artistCount = 67
|
||||
|
||||
mockWebServerRule.assertRequestParam(responseResourceName = "search3_ok.json", apiRequest = {
|
||||
client.api.search3("some", artistCount = artistCount).execute()
|
||||
}, expectedParam = "artistCount=$artistCount")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should pass artist offset as request param`() {
|
||||
val artistOffset = 34
|
||||
|
||||
mockWebServerRule.assertRequestParam(responseResourceName = "search3_ok.json", apiRequest = {
|
||||
client.api.search3("some", artistOffset = artistOffset).execute()
|
||||
}, expectedParam = "artistOffset=$artistOffset")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should pass album count as request param`() {
|
||||
val albumCount = 21
|
||||
|
||||
mockWebServerRule.assertRequestParam(responseResourceName = "search3_ok.json", apiRequest = {
|
||||
client.api.search3("some", albumCount = albumCount).execute()
|
||||
}, expectedParam = "albumCount=$albumCount")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should pass album offset as request param`() {
|
||||
val albumOffset = 43
|
||||
|
||||
mockWebServerRule.assertRequestParam(responseResourceName = "search3_ok.json", apiRequest = {
|
||||
client.api.search3("some", albumOffset = albumOffset).execute()
|
||||
}, expectedParam = "albumOffset=$albumOffset")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should pass song count as request param`() {
|
||||
val songCount = 15
|
||||
|
||||
mockWebServerRule.assertRequestParam(responseResourceName = "search3_ok.json", apiRequest = {
|
||||
client.api.search3("some", songCount = songCount).execute()
|
||||
}, expectedParam = "songCount=$songCount")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should pass music folder id as request param`() {
|
||||
val musicFolderId = 43L
|
||||
|
||||
mockWebServerRule.assertRequestParam(responseResourceName = "search3_ok.json", apiRequest = {
|
||||
client.api.search3("some", musicFolderId = musicFolderId).execute()
|
||||
}, expectedParam = "musicFolderId=$musicFolderId")
|
||||
}
|
||||
}
|
51
subsonic-api/src/integrationTest/resources/search3_ok.json
Normal file
51
subsonic-api/src/integrationTest/resources/search3_ok.json
Normal file
@ -0,0 +1,51 @@
|
||||
{
|
||||
"subsonic-response" : {
|
||||
"status" : "ok",
|
||||
"version" : "1.15.0",
|
||||
"searchResult3" : {
|
||||
"artist" : [ {
|
||||
"id" : "505",
|
||||
"name" : "The Prodigy",
|
||||
"coverArt" : "ar-505",
|
||||
"albumCount" : 5
|
||||
} ],
|
||||
"album" : [ {
|
||||
"id" : "855",
|
||||
"name" : "Always Outnumbered, Never Outgunned",
|
||||
"artist" : "The Prodigy",
|
||||
"artistId" : "505",
|
||||
"coverArt" : "al-855",
|
||||
"songCount" : 12,
|
||||
"duration" : 3313,
|
||||
"created" : "2016-10-23T20:57:27.000Z",
|
||||
"year" : 2004,
|
||||
"genre" : "Electronic"
|
||||
} ],
|
||||
"song" : [ {
|
||||
"id" : "5831",
|
||||
"parent" : "5766",
|
||||
"isDir" : false,
|
||||
"title" : "You'll Be Under My Wheels",
|
||||
"album" : "Need for Speed Most Wanted",
|
||||
"artist" : "The Prodigy",
|
||||
"track" : 17,
|
||||
"year" : 2005,
|
||||
"genre" : "Rap",
|
||||
"coverArt" : "5766",
|
||||
"size" : 5607024,
|
||||
"contentType" : "audio/mpeg",
|
||||
"suffix" : "mp3",
|
||||
"duration" : 233,
|
||||
"bitRate" : 192,
|
||||
"path" : "Compilations/Need for Speed Most Wanted/17 You'll Be Under My Wheels.mp3",
|
||||
"isVideo" : false,
|
||||
"playCount" : 0,
|
||||
"discNumber" : 1,
|
||||
"created" : "2016-10-23T20:09:02.000Z",
|
||||
"albumId" : "568",
|
||||
"artistId" : "505",
|
||||
"type" : "music"
|
||||
} ]
|
||||
}
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@ import org.moire.ultrasonic.api.subsonic.response.LicenseResponse
|
||||
import org.moire.ultrasonic.api.subsonic.response.MusicFoldersResponse
|
||||
import org.moire.ultrasonic.api.subsonic.response.SearchTwoResponse
|
||||
import org.moire.ultrasonic.api.subsonic.response.SearchResponse
|
||||
import org.moire.ultrasonic.api.subsonic.response.SearchThreeResponse
|
||||
import org.moire.ultrasonic.api.subsonic.response.SubsonicResponse
|
||||
import retrofit2.Call
|
||||
import retrofit2.http.GET
|
||||
@ -73,4 +74,13 @@ interface SubsonicAPIDefinition {
|
||||
@Query("albumOffset") albumOffset: Int? = null,
|
||||
@Query("songCount") songCount: Int? = null,
|
||||
@Query("musicFolderId") musicFolderId: Long? = null): Call<SearchTwoResponse>
|
||||
|
||||
@GET("search3.view")
|
||||
fun search3(@Query("query") query: String,
|
||||
@Query("artistCount") artistCount: Int? = null,
|
||||
@Query("artistOffset") artistOffset: Int? = null,
|
||||
@Query("albumCount") albumCount: Int? = null,
|
||||
@Query("albumOffset") albumOffset: Int? = null,
|
||||
@Query("songCount") songCount: Int? = null,
|
||||
@Query("musicFolderId") musicFolderId: Long? = null): Call<SearchThreeResponse>
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
package org.moire.ultrasonic.api.subsonic.models
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
|
||||
data class SearchThreeResult(
|
||||
@JsonProperty("artist") val artistList: List<Artist> = emptyList(),
|
||||
@JsonProperty("album") val albumList: List<Album> = emptyList(),
|
||||
@JsonProperty("song") val songList: List<MusicDirectoryChild> = emptyList()
|
||||
)
|
@ -0,0 +1,13 @@
|
||||
package org.moire.ultrasonic.api.subsonic.response
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty
|
||||
import org.moire.ultrasonic.api.subsonic.SubsonicAPIVersions
|
||||
import org.moire.ultrasonic.api.subsonic.SubsonicError
|
||||
import org.moire.ultrasonic.api.subsonic.models.SearchThreeResult
|
||||
|
||||
class SearchThreeResponse(
|
||||
status: Status,
|
||||
version: SubsonicAPIVersions,
|
||||
error: SubsonicError?,
|
||||
@JsonProperty("searchResult3") val searchResult: SearchThreeResult = SearchThreeResult())
|
||||
: SubsonicResponse(status, version, error)
|
@ -64,6 +64,7 @@ import org.moire.ultrasonic.api.subsonic.response.GetMusicDirectoryResponse;
|
||||
import org.moire.ultrasonic.api.subsonic.response.LicenseResponse;
|
||||
import org.moire.ultrasonic.api.subsonic.response.MusicFoldersResponse;
|
||||
import org.moire.ultrasonic.api.subsonic.response.SearchResponse;
|
||||
import org.moire.ultrasonic.api.subsonic.response.SearchThreeResponse;
|
||||
import org.moire.ultrasonic.api.subsonic.response.SearchTwoResponse;
|
||||
import org.moire.ultrasonic.api.subsonic.response.SubsonicResponse;
|
||||
import org.moire.ultrasonic.data.APIConverter;
|
||||
@ -456,22 +457,21 @@ public class RESTMusicService implements MusicService
|
||||
return APIConverter.toDomainEntity(response.body().getSearchResult());
|
||||
}
|
||||
|
||||
private SearchResult search3(SearchCriteria criteria, Context context, ProgressListener progressListener) throws Exception
|
||||
{
|
||||
checkServerVersion(context, "1.8", "Searching by ID3 tag not supported.");
|
||||
private SearchResult search3(SearchCriteria criteria,
|
||||
Context context,
|
||||
ProgressListener progressListener) throws Exception {
|
||||
if (criteria.getQuery() == null) {
|
||||
throw new IllegalArgumentException("Query param is null");
|
||||
}
|
||||
|
||||
List<String> parameterNames = asList("query", "artistCount", "albumCount", "songCount");
|
||||
List<Object> parameterValues = Arrays.<Object>asList(criteria.getQuery(), criteria.getArtistCount(), criteria.getAlbumCount(), criteria.getSongCount());
|
||||
Reader reader = getReader(context, progressListener, "search3", null, parameterNames, parameterValues);
|
||||
try
|
||||
{
|
||||
return new SearchResult2Parser(context).parse(reader, progressListener, true);
|
||||
}
|
||||
finally
|
||||
{
|
||||
Util.close(reader);
|
||||
}
|
||||
}
|
||||
updateProgressListener(progressListener, R.string.parser_reading);
|
||||
Response<SearchThreeResponse> response = subsonicAPIClient.getApi().search3(criteria.getQuery(),
|
||||
criteria.getArtistCount(), null, criteria.getAlbumCount(), null,
|
||||
criteria.getSongCount(), null).execute();
|
||||
checkResponseSuccessful(response);
|
||||
|
||||
return APIConverter.toDomainEntity(response.body().getSearchResult());
|
||||
}
|
||||
|
||||
@Override
|
||||
public MusicDirectory getPlaylist(String id, String name, Context context, ProgressListener progressListener) throws Exception
|
||||
|
@ -5,6 +5,7 @@ package org.moire.ultrasonic.data
|
||||
import org.moire.ultrasonic.api.subsonic.models.Album
|
||||
import org.moire.ultrasonic.api.subsonic.models.Index
|
||||
import org.moire.ultrasonic.api.subsonic.models.MusicDirectoryChild
|
||||
import org.moire.ultrasonic.api.subsonic.models.SearchThreeResult
|
||||
import org.moire.ultrasonic.api.subsonic.models.SearchTwoResult
|
||||
import org.moire.ultrasonic.domain.Artist
|
||||
import org.moire.ultrasonic.domain.Indexes
|
||||
@ -41,6 +42,7 @@ fun APIArtist.toMusicDirectoryDomainEntity(): MusicDirectory = MusicDirectory().
|
||||
|
||||
fun Album.toDomainEntity(): MusicDirectory.Entry = MusicDirectory.Entry().apply {
|
||||
id = this@toDomainEntity.id.toString()
|
||||
setIsDirectory(true)
|
||||
title = this@toDomainEntity.name
|
||||
coverArt = this@toDomainEntity.coverArt
|
||||
artist = this@toDomainEntity.artist
|
||||
@ -92,5 +94,12 @@ fun APIMusicDirectory.toDomainEntity(): MusicDirectory = MusicDirectory().apply
|
||||
fun APISearchResult.toDomainEntity(): SearchResult = SearchResult(emptyList(), emptyList(),
|
||||
this.matchList.map { it.toDomainEntity() })
|
||||
|
||||
fun SearchTwoResult.toDomainEntity(): SearchResult = SearchResult(this.artistList.map { it.toDomainEntity() },
|
||||
this.albumList.map { it.toDomainEntity() }, this.songList.map { it.toDomainEntity() })
|
||||
fun SearchTwoResult.toDomainEntity(): SearchResult = SearchResult(
|
||||
this.artistList.map { it.toDomainEntity() },
|
||||
this.albumList.map { it.toDomainEntity() },
|
||||
this.songList.map { it.toDomainEntity() })
|
||||
|
||||
fun SearchThreeResult.toDomainEntity(): SearchResult = SearchResult(
|
||||
this.artistList.map { it.toDomainEntity() },
|
||||
this.albumList.map { it.toDomainEntity() },
|
||||
this.songList.map { it.toDomainEntity() })
|
||||
|
@ -14,6 +14,7 @@ import org.moire.ultrasonic.api.subsonic.models.MusicDirectory
|
||||
import org.moire.ultrasonic.api.subsonic.models.MusicDirectoryChild
|
||||
import org.moire.ultrasonic.api.subsonic.models.MusicFolder
|
||||
import org.moire.ultrasonic.api.subsonic.models.SearchResult
|
||||
import org.moire.ultrasonic.api.subsonic.models.SearchThreeResult
|
||||
import org.moire.ultrasonic.api.subsonic.models.SearchTwoResult
|
||||
import java.util.Calendar
|
||||
|
||||
@ -168,6 +169,7 @@ class APIConverterTest {
|
||||
with(convertedEntity) {
|
||||
id `should equal to` entity.id.toString()
|
||||
title `should equal to` entity.name
|
||||
isDirectory `should equal to` true
|
||||
coverArt `should equal to` entity.coverArt
|
||||
artist `should equal to` entity.artist
|
||||
artistId `should equal to` entity.artistId.toString()
|
||||
@ -235,6 +237,26 @@ class APIConverterTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Should convert SearchThreeResult to domain entity`() {
|
||||
val entity = SearchThreeResult(
|
||||
artistList = listOf(Artist(id = 612, name = "artist1")),
|
||||
albumList = listOf(Album(id = 221, name = "album1")),
|
||||
songList = listOf(MusicDirectoryChild(id = 7123, title = "song1"))
|
||||
)
|
||||
|
||||
val convertedEntity = entity.toDomainEntity()
|
||||
|
||||
with(convertedEntity) {
|
||||
artists.size `should equal to` entity.artistList.size
|
||||
artists[0] `should equal` entity.artistList[0].toDomainEntity()
|
||||
albums.size `should equal to` entity.albumList.size
|
||||
albums[0] `should equal` entity.albumList[0].toDomainEntity()
|
||||
songs.size `should equal to` entity.songList.size
|
||||
songs[0] `should equal` entity.songList[0].toDomainEntity()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createMusicFolder(id: Long = 0, name: String = ""): MusicFolder =
|
||||
MusicFolder(id, name)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user