diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2013-09-02 15:13:00 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2013-09-02 15:13:00 +0200 |
commit | 1f594ad311b8f72fb78f14d137dfb53f59b03f85 (patch) | |
tree | 86390d3f87c2c8da8614e122f7d584c4775c264e /src/de/danoeh/antennapod/gpoddernet/GpodnetService.java | |
parent | 730ba3cc26d65a2f4c6c8f2489ea98beab10364b (diff) | |
download | AntennaPod-1f594ad311b8f72fb78f14d137dfb53f59b03f85.zip |
Upload changes instead of whole subscription list
Diffstat (limited to 'src/de/danoeh/antennapod/gpoddernet/GpodnetService.java')
-rw-r--r-- | src/de/danoeh/antennapod/gpoddernet/GpodnetService.java | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/src/de/danoeh/antennapod/gpoddernet/GpodnetService.java b/src/de/danoeh/antennapod/gpoddernet/GpodnetService.java index 11d7a8517..4c922d12b 100644 --- a/src/de/danoeh/antennapod/gpoddernet/GpodnetService.java +++ b/src/de/danoeh/antennapod/gpoddernet/GpodnetService.java @@ -1,10 +1,7 @@ package de.danoeh.antennapod.gpoddernet; import de.danoeh.antennapod.AppConfig; -import de.danoeh.antennapod.gpoddernet.model.GpodnetDevice; -import de.danoeh.antennapod.gpoddernet.model.GpodnetPodcast; -import de.danoeh.antennapod.gpoddernet.model.GpodnetSubscriptionChange; -import de.danoeh.antennapod.gpoddernet.model.GpodnetTag; +import de.danoeh.antennapod.gpoddernet.model.*; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; @@ -30,6 +27,7 @@ import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; +import java.util.Collection; import java.util.LinkedList; import java.util.List; @@ -388,6 +386,55 @@ public class GpodnetService { } /** + * Updates the subscription list of a specific device. + * <p/> + * This method requires authentication. + * + * @param username The username. Must be the same user as the one which is + * currently logged in. + * @param deviceId The ID of the device whose subscriptions should be updated. + * @param added Collection of feed URLs of added feeds. This Collection MUST NOT contain any duplicates + * @param removed Collection of feed URLs of removed feeds. This Collection MUST NOT contain any duplicates + * @return a GpodnetUploadChangesResponse. See {@link de.danoeh.antennapod.gpoddernet.model.GpodnetUploadChangesResponse} + * for details. + * @throws java.lang.IllegalArgumentException if username, deviceId, added or removed is null. + * @throws de.danoeh.antennapod.gpoddernet.GpodnetServiceException if added or removed contain duplicates or if there + * is an authentication error. + */ + public GpodnetUploadChangesResponse uploadChanges(String username, String deviceId, Collection<String> added, + Collection<String> removed) throws GpodnetServiceException { + if (username == null || deviceId == null || added == null || removed == null) { + throw new IllegalArgumentException( + "Username, device ID, added and removed must not be null"); + } + try { + URI uri = new URI(BASE_SCHEME, BASE_HOST, String.format( + "/api/2/subscriptions/%s/%s.json", username, deviceId), null); + + final JSONObject requestObject = new JSONObject(); + requestObject.put("add", new JSONArray(added)); + requestObject.put("remove", new JSONArray(removed)); + + HttpPost request = new HttpPost(uri); + StringEntity entity = new StringEntity(requestObject.toString(), "UTF-8"); + request.setEntity(entity); + + final String response = executeRequest(request); + return GpodnetUploadChangesResponse.fromJSONObject(response); + } catch (URISyntaxException e) { + e.printStackTrace(); + throw new IllegalStateException(e); + } catch (JSONException e) { + e.printStackTrace(); + throw new GpodnetServiceException(e); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + throw new IllegalStateException(e); + } + + } + + /** * Returns all subscription changes of a specific device. * <p/> * This method requires authentication. |