use of org.dataportabilityproject.spi.transfer.types.StringPaginationToken in project data-transfer-project by google.
the class SmugMugPhotosExporter method exportPhotos.
private ExportResult<PhotosContainerResource> exportPhotos(IdOnlyContainerResource containerResource, Optional<PaginationData> paginationData) throws IOException {
List<PhotoModel> photoList = new ArrayList<>();
// Make request to SmugMug
String photoInfoUri;
if (paginationData.isPresent()) {
String token = ((StringPaginationToken) paginationData.get()).getToken();
Preconditions.checkState(token.startsWith(PHOTO_TOKEN_PREFIX), "Invalid pagination token " + token);
photoInfoUri = token.substring(PHOTO_TOKEN_PREFIX.length());
} else {
String id = containerResource.getId();
photoInfoUri = String.format(ALBUM_URL_FORMATTER, id);
}
SmugMugResponse<SmugMugAlbumInfoResponse> albumInfoResponse = smugMugInterface.makeAlbumInfoRequest(photoInfoUri);
// Set up continuation data
StringPaginationToken pageToken = null;
if (albumInfoResponse.getResponse().getPageInfo().getNextPage() != null) {
pageToken = new StringPaginationToken(PHOTO_TOKEN_PREFIX + albumInfoResponse.getResponse().getPageInfo().getNextPage());
}
ContinuationData continuationData = new ContinuationData(pageToken);
// Make list of photos
for (SmugMugAlbumImage image : albumInfoResponse.getResponse().getImages()) {
String title = image.getTitle();
if (Strings.isNullOrEmpty(title)) {
title = image.getFileName();
}
// TODO(olsona): this.authConsumer.sign(image.getArchivedUri()) ?
photoList.add(new PhotoModel(title, image.getArchivedUri(), image.getCaption(), image.getFormat(), containerResource.getId()));
}
PhotosContainerResource resource = new PhotosContainerResource(null, photoList);
// Get result type
ResultType resultType = ResultType.CONTINUE;
if (pageToken == null) {
resultType = ResultType.END;
}
return new ExportResult<>(resultType, resource, continuationData);
}
use of org.dataportabilityproject.spi.transfer.types.StringPaginationToken in project data-transfer-project by google.
the class SmugMugPhotosExporter method export.
@Override
public ExportResult<PhotosContainerResource> export(UUID jobId, AuthData authData, ExportInformation exportInformation) {
StringPaginationToken paginationToken = (StringPaginationToken) exportInformation.getPaginationData();
if (paginationToken != null && paginationToken.getToken().startsWith(ALBUM_TOKEN_PREFIX)) {
// Next thing to export is more albums
try {
return exportAlbums(Optional.of(paginationToken));
} catch (IOException e) {
return new ExportResult<>(ResultType.ERROR, e.getMessage());
}
} else {
// Next thing to export is photos
IdOnlyContainerResource idOnlyContainerResource = (IdOnlyContainerResource) exportInformation.getContainerResource();
Optional<PaginationData> pageData = paginationToken != null ? Optional.of(paginationToken) : Optional.empty();
try {
return exportPhotos(idOnlyContainerResource, pageData);
} catch (IOException e) {
return new ExportResult<>(ResultType.ERROR, e.getMessage());
}
}
}
use of org.dataportabilityproject.spi.transfer.types.StringPaginationToken in project data-transfer-project by google.
the class GooglePhotosExporter method exportAlbums.
private ExportResult<PhotosContainerResource> exportAlbums(TokensAndUrlAuthData authData, Optional<PaginationData> paginationData) {
try {
int startItem = 1;
if (paginationData.isPresent()) {
String token = ((StringPaginationToken) paginationData.get()).getToken();
Preconditions.checkArgument(token.startsWith(ALBUM_TOKEN_PREFIX), "Invalid pagination token " + token);
startItem = Integer.parseInt(token.substring(ALBUM_TOKEN_PREFIX.length()));
}
URL albumUrl = new URL(String.format(URL_ALBUM_FEED_FORMAT, startItem, MAX_RESULTS));
UserFeed albumFeed = getOrCreatePhotosService(authData).getFeed(albumUrl, UserFeed.class);
PaginationData nextPageData = null;
if (albumFeed.getAlbumEntries().size() == MAX_RESULTS) {
int nextPageStart = startItem + MAX_RESULTS;
nextPageData = new StringPaginationToken(ALBUM_TOKEN_PREFIX + nextPageStart);
}
ContinuationData continuationData = new ContinuationData(nextPageData);
List<PhotoAlbum> albums = new ArrayList<>(albumFeed.getAlbumEntries().size());
for (GphotoEntry googleAlbum : albumFeed.getAlbumEntries()) {
// Add album info to list so album can be recreated later
albums.add(new PhotoAlbum(googleAlbum.getGphotoId(), googleAlbum.getTitle().getPlainText(), googleAlbum.getDescription().getPlainText()));
// Add album id to continuation data
continuationData.addContainerResource(new IdOnlyContainerResource(googleAlbum.getGphotoId()));
}
ResultType resultType = ResultType.CONTINUE;
if (nextPageData == null || continuationData.getContainerResources().isEmpty()) {
resultType = ResultType.END;
}
PhotosContainerResource containerResource = new PhotosContainerResource(albums, null);
return new ExportResult<>(resultType, containerResource, continuationData);
} catch (ServiceException | IOException e) {
return new ExportResult<>(ResultType.ERROR, e.getMessage());
}
}
use of org.dataportabilityproject.spi.transfer.types.StringPaginationToken in project data-transfer-project by google.
the class GoogleTasksExporter method getTasksList.
private ExportResult getTasksList(Tasks tasksSerivce, PaginationData paginationData) throws IOException {
Tasks.Tasklists.List query = tasksSerivce.tasklists().list().setMaxResults(PAGE_SIZE);
if (paginationData != null) {
query.setPageToken(((StringPaginationToken) paginationData).getToken());
}
TaskLists result = query.execute();
ImmutableList.Builder<TaskListModel> newTaskListsBuilder = ImmutableList.builder();
ImmutableList.Builder<IdOnlyContainerResource> newResourcesBuilder = ImmutableList.builder();
for (TaskList taskList : result.getItems()) {
newTaskListsBuilder.add(new TaskListModel(taskList.getId(), taskList.getTitle()));
newResourcesBuilder.add(new IdOnlyContainerResource(taskList.getId()));
}
PaginationData newPage = null;
ResultType resultType = ResultType.END;
if (result.getNextPageToken() != null) {
newPage = new StringPaginationToken(result.getNextPageToken());
resultType = ResultType.CONTINUE;
}
List<IdOnlyContainerResource> newResources = newResourcesBuilder.build();
if (!newResources.isEmpty()) {
resultType = ResultType.CONTINUE;
}
TaskContainerResource taskContainerResource = new TaskContainerResource(newTaskListsBuilder.build(), null);
ContinuationData continuationData = new ContinuationData(newPage);
newResourcesBuilder.build().forEach(resource -> continuationData.addContainerResource(resource));
return new ExportResult<>(resultType, taskContainerResource, continuationData);
}
use of org.dataportabilityproject.spi.transfer.types.StringPaginationToken in project data-transfer-project by google.
the class GoogleCalendarExporterTest method exportCalendarSubsequentSet.
@Test
public void exportCalendarSubsequentSet() throws IOException {
setUpSingleCalendarResponse();
// Looking at subsequent page, with no page after it
PaginationData paginationData = new StringPaginationToken(CALENDAR_TOKEN_PREFIX + NEXT_TOKEN);
ExportInformation exportInformation = new ExportInformation(paginationData, null);
calendarListResponse.setNextPageToken(null);
// Run test
ExportResult<CalendarContainerResource> result = googleCalendarExporter.export(UUID.randomUUID(), null, exportInformation);
// Check results
// Verify correct calls were made
InOrder inOrder = Mockito.inOrder(calendarListRequest);
inOrder.verify(calendarListRequest).setPageToken(NEXT_TOKEN);
inOrder.verify(calendarListRequest).execute();
// Check pagination token
ContinuationData continuationData = (ContinuationData) result.getContinuationData();
StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
assertThat(paginationToken).isNull();
}
Aggregations