From 1012e416203fc57f17d4a4c38726f76defb8ce1d Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Sat, 11 Nov 2017 20:07:15 +0100 Subject: [PATCH 1/2] Add createShare api call. Signed-off-by: Yahor Berdnikau --- .../subsonic/SubsonicApiCreateShareTest.kt | 78 +++++++++++++++++++ .../api/subsonic/SubsonicApiGetSharesTest.kt | 2 +- .../resources/get_shares_ok.json | 2 +- .../api/subsonic/SubsonicAPIDefinition.kt | 5 ++ 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicApiCreateShareTest.kt diff --git a/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicApiCreateShareTest.kt b/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicApiCreateShareTest.kt new file mode 100644 index 00000000..caebfde4 --- /dev/null +++ b/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicApiCreateShareTest.kt @@ -0,0 +1,78 @@ +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.MusicDirectoryChild +import java.util.Calendar + +/** + * Instrumentation test for [SubsonicAPIDefinition.createShare] call. + */ +class SubsonicApiCreateShareTest : SubsonicAPIClientTest() { + @Test + fun `Should handle error responce`() { + val response = checkErrorCallParsed(mockWebServerRule) { + client.api.createShare(listOf("some-id")).execute() + } + + response.shares `should equal` emptyList() + } + + @Test + fun `Should handle ok response`() { + mockWebServerRule.enqueueResponse("get_shares_ok.json") + + val response = client.api.createShare(listOf("some-id")).execute() + + assertResponseSuccessful(response) + response.body().shares.size `should equal to` 1 + with(response.body().shares[0]) { + id `should equal to` 0 + url `should equal to` "https://subsonic.com/ext/share/awdwo?jwt=eyJhbGciOiJIUzI1NiJ9." + + "eyJwYXRoIjoiL2V4dC9zaGFyZS9hd2R3byIsImV4cCI6MTU0MTYyNjQzMX0.iy8dkt_ZZc8hJ692" + + "UxorHdHWFU2RB-fMCmCA4IJ_dTw" + username `should equal to` "admin" + created `should equal` parseDate("2017-11-07T21:33:51.748Z") + expires `should equal` parseDate("2018-11-07T21:33:51.748Z") + lastVisited `should equal` parseDate("2018-11-07T21:33:51.748Z") + description `should equal to` "Awesome link!" + visitCount `should equal to` 0 + items.size `should equal to` 1 + items[0] `should equal` MusicDirectoryChild(id = 4212, parent = 4186, isDir = false, + title = "Heaven Knows", album = "Going to Hell", artist = "The Pretty Reckless", + track = 3, year = 2014, genre = "Hard Rock", coverArt = "4186", size = 9025090, + contentType = "audio/mpeg", suffix = "mp3", duration = 225, bitRate = 320, + path = "The Pretty Reckless/Going to Hell/03 Heaven Knows.mp3", isVideo = false, + playCount = 2, discNumber = 1, created = parseDate("2016-10-23T21:30:40.000Z"), + albumId = 388, artistId = 238, type = "music") + } + } + + @Test + fun `Should pass ids in request param`() { + val idsList = listOf("some-id1", "some-id2") + mockWebServerRule.assertRequestParam(expectedParam = "id=${idsList[0]}&id=${idsList[1]}") { + client.api.createShare(idsList).execute() + } + } + + @Test + fun `Should pass description in request param`() { + val description = "description-banana" + + mockWebServerRule.assertRequestParam(expectedParam = "description=$description") { + client.api.createShare(idsToShare = listOf("id1", "id2"), description = description) + .execute() + } + } + + @Test + fun `Should pass expires in request param`() { + val expires = Calendar.getInstance().timeInMillis + + mockWebServerRule.assertRequestParam(expectedParam = "expires=$expires") { + client.api.createShare(idsToShare = listOf("id1"), expires = expires).execute() + } + } +} diff --git a/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicApiGetSharesTest.kt b/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicApiGetSharesTest.kt index dd98c2d2..8fc2dc28 100644 --- a/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicApiGetSharesTest.kt +++ b/subsonic-api/src/integrationTest/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicApiGetSharesTest.kt @@ -28,7 +28,7 @@ class SubsonicApiGetSharesTest : SubsonicAPIClientTest() { response.body().shares.size `should equal to` 1 with(response.body().shares[0]) { id `should equal to` 0 - url `should equal to` "https://airsonic.egorr.by/ext/share/awdwo?jwt=eyJhbGciOiJIUzI1" + + url `should equal to` "https://subsonic.com/ext/share/awdwo?jwt=eyJhbGciOiJIUzI1" + "NiJ9.eyJwYXRoIjoiL2V4dC9zaGFyZS9hd2R3byIsImV4cCI6MTU0MTYyNjQzMX0.iy8dkt_ZZc8" + "hJ692UxorHdHWFU2RB-fMCmCA4IJ_dTw" username `should equal to` "admin" diff --git a/subsonic-api/src/integrationTest/resources/get_shares_ok.json b/subsonic-api/src/integrationTest/resources/get_shares_ok.json index 1a165c94..b6c11b52 100644 --- a/subsonic-api/src/integrationTest/resources/get_shares_ok.json +++ b/subsonic-api/src/integrationTest/resources/get_shares_ok.json @@ -5,7 +5,7 @@ "shares" : { "share" : [ { "id" : "0", - "url" : "https://airsonic.egorr.by/ext/share/awdwo?jwt=eyJhbGciOiJIUzI1NiJ9.eyJwYXRoIjoiL2V4dC9zaGFyZS9hd2R3byIsImV4cCI6MTU0MTYyNjQzMX0.iy8dkt_ZZc8hJ692UxorHdHWFU2RB-fMCmCA4IJ_dTw", + "url" : "https://subsonic.com/ext/share/awdwo?jwt=eyJhbGciOiJIUzI1NiJ9.eyJwYXRoIjoiL2V4dC9zaGFyZS9hd2R3byIsImV4cCI6MTU0MTYyNjQzMX0.iy8dkt_ZZc8hJ692UxorHdHWFU2RB-fMCmCA4IJ_dTw", "username" : "admin", "created" : "2017-11-07T21:33:51.748Z", "expires" : "2018-11-07T21:33:51.748Z", diff --git a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIDefinition.kt b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIDefinition.kt index 4650afba..afce5798 100644 --- a/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIDefinition.kt +++ b/subsonic-api/src/main/kotlin/org/moire/ultrasonic/api/subsonic/SubsonicAPIDefinition.kt @@ -192,4 +192,9 @@ interface SubsonicAPIDefinition { @GET("getShares.view") fun getShares(): Call + + @GET("createShare.view") + fun createShare(@Query("id") idsToShare: List, + @Query("description") description: String? = null, + @Query("expires") expires: Long? = null): Call } From c6c640f0cf3dfa3d78084d187c7af2ecaac3fa4d Mon Sep 17 00:00:00 2001 From: Yahor Berdnikau Date: Sat, 11 Nov 2017 20:23:46 +0100 Subject: [PATCH 2/2] Use new api createShare call. Signed-off-by: Yahor Berdnikau --- .../ultrasonic/service/RESTMusicService.java | 47 +++--------- .../service/parser/ShareParser.java | 76 ------------------- 2 files changed, 12 insertions(+), 111 deletions(-) delete mode 100644 ultrasonic/src/main/java/org/moire/ultrasonic/service/parser/ShareParser.java diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/service/RESTMusicService.java b/ultrasonic/src/main/java/org/moire/ultrasonic/service/RESTMusicService.java index abd2a32e..cd1e809e 100644 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/service/RESTMusicService.java +++ b/ultrasonic/src/main/java/org/moire/ultrasonic/service/RESTMusicService.java @@ -113,7 +113,6 @@ import org.moire.ultrasonic.service.parser.ErrorParser; import org.moire.ultrasonic.service.parser.GenreParser; import org.moire.ultrasonic.service.parser.MusicDirectoryParser; import org.moire.ultrasonic.service.parser.RandomSongsParser; -import org.moire.ultrasonic.service.parser.ShareParser; import org.moire.ultrasonic.service.parser.SubsonicRESTException; import org.moire.ultrasonic.service.parser.UserInfoParser; import org.moire.ultrasonic.service.ssl.SSLSocketFactory; @@ -137,7 +136,6 @@ import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.concurrent.atomic.AtomicReference; @@ -1410,40 +1408,19 @@ public class RESTMusicService implements MusicService } } - @Override - public List createShare(List ids, String description, Long expires, Context context, ProgressListener progressListener) throws Exception - { - List parameterNames = new LinkedList(); - List parameterValues = new LinkedList(); + @Override + public List createShare(List ids, + String description, + Long expires, + Context context, + ProgressListener progressListener) throws Exception { + updateProgressListener(progressListener, R.string.parser_reading); + Response response = subsonicAPIClient.getApi() + .createShare(ids, description, expires).execute(); + checkResponseSuccessful(response); - for (String id : ids) - { - parameterNames.add("id"); - parameterValues.add(id); - } - - if (description != null) - { - parameterNames.add("description"); - parameterValues.add(description); - } - - if (expires > 0) - { - parameterNames.add("expires"); - parameterValues.add(expires); - } - - Reader reader = getReader(context, progressListener, "createShare", null, parameterNames, parameterValues); - try - { - return new ShareParser(context).parse(reader, progressListener); - } - finally - { - Util.close(reader); - } - } + return APIShareConverter.toDomainEntitiesList(response.body().getShares()); + } @Override public void deleteShare(String id, Context context, ProgressListener progressListener) throws Exception diff --git a/ultrasonic/src/main/java/org/moire/ultrasonic/service/parser/ShareParser.java b/ultrasonic/src/main/java/org/moire/ultrasonic/service/parser/ShareParser.java deleted file mode 100644 index d8054591..00000000 --- a/ultrasonic/src/main/java/org/moire/ultrasonic/service/parser/ShareParser.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.moire.ultrasonic.service.parser; - -import android.content.Context; - -import org.moire.ultrasonic.R; -import org.moire.ultrasonic.domain.Share; -import org.moire.ultrasonic.util.ProgressListener; - -import org.xmlpull.v1.XmlPullParser; - -import java.io.Reader; -import java.util.ArrayList; -import java.util.List; - -/** - * @author Joshua Bahnsen - */ -public class ShareParser extends MusicDirectoryEntryParser -{ - - public ShareParser(Context context) - { - super(context); - } - - public List parse(Reader reader, ProgressListener progressListener) throws Exception - { - - updateProgress(progressListener, R.string.parser_reading); - init(reader); - - List shares = new ArrayList(); - Share share = null; - int eventType; - - do - { - eventType = nextParseEvent(); - - if (eventType == XmlPullParser.START_TAG) - { - String name = getElementName(); - - if ("share".equals(name)) - { - share = new Share(); - share.setCreated(get("created")); - share.setDescription(get("description")); - share.setExpires(get("expires")); - share.setId(get("id")); - share.setLastVisited(get("lastVisited")); - share.setUrl(get("url")); - share.setUsername(get("username")); - share.setVisitCount(getLong("visitCount")); - shares.add(share); - } - else if ("entry".equals(name)) - { - if (share != null) - { - share.addEntry(parseEntry(null, false, 0)); - } - } - else if ("error".equals(name)) - { - handleError(); - } - } - } while (eventType != XmlPullParser.END_DOCUMENT); - - validate(); - updateProgress(progressListener, R.string.parser_reading_done); - - return shares; - } -} \ No newline at end of file