nvllsvm/audinaut#37 Use query parameters instead of form bodies.

This commit is contained in:
dddddd-mmmmmm 2019-03-01 15:28:58 +11:00
parent 2015039c3d
commit 13ffabdb20
1 changed files with 106 additions and 136 deletions

View File

@ -55,6 +55,7 @@ import net.nullsum.audinaut.util.Util;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -102,19 +103,21 @@ public class RESTMusicService implements MusicService {
@Override @Override
public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception { public Indexes getIndexes(String musicFolderId, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "getArtists", null);
Builder builder = new FormBody.Builder();
Map<String, String> parameters = new HashMap<>();
if (musicFolderId != null) { if (musicFolderId != null) {
builder.add("musicFolderId", musicFolderId); parameters.put("musicFolderId", musicFolderId);
} else {
parameters = null;
} }
RequestBody formBody = builder.build(); String url = getRestUrl(context, "getArtists", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -160,15 +163,14 @@ public class RESTMusicService implements MusicService {
} }
private MusicDirectory getMusicDirectoryImpl(String id, String name, Context context) throws Exception { private MusicDirectory getMusicDirectoryImpl(String id, String name, Context context) throws Exception {
String url = getRestUrl(context, "getMusicDirectory", null); Map<String, String> parameters = new HashMap<>();
RequestBody formBody = new FormBody.Builder() parameters.put("id", id);
.add("id", id)
.build(); String url = getRestUrl(context, "getMusicDirectory", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -178,15 +180,15 @@ public class RESTMusicService implements MusicService {
@Override @Override
public MusicDirectory getArtist(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception { public MusicDirectory getArtist(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "getArtist", null);
RequestBody formBody = new FormBody.Builder() Map<String, String> parameters = new HashMap<>();
.add("id", id)
.build(); parameters.put("id", id);
String url = getRestUrl(context, "getArtist", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -196,15 +198,14 @@ public class RESTMusicService implements MusicService {
@Override @Override
public MusicDirectory getAlbum(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception { public MusicDirectory getAlbum(String id, String name, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "getAlbum", null); Map<String, String> parameters = new HashMap<>();
RequestBody formBody = new FormBody.Builder() parameters.put("id", id);
.add("id", id)
.build(); String url = getRestUrl(context, "getAlbum", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -214,20 +215,17 @@ public class RESTMusicService implements MusicService {
@Override @Override
public SearchResult search(SearchCritera critera, Context context, ProgressListener progressListener) throws Exception { public SearchResult search(SearchCritera critera, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "search3", null); Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder(); parameters.put("query", critera.getQuery());
parameters.put("artistCount", Integer.toString(critera.getArtistCount()));
parameters.put("albumCount", Integer.toString(critera.getAlbumCount()));
parameters.put("songCount", Integer.toString(critera.getSongCount()));
builder.add("query", critera.getQuery()); String url = getRestUrl(context, "search3", parameters);
builder.add("artistCount", Integer.toString(critera.getArtistCount()));
builder.add("albumCount", Integer.toString(critera.getAlbumCount()));
builder.add("songCount", Integer.toString(critera.getSongCount()));
RequestBody formBody = builder.build();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -237,15 +235,14 @@ public class RESTMusicService implements MusicService {
@Override @Override
public MusicDirectory getPlaylist(boolean refresh, String id, String name, Context context, ProgressListener progressListener) throws Exception { public MusicDirectory getPlaylist(boolean refresh, String id, String name, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "getPlaylist", null); Map<String, String> parameters = new HashMap<>();
RequestBody formBody = new FormBody.Builder() parameters.put("id", id);
.add("id", id)
.build(); String url = getRestUrl(context, "getPlaylist", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -268,27 +265,24 @@ public class RESTMusicService implements MusicService {
@Override @Override
public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception { public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "createPlaylist", null); Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder();
if (id != null) { if (id != null) {
builder.add("playlistId", id); parameters.put("playlistId", id);
} }
if (name != null) { if (name != null) {
builder.add("name", name); parameters.put("name", name);
} }
for (MusicDirectory.Entry entry : entries) { for (MusicDirectory.Entry entry : entries) {
builder.add("songId", getOfflineSongId(entry.getId(), context, progressListener)); parameters.put("songId", getOfflineSongId(entry.getId(), context, progressListener));
} }
RequestBody formBody = builder.build(); String url = getRestUrl(context, "createPlaylist", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -298,15 +292,14 @@ public class RESTMusicService implements MusicService {
@Override @Override
public void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception { public void deletePlaylist(String id, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "deletePlaylist", null); Map<String, String> parameters = new HashMap<>();
RequestBody formBody = new FormBody.Builder() parameters.put("id", id);
.add("id", id)
.build(); String url = getRestUrl(context, "deletePlaylist", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -316,19 +309,17 @@ public class RESTMusicService implements MusicService {
@Override @Override
public void addToPlaylist(String id, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception { public void addToPlaylist(String id, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "updatePlaylist", null); Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder(); parameters.put("playlistId", id);
builder.add("playlistId", id);
for (MusicDirectory.Entry song : toAdd) { for (MusicDirectory.Entry song : toAdd) {
builder.add("songIdToAdd", getOfflineSongId(song.getId(), context, progressListener)); parameters.put("songIdToAdd", getOfflineSongId(song.getId(), context, progressListener));
} }
RequestBody formBody = builder.build(); String url = getRestUrl(context, "updatePlaylist", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -338,20 +329,18 @@ public class RESTMusicService implements MusicService {
@Override @Override
public void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception { public void removeFromPlaylist(String id, List<Integer> toRemove, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "updatePlaylist", null); Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder(); parameters.put("playlistId", id);
builder.add("playlistId", id);
for (Integer song : toRemove) { for (Integer song : toRemove) {
builder.add("songIndexToRemove", Integer.toString(song)); parameters.put("songIndexToRemove", Integer.toString(song));
} }
RequestBody formBody = builder.build(); String url = getRestUrl(context, "updatePlaylist", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -361,25 +350,23 @@ public class RESTMusicService implements MusicService {
@Override @Override
public void overwritePlaylist(String id, String name, int toRemove, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception { public void overwritePlaylist(String id, String name, int toRemove, List<MusicDirectory.Entry> toAdd, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "updatePlaylist", null); Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder(); parameters.put("playlistId", id);
builder.add("playlistId", id); parameters.put("name", name);
builder.add("name", name);
for (MusicDirectory.Entry song : toAdd) { for (MusicDirectory.Entry song : toAdd) {
builder.add("songIdToAdd", getOfflineSongId(song.getId(), context, progressListener)); parameters.put("songIdToAdd", getOfflineSongId(song.getId(), context, progressListener));
} }
for (int i = 0; i < toRemove; i++) { for (int i = 0; i < toRemove; i++) {
builder.add("songIndexToRemove", Integer.toString(i)); parameters.put("songIndexToRemove", Integer.toString(i));
} }
RequestBody formBody = builder.build(); String url = getRestUrl(context, "updatePlaylist", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -389,19 +376,17 @@ public class RESTMusicService implements MusicService {
@Override @Override
public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception { public void updatePlaylist(String id, String name, String comment, boolean pub, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "updatePlaylist", null); Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder(); parameters.put("playlistId", id);
builder.add("playlistId", id); parameters.put("name", name);
builder.add("name", name); parameters.put("comment", comment);
builder.add("comment", comment); parameters.put("public", Boolean.toString(pub));
builder.add("public", Boolean.toString(pub));
RequestBody formBody = builder.build(); String url = getRestUrl(context, "updatePlaylist", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -411,27 +396,25 @@ public class RESTMusicService implements MusicService {
@Override @Override
public MusicDirectory getAlbumList(String type, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception { public MusicDirectory getAlbumList(String type, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "getAlbumList2", null); Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder(); parameters.put("type", type);
builder.add("type", type); parameters.put("size", Integer.toString(size));
builder.add("size", Integer.toString(size)); parameters.put("offset", Integer.toString(offset));
builder.add("offset", Integer.toString(offset));
// Add folder if it was set and is non null // Add folder if it was set and is non null
int instance = getInstance(context); int instance = getInstance(context);
if (Util.getAlbumListsPerFolder(context, instance)) { if (Util.getAlbumListsPerFolder(context, instance)) {
String folderId = Util.getSelectedMusicFolderId(context, instance); String folderId = Util.getSelectedMusicFolderId(context, instance);
if (folderId != null) { if (folderId != null) {
builder.add("musicFolderId", folderId); parameters.put("musicFolderId", folderId);
} }
} }
RequestBody formBody = builder.build(); String url = getRestUrl(context, "getAlbumList2", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -441,37 +424,35 @@ public class RESTMusicService implements MusicService {
@Override @Override
public MusicDirectory getAlbumList(String type, String extra, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception { public MusicDirectory getAlbumList(String type, String extra, int size, int offset, boolean refresh, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "getAlbumList2", null); Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder(); parameters.put("size", Integer.toString(size));
builder.add("size", Integer.toString(size)); parameters.put("offset", Integer.toString(offset));
builder.add("offset", Integer.toString(offset));
int instance = getInstance(context); int instance = getInstance(context);
if ("genres".equals(type)) { if ("genres".equals(type)) {
builder.add("type", "byGenre"); parameters.put("type", "byGenre");
builder.add("genre", extra); parameters.put("genre", extra);
} else if ("years".equals(type)) { } else if ("years".equals(type)) {
int decade = Integer.parseInt(extra); int decade = Integer.parseInt(extra);
builder.add("type", "byYear"); parameters.put("type", "byYear");
builder.add("fromYear", Integer.toString(decade + 9)); parameters.put("fromYear", Integer.toString(decade + 9));
builder.add("toYear", Integer.toString(decade)); parameters.put("toYear", Integer.toString(decade));
} }
// Add folder if it was set and is non null // Add folder if it was set and is non null
if (Util.getAlbumListsPerFolder(context, instance)) { if (Util.getAlbumListsPerFolder(context, instance)) {
String folderId = Util.getSelectedMusicFolderId(context, instance); String folderId = Util.getSelectedMusicFolderId(context, instance);
if (folderId != null) { if (folderId != null) {
builder.add("musicFolderId", folderId); parameters.put("musicFolderId", folderId);
} }
} }
RequestBody formBody = builder.build(); String url = getRestUrl(context, "getAlbumList2", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -481,9 +462,10 @@ public class RESTMusicService implements MusicService {
@Override @Override
public MusicDirectory getSongList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception { public MusicDirectory getSongList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception {
Builder builder = new FormBody.Builder(); Map<String, String> parameters = new HashMap<>();
builder.add("size", Integer.toString(size));
builder.add("offset", Integer.toString(offset)); parameters.put("size", Integer.toString(size));
parameters.put("offset", Integer.toString(offset));
String method; String method;
switch (type) { switch (type) {
@ -503,13 +485,10 @@ public class RESTMusicService implements MusicService {
method = "getNewaddedSongs"; method = "getNewaddedSongs";
} }
String url = getRestUrl(context, method, null); String url = getRestUrl(context, method, parameters);
RequestBody formBody = builder.build();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -519,11 +498,12 @@ public class RESTMusicService implements MusicService {
@Override @Override
public MusicDirectory getRandomSongs(int size, String musicFolderId, String genre, String startYear, String endYear, Context context, ProgressListener progressListener) throws Exception { public MusicDirectory getRandomSongs(int size, String musicFolderId, String genre, String startYear, String endYear, Context context, ProgressListener progressListener) throws Exception {
Builder builder = new FormBody.Builder(); Map<String, String> parameters = new HashMap<>();
builder.add("size", Integer.toString(size));
parameters.put("size", Integer.toString(size));
if (genre != null && !"".equals(genre)) { if (genre != null && !"".equals(genre)) {
builder.add("genre", genre); parameters.put("genre", genre);
} }
if (startYear != null && !"".equals(startYear)) { if (startYear != null && !"".equals(startYear)) {
// Check to make sure user isn't doing 2015 -> 2010 since Subsonic will return no results // Check to make sure user isn't doing 2015 -> 2010 since Subsonic will return no results
@ -542,19 +522,16 @@ public class RESTMusicService implements MusicService {
} }
} }
builder.add("fromYear", startYear); parameters.put("fromYear", startYear);
} }
if (endYear != null && !"".equals(endYear)) { if (endYear != null && !"".equals(endYear)) {
builder.add("toYear", endYear); parameters.put("toYear", endYear);
} }
String url = getRestUrl(context, "getRandomSongs", null); String url = getRestUrl(context, "getRandomSongs", parameters);
RequestBody formBody = builder.build();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -574,16 +551,14 @@ public class RESTMusicService implements MusicService {
return bitmap; return bitmap;
} }
String url = getRestUrl(context, "getCoverArt", null); Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder(); parameters.put("id", entry.getCoverArt());
builder.add("id", entry.getCoverArt());
RequestBody formBody = builder.build(); String url = getRestUrl(context, "getCoverArt", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -621,13 +596,12 @@ public class RESTMusicService implements MusicService {
.readTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS)
.build(); .build();
String url = getRestUrl(context, "stream", null); Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder(); parameters.put("id", song.getId());
builder.add("id", song.getId()); parameters.put("maxBitRate", Integer.toString(maxBitrate));
builder.add("maxBitRate", Integer.toString(maxBitrate));
RequestBody formBody = builder.build(); String url = getRestUrl(context, "stream", parameters);
Request.Builder requestBuilder = new Request.Builder(); Request.Builder requestBuilder = new Request.Builder();
if (offset > 0) { if (offset > 0) {
@ -635,7 +609,6 @@ public class RESTMusicService implements MusicService {
} }
requestBuilder.url(url); requestBuilder.url(url);
requestBuilder.post(formBody);
Request request = requestBuilder.build(); Request request = requestBuilder.build();
@ -658,27 +631,25 @@ public class RESTMusicService implements MusicService {
@Override @Override
public MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception { public MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception {
Builder builder = new FormBody.Builder(); Map<String, String> parameters = new HashMap<>();
builder.add("genre", genre);
builder.add("count", Integer.toString(count)); parameters.put("genre", genre);
builder.add("offset", Integer.toString(offset)); parameters.put("count", Integer.toString(count));
parameters.put("offset", Integer.toString(offset));
// Add folder if it was set and is non null // Add folder if it was set and is non null
int instance = getInstance(context); int instance = getInstance(context);
if (Util.getAlbumListsPerFolder(context, instance)) { if (Util.getAlbumListsPerFolder(context, instance)) {
String folderId = Util.getSelectedMusicFolderId(context, instance); String folderId = Util.getSelectedMusicFolderId(context, instance);
if (folderId != null) { if (folderId != null) {
builder.add("musicFolderId", folderId); parameters.put("musicFolderId", folderId);
} }
} }
String url = getRestUrl(context, "getSongsByGenre", null); String url = getRestUrl(context, "getSongsByGenre", parameters);
RequestBody formBody = builder.build();
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {
@ -688,15 +659,14 @@ public class RESTMusicService implements MusicService {
@Override @Override
public User getUser(boolean refresh, String username, Context context, ProgressListener progressListener) throws Exception { public User getUser(boolean refresh, String username, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "getUser", null); Map<String, String> parameters = new HashMap<>();
RequestBody formBody = new FormBody.Builder() parameters.put("username", username);
.add("username", username)
.build(); String url = getRestUrl(context, "getUser", parameters);
Request request = new Request.Builder() Request request = new Request.Builder()
.url(url) .url(url)
.post(formBody)
.build(); .build();
try (Response response = client.newCall(request).execute()) { try (Response response = client.newCall(request).execute()) {