Search in sources :

Example 1 with SyncServiceException

use of de.danoeh.antennapod.net.sync.model.SyncServiceException in project AntennaPod by AntennaPod.

the class GpodnetService method getEpisodeActionChanges.

/**
 * Returns all subscription changes of a specific device.
 * <p/>
 * This method requires authentication.
 *
 * @param timestamp A timestamp that can be used to receive all changes since a
 *                  specific point in time.
 * @throws SyncServiceException If there is an authentication error.
 */
@Override
public EpisodeActionChanges getEpisodeActionChanges(long timestamp) throws SyncServiceException {
    requireLoggedIn();
    String params = String.format(Locale.US, "since=%d", timestamp);
    String path = String.format("/api/2/episodes/%s.json", username);
    try {
        URL url = new URI(baseScheme, null, baseHost, basePort, path, params, null).toURL();
        Request.Builder request = new Request.Builder().url(url);
        String response = executeRequest(request);
        JSONObject json = new JSONObject(response);
        return ResponseMapper.readEpisodeActionsFromJsonObject(json);
    } catch (URISyntaxException e) {
        e.printStackTrace();
        throw new IllegalStateException(e);
    } catch (JSONException | MalformedURLException e) {
        e.printStackTrace();
        throw new SyncServiceException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) JSONObject(org.json.JSONObject) Request(okhttp3.Request) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) SyncServiceException(de.danoeh.antennapod.net.sync.model.SyncServiceException) URI(java.net.URI) URL(java.net.URL)

Example 2 with SyncServiceException

use of de.danoeh.antennapod.net.sync.model.SyncServiceException in project AntennaPod by AntennaPod.

the class SyncService method doWork.

@Override
@NonNull
public Result doWork() {
    ISyncService activeSyncProvider = getActiveSyncProvider();
    if (activeSyncProvider == null) {
        return Result.success();
    }
    SynchronizationSettings.updateLastSynchronizationAttempt();
    setCurrentlyActive(true);
    try {
        activeSyncProvider.login();
        syncSubscriptions(activeSyncProvider);
        waitForDownloadServiceCompleted();
        syncEpisodeActions(activeSyncProvider);
        activeSyncProvider.logout();
        clearErrorNotifications();
        EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_success));
        SynchronizationSettings.setLastSynchronizationAttemptSuccess(true);
        return Result.success();
    } catch (Exception e) {
        EventBus.getDefault().postSticky(new SyncServiceEvent(R.string.sync_status_error));
        SynchronizationSettings.setLastSynchronizationAttemptSuccess(false);
        Log.e(TAG, Log.getStackTraceString(e));
        if (e instanceof SyncServiceException) {
            if (getRunAttemptCount() % 3 == 2) {
                // Do not spam users with notification and retry before notifying
                updateErrorNotification(e);
            }
            return Result.retry();
        } else {
            updateErrorNotification(e);
            return Result.failure();
        }
    } finally {
        setCurrentlyActive(false);
    }
}
Also used : SyncServiceException(de.danoeh.antennapod.net.sync.model.SyncServiceException) SyncServiceEvent(de.danoeh.antennapod.event.SyncServiceEvent) ISyncService(de.danoeh.antennapod.net.sync.model.ISyncService) SyncServiceException(de.danoeh.antennapod.net.sync.model.SyncServiceException) NonNull(androidx.annotation.NonNull)

Example 3 with SyncServiceException

use of de.danoeh.antennapod.net.sync.model.SyncServiceException in project AntennaPod by AntennaPod.

the class GpodnetService method uploadEpisodeActionsPartial.

private UploadChangesResponse uploadEpisodeActionsPartial(List<EpisodeAction> episodeActions, int from, int to) throws SyncServiceException {
    try {
        Log.d(TAG, "Uploading partial actions " + from + " to " + to + " of " + episodeActions.size());
        URL url = new URI(baseScheme, null, baseHost, basePort, String.format("/api/2/episodes/%s.json", username), null, null).toURL();
        final JSONArray list = new JSONArray();
        for (int i = from; i < to; i++) {
            EpisodeAction episodeAction = episodeActions.get(i);
            JSONObject obj = episodeAction.writeToJsonObject();
            if (obj != null) {
                obj.put("device", deviceId);
                list.put(obj);
            }
        }
        RequestBody body = RequestBody.create(JSON, list.toString());
        Request.Builder request = new Request.Builder().post(body).url(url);
        final String response = executeRequest(request);
        return GpodnetEpisodeActionPostResponse.fromJSONObject(response);
    } catch (JSONException | MalformedURLException | URISyntaxException e) {
        e.printStackTrace();
        throw new SyncServiceException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) JSONArray(org.json.JSONArray) Request(okhttp3.Request) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) EpisodeAction(de.danoeh.antennapod.net.sync.model.EpisodeAction) JSONObject(org.json.JSONObject) SyncServiceException(de.danoeh.antennapod.net.sync.model.SyncServiceException) RequestBody(okhttp3.RequestBody)

Example 4 with SyncServiceException

use of de.danoeh.antennapod.net.sync.model.SyncServiceException in project AntennaPod by AntennaPod.

the class NextcloudSyncService method getSubscriptionChanges.

@Override
public SubscriptionChanges getSubscriptionChanges(long lastSync) throws SyncServiceException {
    try {
        HttpUrl.Builder url = makeUrl("/index.php/apps/gpoddersync/subscriptions");
        url.addQueryParameter("since", "" + lastSync);
        String responseString = performRequest(url, "GET", null);
        JSONObject json = new JSONObject(responseString);
        return ResponseMapper.readSubscriptionChangesFromJsonObject(json);
    } catch (JSONException | MalformedURLException e) {
        e.printStackTrace();
        throw new SyncServiceException(e);
    } catch (Exception e) {
        e.printStackTrace();
        throw new SyncServiceException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) SyncServiceException(de.danoeh.antennapod.net.sync.model.SyncServiceException) HttpUrl(okhttp3.HttpUrl) SyncServiceException(de.danoeh.antennapod.net.sync.model.SyncServiceException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JSONException(org.json.JSONException)

Example 5 with SyncServiceException

use of de.danoeh.antennapod.net.sync.model.SyncServiceException in project AntennaPod by AntennaPod.

the class NextcloudSyncService method getEpisodeActionChanges.

@Override
public EpisodeActionChanges getEpisodeActionChanges(long timestamp) throws SyncServiceException {
    try {
        HttpUrl.Builder uri = makeUrl("/index.php/apps/gpoddersync/episode_action");
        uri.addQueryParameter("since", "" + timestamp);
        String responseString = performRequest(uri, "GET", null);
        JSONObject json = new JSONObject(responseString);
        return ResponseMapper.readEpisodeActionsFromJsonObject(json);
    } catch (JSONException | MalformedURLException e) {
        e.printStackTrace();
        throw new SyncServiceException(e);
    } catch (Exception e) {
        e.printStackTrace();
        throw new SyncServiceException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) SyncServiceException(de.danoeh.antennapod.net.sync.model.SyncServiceException) HttpUrl(okhttp3.HttpUrl) SyncServiceException(de.danoeh.antennapod.net.sync.model.SyncServiceException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JSONException(org.json.JSONException)

Aggregations

SyncServiceException (de.danoeh.antennapod.net.sync.model.SyncServiceException)5 MalformedURLException (java.net.MalformedURLException)4 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 IOException (java.io.IOException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 HttpUrl (okhttp3.HttpUrl)2 Request (okhttp3.Request)2 NonNull (androidx.annotation.NonNull)1 SyncServiceEvent (de.danoeh.antennapod.event.SyncServiceEvent)1 EpisodeAction (de.danoeh.antennapod.net.sync.model.EpisodeAction)1 ISyncService (de.danoeh.antennapod.net.sync.model.ISyncService)1 RequestBody (okhttp3.RequestBody)1 JSONArray (org.json.JSONArray)1