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.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@ -102,19 +103,21 @@ public class RESTMusicService implements MusicService {
@Override
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) {
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()
.url(url)
.post(formBody)
.build();
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 {
String url = getRestUrl(context, "getMusicDirectory", null);
Map<String, String> parameters = new HashMap<>();
RequestBody formBody = new FormBody.Builder()
.add("id", id)
.build();
parameters.put("id", id);
String url = getRestUrl(context, "getMusicDirectory", parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -178,15 +180,15 @@ public class RESTMusicService implements MusicService {
@Override
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()
.add("id", id)
.build();
Map<String, String> parameters = new HashMap<>();
parameters.put("id", id);
String url = getRestUrl(context, "getArtist", parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -196,15 +198,14 @@ public class RESTMusicService implements MusicService {
@Override
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()
.add("id", id)
.build();
parameters.put("id", id);
String url = getRestUrl(context, "getAlbum", parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -214,20 +215,17 @@ public class RESTMusicService implements MusicService {
@Override
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());
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();
String url = getRestUrl(context, "search3", parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -237,15 +235,14 @@ public class RESTMusicService implements MusicService {
@Override
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()
.add("id", id)
.build();
parameters.put("id", id);
String url = getRestUrl(context, "getPlaylist", parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -268,27 +265,24 @@ public class RESTMusicService implements MusicService {
@Override
public void createPlaylist(String id, String name, List<MusicDirectory.Entry> entries, Context context, ProgressListener progressListener) throws Exception {
String url = getRestUrl(context, "createPlaylist", null);
Builder builder = new FormBody.Builder();
Map<String, String> parameters = new HashMap<>();
if (id != null) {
builder.add("playlistId", id);
parameters.put("playlistId", id);
}
if (name != null) {
builder.add("name", name);
parameters.put("name", name);
}
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()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -298,15 +292,14 @@ public class RESTMusicService implements MusicService {
@Override
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()
.add("id", id)
.build();
parameters.put("id", id);
String url = getRestUrl(context, "deletePlaylist", parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -316,19 +309,17 @@ public class RESTMusicService implements MusicService {
@Override
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();
builder.add("playlistId", id);
parameters.put("playlistId", id);
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()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -338,20 +329,18 @@ public class RESTMusicService implements MusicService {
@Override
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();
builder.add("playlistId", id);
parameters.put("playlistId", id);
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()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -361,25 +350,23 @@ public class RESTMusicService implements MusicService {
@Override
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();
builder.add("playlistId", id);
builder.add("name", name);
parameters.put("playlistId", id);
parameters.put("name", name);
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++) {
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()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -389,19 +376,17 @@ public class RESTMusicService implements MusicService {
@Override
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();
builder.add("playlistId", id);
builder.add("name", name);
builder.add("comment", comment);
builder.add("public", Boolean.toString(pub));
parameters.put("playlistId", id);
parameters.put("name", name);
parameters.put("comment", comment);
parameters.put("public", Boolean.toString(pub));
RequestBody formBody = builder.build();
String url = getRestUrl(context, "updatePlaylist", parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -411,27 +396,25 @@ public class RESTMusicService implements MusicService {
@Override
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();
builder.add("type", type);
builder.add("size", Integer.toString(size));
builder.add("offset", Integer.toString(offset));
parameters.put("type", type);
parameters.put("size", Integer.toString(size));
parameters.put("offset", Integer.toString(offset));
// Add folder if it was set and is non null
int instance = getInstance(context);
if (Util.getAlbumListsPerFolder(context, instance)) {
String folderId = Util.getSelectedMusicFolderId(context, instance);
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()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -441,37 +424,35 @@ public class RESTMusicService implements MusicService {
@Override
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();
builder.add("size", Integer.toString(size));
builder.add("offset", Integer.toString(offset));
parameters.put("size", Integer.toString(size));
parameters.put("offset", Integer.toString(offset));
int instance = getInstance(context);
if ("genres".equals(type)) {
builder.add("type", "byGenre");
builder.add("genre", extra);
parameters.put("type", "byGenre");
parameters.put("genre", extra);
} else if ("years".equals(type)) {
int decade = Integer.parseInt(extra);
builder.add("type", "byYear");
builder.add("fromYear", Integer.toString(decade + 9));
builder.add("toYear", Integer.toString(decade));
parameters.put("type", "byYear");
parameters.put("fromYear", Integer.toString(decade + 9));
parameters.put("toYear", Integer.toString(decade));
}
// Add folder if it was set and is non null
if (Util.getAlbumListsPerFolder(context, instance)) {
String folderId = Util.getSelectedMusicFolderId(context, instance);
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()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -481,9 +462,10 @@ public class RESTMusicService implements MusicService {
@Override
public MusicDirectory getSongList(String type, int size, int offset, Context context, ProgressListener progressListener) throws Exception {
Builder builder = new FormBody.Builder();
builder.add("size", Integer.toString(size));
builder.add("offset", Integer.toString(offset));
Map<String, String> parameters = new HashMap<>();
parameters.put("size", Integer.toString(size));
parameters.put("offset", Integer.toString(offset));
String method;
switch (type) {
@ -503,13 +485,10 @@ public class RESTMusicService implements MusicService {
method = "getNewaddedSongs";
}
String url = getRestUrl(context, method, null);
RequestBody formBody = builder.build();
String url = getRestUrl(context, method, parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -519,11 +498,12 @@ public class RESTMusicService implements MusicService {
@Override
public MusicDirectory getRandomSongs(int size, String musicFolderId, String genre, String startYear, String endYear, Context context, ProgressListener progressListener) throws Exception {
Builder builder = new FormBody.Builder();
builder.add("size", Integer.toString(size));
Map<String, String> parameters = new HashMap<>();
parameters.put("size", Integer.toString(size));
if (genre != null && !"".equals(genre)) {
builder.add("genre", genre);
parameters.put("genre", genre);
}
if (startYear != null && !"".equals(startYear)) {
// 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)) {
builder.add("toYear", endYear);
parameters.put("toYear", endYear);
}
String url = getRestUrl(context, "getRandomSongs", null);
RequestBody formBody = builder.build();
String url = getRestUrl(context, "getRandomSongs", parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -574,16 +551,14 @@ public class RESTMusicService implements MusicService {
return bitmap;
}
String url = getRestUrl(context, "getCoverArt", null);
Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder();
builder.add("id", entry.getCoverArt());
parameters.put("id", entry.getCoverArt());
RequestBody formBody = builder.build();
String url = getRestUrl(context, "getCoverArt", parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -621,13 +596,12 @@ public class RESTMusicService implements MusicService {
.readTimeout(30, TimeUnit.SECONDS)
.build();
String url = getRestUrl(context, "stream", null);
Map<String, String> parameters = new HashMap<>();
Builder builder = new FormBody.Builder();
builder.add("id", song.getId());
builder.add("maxBitRate", Integer.toString(maxBitrate));
parameters.put("id", song.getId());
parameters.put("maxBitRate", Integer.toString(maxBitrate));
RequestBody formBody = builder.build();
String url = getRestUrl(context, "stream", parameters);
Request.Builder requestBuilder = new Request.Builder();
if (offset > 0) {
@ -635,7 +609,6 @@ public class RESTMusicService implements MusicService {
}
requestBuilder.url(url);
requestBuilder.post(formBody);
Request request = requestBuilder.build();
@ -658,27 +631,25 @@ public class RESTMusicService implements MusicService {
@Override
public MusicDirectory getSongsByGenre(String genre, int count, int offset, Context context, ProgressListener progressListener) throws Exception {
Builder builder = new FormBody.Builder();
builder.add("genre", genre);
builder.add("count", Integer.toString(count));
builder.add("offset", Integer.toString(offset));
Map<String, String> parameters = new HashMap<>();
parameters.put("genre", genre);
parameters.put("count", Integer.toString(count));
parameters.put("offset", Integer.toString(offset));
// Add folder if it was set and is non null
int instance = getInstance(context);
if (Util.getAlbumListsPerFolder(context, instance)) {
String folderId = Util.getSelectedMusicFolderId(context, instance);
if (folderId != null) {
builder.add("musicFolderId", folderId);
parameters.put("musicFolderId", folderId);
}
}
String url = getRestUrl(context, "getSongsByGenre", null);
RequestBody formBody = builder.build();
String url = getRestUrl(context, "getSongsByGenre", parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {
@ -688,15 +659,14 @@ public class RESTMusicService implements MusicService {
@Override
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()
.add("username", username)
.build();
parameters.put("username", username);
String url = getRestUrl(context, "getUser", parameters);
Request request = new Request.Builder()
.url(url)
.post(formBody)
.build();
try (Response response = client.newCall(request).execute()) {