Esempio n. 1
0
  /**
   * Fetches stories for the given FeedSet, choosing the correct API and the right request
   * parameters as needed.
   */
  public StoriesResponse getStories(
      FeedSet fs, int pageNumber, StoryOrder order, ReadFilter filter) {
    Uri uri = null;
    ValueMultimap values = new ValueMultimap();

    // create the URI and populate request params depending on what kind of stories we want
    if (fs.getSingleFeed() != null) {
      uri =
          Uri.parse(APIConstants.URL_FEED_STORIES)
              .buildUpon()
              .appendPath(fs.getSingleFeed())
              .build();
      values.put(APIConstants.PARAMETER_FEEDS, fs.getSingleFeed());
      values.put(APIConstants.PARAMETER_INCLUDE_HIDDEN, APIConstants.VALUE_TRUE);
    } else if (fs.getMultipleFeeds() != null) {
      uri = Uri.parse(APIConstants.URL_RIVER_STORIES);
      for (String feedId : fs.getMultipleFeeds()) values.put(APIConstants.PARAMETER_FEEDS, feedId);
      values.put(APIConstants.PARAMETER_INCLUDE_HIDDEN, APIConstants.VALUE_TRUE);
    } else if (fs.getSingleSocialFeed() != null) {
      String feedId = fs.getSingleSocialFeed().getKey();
      String username = fs.getSingleSocialFeed().getValue();
      uri =
          Uri.parse(APIConstants.URL_SOCIALFEED_STORIES)
              .buildUpon()
              .appendPath(feedId)
              .appendPath(username)
              .build();
      values.put(APIConstants.PARAMETER_USER_ID, feedId);
      values.put(APIConstants.PARAMETER_USERNAME, username);
    } else if (fs.getMultipleSocialFeeds() != null) {
      uri = Uri.parse(APIConstants.URL_SHARED_RIVER_STORIES);
      for (Map.Entry<String, String> entry : fs.getMultipleSocialFeeds().entrySet()) {
        values.put(APIConstants.PARAMETER_FEEDS, entry.getKey());
      }
    } else if (fs.isAllNormal()) {
      uri = Uri.parse(APIConstants.URL_RIVER_STORIES);
      values.put(APIConstants.PARAMETER_INCLUDE_HIDDEN, APIConstants.VALUE_TRUE);
    } else if (fs.isAllSocial()) {
      uri = Uri.parse(APIConstants.URL_SHARED_RIVER_STORIES);
    } else if (fs.isAllRead()) {
      uri = Uri.parse(APIConstants.URL_READ_STORIES);
    } else if (fs.isAllSaved()) {
      uri = Uri.parse(APIConstants.URL_STARRED_STORIES);
    } else if (fs.isGlobalShared()) {
      uri = Uri.parse(APIConstants.URL_SHARED_RIVER_STORIES);
      values.put(APIConstants.PARAMETER_GLOBAL_FEED, Boolean.TRUE.toString());
    } else {
      throw new IllegalStateException("Asked to get stories for FeedSet of unknown type.");
    }

    // request params common to most story sets
    values.put(APIConstants.PARAMETER_PAGE_NUMBER, Integer.toString(pageNumber));
    if (!(fs.isAllRead() || fs.isAllSaved())) {
      values.put(APIConstants.PARAMETER_ORDER, order.getParameterValue());
      values.put(APIConstants.PARAMETER_READ_FILTER, filter.getParameterValue());
    }

    APIResponse response = get(uri.toString(), values);
    return (StoriesResponse) response.getResponse(gson, StoriesResponse.class);
  }