Search in sources :

Example 41 with Request

use of com.tonyodev.fetch2.Request in project AntennaPod by AntennaPod.

the class GpodnetService method uploadEpisodeActions.

/**
     * Updates the episode actions
     * <p/>
     * This method requires authentication.
     *
     * @param episodeActions    Collection of episode actions.
     * @return a GpodnetUploadChangesResponse. See {@link de.danoeh.antennapod.core.gpoddernet.model.GpodnetUploadChangesResponse}
     * for details.
     * @throws java.lang.IllegalArgumentException                           if username, deviceId, added or removed is null.
     * @throws de.danoeh.antennapod.core.gpoddernet.GpodnetServiceException if added or removed contain duplicates or if there
     *                                                                      is an authentication error.
     */
public GpodnetEpisodeActionPostResponse uploadEpisodeActions(@NonNull Collection<GpodnetEpisodeAction> episodeActions) throws GpodnetServiceException {
    String username = GpodnetPreferences.getUsername();
    try {
        URL url = new URI(BASE_SCHEME, BASE_HOST, String.format("/api/2/episodes/%s.json", username), null).toURL();
        final JSONArray list = new JSONArray();
        for (GpodnetEpisodeAction episodeAction : episodeActions) {
            JSONObject obj = episodeAction.writeToJSONObject();
            if (obj != null) {
                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 GpodnetServiceException(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) GpodnetEpisodeAction(de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction) JSONObject(org.json.JSONObject) RequestBody(okhttp3.RequestBody)

Example 42 with Request

use of com.tonyodev.fetch2.Request in project AntennaPod by AntennaPod.

the class GpodnetService method getPodcastToplist.

/**
     * Returns the toplist of podcast.
     *
     * @param count of elements that should be returned. Must be in range 1..100.
     * @throws IllegalArgumentException if count is out of range.
     */
public List<GpodnetPodcast> getPodcastToplist(int count) throws GpodnetServiceException {
    if (count < 1 || count > 100) {
        throw new IllegalArgumentException("Count must be in range 1..100");
    }
    try {
        URL url = new URI(BASE_SCHEME, BASE_HOST, String.format("/toplist/%d.json", count), null).toURL();
        Request.Builder request = new Request.Builder().url(url);
        String response = executeRequest(request);
        JSONArray jsonArray = new JSONArray(response);
        return readPodcastListFromJSONArray(jsonArray);
    } catch (JSONException | MalformedURLException | URISyntaxException e) {
        e.printStackTrace();
        throw new GpodnetServiceException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Request(okhttp3.Request) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL)

Example 43 with Request

use of com.tonyodev.fetch2.Request in project AntennaPod by AntennaPod.

the class GpodnetService method uploadSubscriptions.

/**
     * Uploads the subscriptions 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 subscriptions A list of feed URLs containing all subscriptions of the
     *                      device.
     * @throws IllegalArgumentException              If username, deviceId or subscriptions is null.
     * @throws GpodnetServiceAuthenticationException If there is an authentication error.
     */
public void uploadSubscriptions(@NonNull String username, @NonNull String deviceId, @NonNull List<String> subscriptions) throws GpodnetServiceException {
    try {
        URL url = new URI(BASE_SCHEME, BASE_HOST, String.format("/subscriptions/%s/%s.txt", username, deviceId), null).toURL();
        StringBuilder builder = new StringBuilder();
        for (String s : subscriptions) {
            builder.append(s);
            builder.append("\n");
        }
        RequestBody body = RequestBody.create(TEXT, builder.toString());
        Request.Builder request = new Request.Builder().put(body).url(url);
        executeRequest(request);
    } catch (MalformedURLException | URISyntaxException e) {
        e.printStackTrace();
        throw new GpodnetServiceException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Request(okhttp3.Request) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) RequestBody(okhttp3.RequestBody)

Example 44 with Request

use of com.tonyodev.fetch2.Request in project AntennaPod by AntennaPod.

the class GpodnetService method getTopTags.

/**
     * Returns the [count] most used tags.
     */
public List<GpodnetTag> getTopTags(int count) throws GpodnetServiceException {
    URL url;
    try {
        url = new URI(BASE_SCHEME, BASE_HOST, String.format("/api/2/tags/%d.json", count), null).toURL();
    } catch (MalformedURLException | URISyntaxException e) {
        e.printStackTrace();
        throw new GpodnetServiceException(e);
    }
    Request.Builder request = new Request.Builder().url(url);
    String response = executeRequest(request);
    try {
        JSONArray jsonTagList = new JSONArray(response);
        List<GpodnetTag> tagList = new ArrayList<>(jsonTagList.length());
        for (int i = 0; i < jsonTagList.length(); i++) {
            JSONObject jObj = jsonTagList.getJSONObject(i);
            String title = jObj.getString("title");
            String tag = jObj.getString("tag");
            int usage = jObj.getInt("usage");
            tagList.add(new GpodnetTag(title, tag, usage));
        }
        return tagList;
    } catch (JSONException e) {
        e.printStackTrace();
        throw new GpodnetServiceException(e);
    }
}
Also used : GpodnetTag(de.danoeh.antennapod.core.gpoddernet.model.GpodnetTag) MalformedURLException(java.net.MalformedURLException) Request(okhttp3.Request) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) JSONObject(org.json.JSONObject)

Example 45 with Request

use of com.tonyodev.fetch2.Request in project AntennaPod by AntennaPod.

the class GpodnetService method searchPodcasts.

/**
     * Searches the podcast directory for a given string.
     *
     * @param query          The search query
     * @param scaledLogoSize The size of the logos that are returned by the search query.
     *                       Must be in range 1..256. If the value is out of range, the
     *                       default value defined by the gpodder.net API will be used.
     */
public List<GpodnetPodcast> searchPodcasts(String query, int scaledLogoSize) throws GpodnetServiceException {
    String parameters = (scaledLogoSize > 0 && scaledLogoSize <= 256) ? String.format("q=%s&scale_logo=%d", query, scaledLogoSize) : String.format("q=%s", query);
    try {
        URL url = new URI(BASE_SCHEME, null, BASE_HOST, -1, "/search.json", parameters, null).toURL();
        Request.Builder request = new Request.Builder().url(url);
        String response = executeRequest(request);
        JSONArray jsonArray = new JSONArray(response);
        return readPodcastListFromJSONArray(jsonArray);
    } catch (JSONException | MalformedURLException e) {
        e.printStackTrace();
        throw new GpodnetServiceException(e);
    } catch (URISyntaxException e) {
        e.printStackTrace();
        throw new IllegalStateException(e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Request(okhttp3.Request) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL)

Aggregations

Request (okhttp3.Request)1601 Response (okhttp3.Response)1009 IOException (java.io.IOException)519 Test (org.junit.Test)406 OkHttpClient (okhttp3.OkHttpClient)330 RequestBody (okhttp3.RequestBody)255 Call (okhttp3.Call)239 ResponseBody (okhttp3.ResponseBody)187 HttpUrl (okhttp3.HttpUrl)139 Callback (okhttp3.Callback)109 Map (java.util.Map)85 File (java.io.File)77 InputStream (java.io.InputStream)77 JSONObject (org.json.JSONObject)76 MediaType (okhttp3.MediaType)75 Buffer (okio.Buffer)73 List (java.util.List)71 Headers (okhttp3.Headers)71 FormBody (okhttp3.FormBody)64 HashMap (java.util.HashMap)63