use of org.dataportabilityproject.spi.transfer.types.StringPaginationToken in project data-transfer-project by google.
the class GooglePhotosExporter method exportPhotos.
private ExportResult<PhotosContainerResource> exportPhotos(TokensAndUrlAuthData authData, String albumId, Optional<PaginationData> paginationData) {
try {
int startItem = 1;
if (paginationData.isPresent()) {
String token = ((StringPaginationToken) paginationData.get()).getToken();
Preconditions.checkArgument(token.startsWith(PHOTO_TOKEN_PREFIX), "Invalid pagination token " + token);
startItem = Integer.parseInt(token.substring(PHOTO_TOKEN_PREFIX.length()));
}
URL photosUrl = new URL(String.format(URL_PHOTO_FEED_FORMAT, albumId, startItem, MAX_RESULTS));
AlbumFeed photoFeed = getOrCreatePhotosService(authData).getFeed(photosUrl, AlbumFeed.class);
PaginationData nextPageData = null;
if (photoFeed.getEntries().size() == MAX_RESULTS) {
int nextPageStart = startItem + MAX_RESULTS;
nextPageData = new StringPaginationToken(PHOTO_TOKEN_PREFIX + nextPageStart);
}
ContinuationData continuationData = new ContinuationData(nextPageData);
List<PhotoModel> photos = new ArrayList<>(photoFeed.getEntries().size());
for (GphotoEntry photo : photoFeed.getEntries()) {
MediaContent mediaContent = (MediaContent) photo.getContent();
photos.add(new PhotoModel(photo.getTitle().getPlainText(), mediaContent.getUri(), photo.getDescription().getPlainText(), mediaContent.getMimeType().getMediaType(), albumId));
}
PhotosContainerResource containerResource = new PhotosContainerResource(null, photos);
ResultType resultType = ResultType.CONTINUE;
if (nextPageData == null) {
resultType = ResultType.END;
}
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 getTasks.
private ExportResult getTasks(Tasks tasksService, IdOnlyContainerResource resource, PaginationData paginationData) throws IOException {
Tasks.TasksOperations.List query = tasksService.tasks().list(resource.getId()).setMaxResults(PAGE_SIZE);
if (paginationData != null) {
query.setPageToken(((StringPaginationToken) paginationData).getToken());
}
com.google.api.services.tasks.model.Tasks result = query.execute();
List<TaskModel> newTasks = result.getItems().stream().map(t -> new TaskModel(resource.getId(), t.getTitle(), t.getNotes())).collect(Collectors.toList());
PaginationData newPage = null;
ResultType resultType = ResultType.END;
if (result.getNextPageToken() != null) {
newPage = new StringPaginationToken(result.getNextPageToken());
resultType = ResultType.CONTINUE;
}
TaskContainerResource taskContainerResource = new TaskContainerResource(null, newTasks);
return new ExportResult<>(resultType, taskContainerResource, new ContinuationData(newPage));
}
use of org.dataportabilityproject.spi.transfer.types.StringPaginationToken in project data-transfer-project by google.
the class GoogleCalendarExporterTest method exportEventFirstSet.
@Test
public void exportEventFirstSet() throws IOException {
setUpSingleEventResponse();
// Looking at first page, with at least one page after it
ContainerResource containerResource = new IdOnlyContainerResource(CALENDAR_ID);
ExportInformation exportInformation = new ExportInformation(null, containerResource);
eventListResponse.setNextPageToken(NEXT_TOKEN);
// Run test
ExportResult<CalendarContainerResource> result = googleCalendarExporter.export(UUID.randomUUID(), null, exportInformation);
// Check results
// Verify correct methods were called
verify(calendarEvents).list(CALENDAR_ID);
verify(eventListRequest).setMaxAttendees(MAX_ATTENDEES);
verify(eventListRequest).execute();
// Check events
Collection<CalendarEventModel> actualEvents = result.getExportedData().getEvents();
assertThat(actualEvents.stream().map(CalendarEventModel::getCalendarId).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
assertThat(actualEvents.stream().map(CalendarEventModel::getTitle).collect(Collectors.toList())).containsExactly(EVENT_DESCRIPTION);
// Check pagination token
ContinuationData continuationData = (ContinuationData) result.getContinuationData();
StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
assertThat(paginationToken.getToken()).isEqualTo(EVENT_TOKEN_PREFIX + NEXT_TOKEN);
}
use of org.dataportabilityproject.spi.transfer.types.StringPaginationToken in project data-transfer-project by google.
the class GoogleCalendarExporterTest method exportCalendarFirstSet.
@Test
public void exportCalendarFirstSet() throws IOException {
setUpSingleCalendarResponse();
// Looking at first page, with at least one page after it
calendarListResponse.setNextPageToken(NEXT_TOKEN);
// Run test
ExportResult<CalendarContainerResource> result = googleCalendarExporter.export(JOB_ID, null);
// Check results
// Verify correct methods were called
verify(calendarClient).calendarList();
verify(calendarCalendarList).list();
verify(calendarListRequest).execute();
// Check pagination token
ContinuationData continuationData = (ContinuationData) result.getContinuationData();
StringPaginationToken paginationToken = (StringPaginationToken) continuationData.getPaginationData();
assertThat(paginationToken.getToken()).isEqualTo(CALENDAR_TOKEN_PREFIX + NEXT_TOKEN);
// Check calendars
Collection<CalendarModel> actualCalendars = result.getExportedData().getCalendars();
assertThat(actualCalendars.stream().map(CalendarModel::getId).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
// Check events (should be empty, even though there is an event in the calendar
Collection<CalendarEventModel> actualEvents = result.getExportedData().getEvents();
assertThat(actualEvents).isEmpty();
// Should be one container in the resource list
List<ContainerResource> actualResources = continuationData.getContainerResources();
assertThat(actualResources.stream().map(a -> ((IdOnlyContainerResource) a).getId()).collect(Collectors.toList())).containsExactly(CALENDAR_ID);
}
use of org.dataportabilityproject.spi.transfer.types.StringPaginationToken in project data-transfer-project by google.
the class GoogleContactsExporterTest method exportFirstPage.
@Test
public void exportFirstPage() throws IOException {
setUpSinglePersonResponse();
// Looking at first page, with at least one page after it
listConnectionsResponse.setNextPageToken(NEXT_PAGE_TOKEN);
ExportResult<ContactsModelWrapper> result = contactsService.export(UUID.randomUUID(), null);
// Check that correct methods were called
verify(connections).list(SELF_RESOURCE);
InOrder inOrder = Mockito.inOrder(getBatchGet);
inOrder.verify(getBatchGet).setResourceNames(Collections.singletonList(RESOURCE_NAME));
inOrder.verify(getBatchGet).setPersonFields(PERSON_FIELDS);
inOrder.verify(getBatchGet).execute();
// Check continuation data
ContinuationData continuationData = (ContinuationData) result.getContinuationData();
assertThat(continuationData.getContainerResources()).isEmpty();
StringPaginationToken paginationToken = (StringPaginationToken) ((ContinuationData) result.getContinuationData()).getPaginationData();
assertThat(paginationToken.getToken()).isEqualTo(NEXT_PAGE_TOKEN);
// Check that the right number of VCards was returned
JCardReader reader = new JCardReader(result.getExportedData().getVCards());
List<VCard> vCardList = reader.readAll();
assertThat(vCardList.size()).isEqualTo(connectionsList.size());
}
Aggregations