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 | |
parent | 730ba3cc26d65a2f4c6c8f2489ea98beab10364b (diff) | |
download | AntennaPod-1f594ad311b8f72fb78f14d137dfb53f59b03f85.zip |
Upload changes instead of whole subscription list
Diffstat (limited to 'src/de/danoeh/antennapod/gpoddernet')
-rw-r--r-- | src/de/danoeh/antennapod/gpoddernet/GpodnetService.java | 55 | ||||
-rw-r--r-- | src/de/danoeh/antennapod/gpoddernet/model/GpodnetUploadChangesResponse.java | 56 |
2 files changed, 107 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. diff --git a/src/de/danoeh/antennapod/gpoddernet/model/GpodnetUploadChangesResponse.java b/src/de/danoeh/antennapod/gpoddernet/model/GpodnetUploadChangesResponse.java new file mode 100644 index 000000000..fee8c7d28 --- /dev/null +++ b/src/de/danoeh/antennapod/gpoddernet/model/GpodnetUploadChangesResponse.java @@ -0,0 +1,56 @@ +package de.danoeh.antennapod.gpoddernet.model; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashMap; +import java.util.Map; + +/** + * Object returned by {@link de.danoeh.antennapod.gpoddernet.GpodnetService} in uploadChanges method. + */ +public class GpodnetUploadChangesResponse { + + /** + * timestamp/ID that can be used for requesting changes since this upload. + */ + public final long timestamp; + + /** + * URLs that should be updated. The key of the map is the original URL, the value of the map + * is the sanitized URL. + */ + public final Map<String, String> updatedUrls; + + public GpodnetUploadChangesResponse(long timestamp, Map<String, String> updatedUrls) { + this.timestamp = timestamp; + this.updatedUrls = updatedUrls; + } + + /** + * Creates a new GpodnetUploadChangesResponse-object from a JSON object that was + * returned by an uploadChanges call. + * + * @throws org.json.JSONException If the method could not parse the JSONObject. + */ + public static GpodnetUploadChangesResponse fromJSONObject(String objectString) throws JSONException { + final JSONObject object = new JSONObject(objectString); + final long timestamp = object.getLong("timestamp"); + Map<String, String> updatedUrls = new HashMap<String, String>(); + JSONArray urls = object.getJSONArray("update_urls"); + for (int i = 0; i < urls.length(); i++) { + JSONArray urlPair = urls.getJSONArray(i); + updatedUrls.put(urlPair.getString(0), urlPair.getString(1)); + } + return new GpodnetUploadChangesResponse(timestamp, updatedUrls); + } + + @Override + public String toString() { + return "GpodnetUploadChangesResponse{" + + "timestamp=" + timestamp + + ", updatedUrls=" + updatedUrls + + '}'; + } +} |