use of org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData in project data-transfer-project by google.
the class GoogleContactsExporter method exportContacts.
private ExportResult<ContactsModelWrapper> exportContacts(TokensAndUrlAuthData authData, Optional<PaginationData> pageData) {
try {
// Set up connection
Connections.List connectionsListRequest = getOrCreatePeopleService(authData).people().connections().list(SELF_RESOURCE);
// Get next page, if we have a page token
if (pageData.isPresent()) {
StringPaginationToken paginationToken = (StringPaginationToken) pageData.get();
connectionsListRequest.setPageToken(paginationToken.getToken());
}
// Get list of connections (nb: not a list containing full info of each Person)
ListConnectionsResponse response = connectionsListRequest.setPersonFields(PERSON_FIELDS).execute();
List<Person> peopleList = response.getConnections();
// Get list of resource names, then get list of Persons
List<String> resourceNames = peopleList.stream().map(Person::getResourceName).collect(Collectors.toList());
GetPeopleResponse batchResponse = getOrCreatePeopleService(authData).people().getBatchGet().setResourceNames(resourceNames).setPersonFields(PERSON_FIELDS).execute();
List<PersonResponse> personResponseList = batchResponse.getResponses();
// Convert Persons to VCards
List<VCard> vCards = personResponseList.stream().map(a -> convert(a.getPerson())).collect(Collectors.toList());
// Determine if there's a next page
StringPaginationToken nextPageData = null;
if (response.getNextPageToken() != null) {
nextPageData = new StringPaginationToken(response.getNextPageToken());
}
ContinuationData continuationData = new ContinuationData(nextPageData);
ContactsModelWrapper wrapper = new ContactsModelWrapper(makeVCardString(vCards));
// Get result type
ResultType resultType = ResultType.CONTINUE;
if (nextPageData == null) {
resultType = ResultType.END;
}
return new ExportResult<ContactsModelWrapper>(resultType, wrapper, continuationData);
} catch (IOException e) {
return new ExportResult<ContactsModelWrapper>(e);
}
}
use of org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData in project data-transfer-project by google.
the class FacebookPhotosExporterTest method testExportPhoto.
@Test
public void testExportPhoto() throws CopyExceptionWithFailureReason {
ExportResult<PhotosContainerResource> result = facebookPhotosExporter.export(uuid, new TokensAndUrlAuthData("accessToken", null, null), Optional.of(new ExportInformation(null, new IdOnlyContainerResource(ALBUM_ID))));
assertEquals(ExportResult.ResultType.END, result.getType());
PhotosContainerResource exportedData = result.getExportedData();
assertEquals(1, exportedData.getPhotos().size());
assertEquals(new PhotoModel(PHOTO_ID + ".jpg", PHOTO_ID, PHOTO_NAME, "image/jpg", PHOTO_ID, ALBUM_ID, false, PHOTO_TIME), exportedData.getPhotos().toArray()[0]);
}
use of org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData in project data-transfer-project by google.
the class FacebookVideosExporterTest method testExportVideo.
@Test
public void testExportVideo() throws CopyExceptionWithFailureReason {
ExportResult<VideosContainerResource> result = facebookVideosExporter.export(uuid, new TokensAndUrlAuthData("accessToken", null, null), Optional.of(new ExportInformation(null, null)));
assertEquals(ExportResult.ResultType.END, result.getType());
VideosContainerResource exportedData = result.getExportedData();
assertEquals(1, exportedData.getVideos().size());
assertEquals(new VideoModel(VIDEO_ID + ".mp4", VIDEO_SOURCE, VIDEO_NAME, "video/mp4", VIDEO_ID, null, false), exportedData.getVideos().toArray()[0]);
}
use of org.datatransferproject.types.transfer.auth.TokensAndUrlAuthData in project data-transfer-project by google.
the class DaybookPostsImporter method insertActivity.
private String insertActivity(IdempotentImportExecutor executor, SocialActivityModel activity, TokensAndUrlAuthData authData) throws IOException {
Map<String, String> imageMap = new HashMap<>();
Map<String, String> linkMap = new HashMap<>();
String content = activity.getContent() == null ? "" : activity.getContent();
String title = activity.getTitle() == null ? "" : activity.getTitle();
String location = activity.getLocation() == null || activity.getLocation().getName() == null ? "" : activity.getLocation().getName();
String published = activity.getPublished().toString() == null ? "" : activity.getPublished().toString();
Request.Builder requestBuilder = new Request.Builder().url(baseUrl);
requestBuilder.header("token", authData.getAccessToken());
FormBody.Builder builder = new FormBody.Builder().add("type", "POSTS");
builder.add("exporter", JobMetadata.getExportService());
builder.add("content", content);
builder.add("title", title);
builder.add("location", location);
builder.add("published", published);
Collection<SocialActivityAttachment> linkAttachments = activity.getAttachments().stream().filter(attachment -> attachment.getType() == SocialActivityAttachmentType.LINK).collect(Collectors.toList());
Collection<SocialActivityAttachment> imageAttachments = activity.getAttachments().stream().filter(attachment -> attachment.getType() == SocialActivityAttachmentType.IMAGE).collect(Collectors.toList());
// don't know how they were laid out in the originating service.
if (!linkAttachments.isEmpty()) {
for (SocialActivityAttachment attachment : linkAttachments) {
linkMap.put(attachment.getName(), attachment.getUrl());
}
try {
String json = objectMapper.writeValueAsString(linkMap);
builder.add("link", json);
} catch (JsonProcessingException e) {
monitor.info(() -> String.format("Error processing JSON: %s", e.getMessage()));
}
}
if (!imageAttachments.isEmpty()) {
for (SocialActivityAttachment image : imageAttachments) {
imageMap.put(image.getName() != null ? image.getName() : image.getUrl(), image.getUrl());
}
try {
String json = objectMapper.writeValueAsString(imageMap);
builder.add("image", json);
} catch (JsonProcessingException e) {
monitor.info(() -> String.format("Error processing JSON: %s", e.getMessage()));
}
}
FormBody formBody = builder.build();
requestBuilder.post(formBody);
try (Response response = client.newCall(requestBuilder.build()).execute()) {
int code = response.code();
// Though sometimes it returns error code for success requests
if (code < 200 || code > 299) {
throw new IOException(String.format("Error occurred in request for adding entry, message: %s", response.message()));
}
return response.message();
}
}
Aggregations