@Before public void setUp() { FacebookPhotosInterface photosInterface = mock(FacebookPhotosInterface.class); // Set up example album Album album = new Album(); album.setId(ALBUM_ID); album.setName(ALBUM_NAME); album.setDescription(ALBUM_DESCRIPTION); ArrayList<Album> albums = new ArrayList<>(); albums.add(album); @SuppressWarnings("unchecked") Connection<Album> albumConnection = mock(Connection.class); when(photosInterface.getAlbums(Mockito.any())).thenReturn(albumConnection); when(albumConnection.getData()).thenReturn(albums); // Set up example photo Photo photo = new Photo(); photo.setId(PHOTO_ID); Photo.Image image = new Photo.Image(); image.setSource(PHOTO_SOURCE); photo.addImage(image); photo.setName(PHOTO_NAME); ArrayList<Photo> photos = new ArrayList<>(); photos.add(photo); @SuppressWarnings("unchecked") Connection<Photo> photoConnection = mock(Connection.class); when(photosInterface.getPhotos(ALBUM_ID, Optional.empty())).thenReturn(photoConnection); when(photoConnection.getData()).thenReturn(photos); facebookPhotosExporter = new FacebookPhotosExporter(new AppCredentials("key", "secret"), photosInterface); }
private ExportResult<PhotosContainerResource> exportAlbums( TokensAndUrlAuthData authData, Optional<StringPaginationToken> paginationData) { Optional<String> paginationToken = stripTokenPrefix(paginationData, ALBUM_TOKEN_PREFIX); ArrayList<PhotoAlbum> exportAlbums = new ArrayList<>(); // Get albums Connection<Album> connection = getOrCreatePhotosInterface(authData).getAlbums(paginationToken); PaginationData nextPageData = null; String token = connection.getAfterCursor(); if (!Strings.isNullOrEmpty(token)) { nextPageData = new StringPaginationToken(ALBUM_TOKEN_PREFIX + token); } ContinuationData continuationData = new ContinuationData(nextPageData); List<Album> albums = connection.getData(); for (Album album : albums) { exportAlbums.add(new PhotoAlbum(album.getId(), album.getName(), album.getDescription())); continuationData.addContainerResource(new IdOnlyContainerResource(album.getId())); } return new ExportResult<>( ExportResult.ResultType.CONTINUE, new PhotosContainerResource(exportAlbums, null), continuationData); }
/** * @see com.restfb.FacebookClient#fetchConnection(java.lang.String, java.lang.Class, com.restfb.Parameter[]) */ @Override public <T> Connection<T> fetchConnection(String connection, Class<T> connectionType, Parameter... parameters) { verifyParameterPresence("connection", connection); verifyParameterPresence("connectionType", connectionType); Connection<T> conn = new Connection<>(this, makeRequest(connection, parameters), connectionType); if (conn.getNextPageUrl() == null && conn.getAfterCursor() != null) { String fullUrl = createEndpointForApiCall(connection, false); String paramString = toParameterString(parameters); fullUrl = UrlUtils.replaceOrAddQueryParameter(fullUrl + "?" + paramString, "after", conn.getAfterCursor()); conn.setNextPageUrl(fullUrl); } if (conn.getPreviousPageUrl() == null && conn.getBeforeCursor() != null) { String fullUrl = createEndpointForApiCall(connection, false); String paramString = toParameterString(parameters); fullUrl = UrlUtils.replaceOrAddQueryParameter(fullUrl + "?" + paramString, "before", conn.getBeforeCursor()); conn.setPreviousPageUrl(fullUrl); } return conn; }
/** * Does this connection have a next page of data? * * @return {@code true} if there is a next page of data for this connection, {@code false} otherwise. */ public boolean hasNext() { return !isBlank(getNextPageUrl()) && !getData().isEmpty() && !isSameCursor(); }
/** * @see java.util.Iterator#next() */ @Override public List<T> next() { // Special case: initial page will always have data, return it // immediately. if (initialPage) { initialPage = false; return connection.getData(); } if (!connection.hasNext()) { throw new NoSuchElementException("There are no more pages in the connection."); } connection = connection.fetchNextPage(); return connection.getData(); }
/** * checks the cursors (if present) for equality * @return {@code true} if both cursor value are equal, {@code false} otherwise */ private boolean isSameCursor() { return getBeforeCursor() != null && getAfterCursor() != null && getBeforeCursor().equals(getAfterCursor()); } }
/** * Does this connection have a previous page of data? * * @return {@code true} if there is a previous page of data for this connection, {@code false} otherwise. */ public boolean hasPrevious() { return !isBlank(getPreviousPageUrl()) && !isSameCursor(); }
/** * Fetches the next page of the connection. Designed to be used by {@link Itr}. * * @return The next page of the connection. * @since 1.6.7 */ protected Connection<T> fetchNextPage() { return facebookClient.fetchConnectionPage(getNextPageUrl(), connectionType); }
/** * Does this connection have a previous page of data? * * @return {@code true} if there is a previous page of data for this * connection, {@code false} otherwise. */ public boolean hasPrevious() { return !isBlank(getPreviousPageUrl()); }
@SuppressWarnings("unchecked") protected <T> Connection<T> mapToConnection(String connectionJson, Class<T> connectionType) { List<T> data = new ArrayList<T>(); String previous = null; String next = null; try { JsonObject jsonObject = new JsonObject(connectionJson); // Pull out data JsonArray jsonData = jsonObject.getJsonArray("data"); for (int i = 0; i < jsonData.length(); i++) data.add(connectionType.equals(JsonObject.class) ? (T) jsonData.get(i) : jsonMapper.toJavaObject(jsonData .get(i).toString(), connectionType)); // Pull out paging info, if present if (jsonObject.has("paging")) { JsonObject jsonPaging = jsonObject.getJsonObject("paging"); previous = jsonPaging.has("previous") ? jsonPaging.getString("previous") : null; next = jsonPaging.has("next") ? jsonPaging.getString("next") : null; } } catch (JsonException e) { throw new FacebookJsonMappingException("Unable to map connection JSON to Java objects", e); } return new Connection<T>(data, previous, next); }
Connection<T> conn = new Connection<>(this, connectionJson, connectionType); if (conn.getNextPageUrl() == null && conn.getAfterCursor() != null) { String fullUrl = UrlUtils.removeQueryParameter(connectionPageUrl, "before"); fullUrl = UrlUtils.replaceOrAddQueryParameter(fullUrl, "after", conn.getAfterCursor()); conn.setNextPageUrl(fullUrl); if (conn.getPreviousPageUrl() == null && conn.getBeforeCursor() != null) { String fullUrl = UrlUtils.removeQueryParameter(connectionPageUrl, "after"); fullUrl = UrlUtils.replaceOrAddQueryParameter(fullUrl, "before", conn.getBeforeCursor()); conn.setPreviousPageUrl(fullUrl);
/** * Does this connection have a next page of data? * * @return {@code true} if there is a next page of data for this connection, * {@code false} otherwise. */ public boolean hasNext() { return !isBlank(getNextPageUrl()); } }
@Override public List<PageMessageTag> getMessageTags() throws FacebookException { return facebookClient.fetchConnection(PAGE_MESSAGE_TAGS_PATH, PageMessageTag.class).getData(); } }
Connection<Photo> photoConnection = getOrCreatePhotosInterface(authData).getPhotos(albumId, paginationToken); List<Photo> photos = photoConnection.getData(); for (Photo photo : photos) { Preconditions.checkNotNull(photo.getImages().get(0).getSource()); String token = photoConnection.getAfterCursor(); if (Strings.isNullOrEmpty(token)) { return new ExportResult<>(
Parameter.with("q", event), Parameter.with("type", "event"),Parameter.with("limit", 100)); System.out.println("Searching FB events for " + event); events= publicSearch.getData(); break;
Parameter.with("q", merchant), Parameter.with("type", "page"),Parameter.with("limit", 100)); System.out.println("Searching FB Pages for " + merchant); groups= publicSearch.getData(); break;