use of org.dataportabilityproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class GoogleTasksImporter method importItem.
@Override
public ImportResult importItem(UUID jobId, TokensAndUrlAuthData authData, TaskContainerResource data) {
Tasks tasksService = getOrCreateTasksService(authData);
TempTasksData tempTasksData = jobStore.findData(TempTasksData.class, jobId);
if (tempTasksData == null) {
tempTasksData = new TempTasksData(jobId.toString());
jobStore.create(jobId, tempTasksData);
}
for (TaskListModel oldTasksList : data.getLists()) {
// TempTasksData shouldn't be null since we added it.
tempTasksData = jobStore.findData(TempTasksData.class, jobId);
TaskList newTaskList = new TaskList().setTitle("Imported copy - " + oldTasksList.getName());
TaskList insertedTaskList;
try {
insertedTaskList = tasksService.tasklists().insert(newTaskList).execute();
} catch (IOException e) {
return new ImportResult(ResultType.ERROR, "Error inserting taskList: " + e.getMessage());
}
logger.info("Storing {} as {}", oldTasksList.getId(), insertedTaskList.getId());
tempTasksData.addTaskListId(oldTasksList.getId(), insertedTaskList.getId());
jobStore.update(jobId, tempTasksData);
}
tempTasksData = jobStore.findData(TempTasksData.class, jobId);
for (TaskModel oldTask : data.getTasks()) {
Task newTask = new Task().setTitle(oldTask.getText()).setNotes(oldTask.getNotes());
String newTaskId = tempTasksData.lookupNewTaskListId(newTask.getId());
try {
tasksService.tasks().insert(newTaskId, newTask).execute();
} catch (IOException e) {
return new ImportResult(ResultType.ERROR, "Error inserting task: " + e.getMessage());
}
}
return new ImportResult(ResultType.OK);
}
use of org.dataportabilityproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class RequestHelper method batchRequest.
/**
* Creates a Graph API batch request with required the authorization header.
*
* @param authData the auth token
* @param requests the batch request data
* @param client the client to construct the request with
* @param objectMapper the mapper to serialize data
*/
@SuppressWarnings("unchecked")
public static BatchResponse batchRequest(TokenAuthData authData, List<Map<String, Object>> requests, String baseUrl, OkHttpClient client, ObjectMapper objectMapper) {
try {
Map<String, Object> batch = new LinkedHashMap<>();
batch.put("requests", requests);
Request.Builder requestBuilder = new Request.Builder().url(baseUrl + BATCH_URL);
requestBuilder.header("Authorization", "Bearer " + authData.getToken());
requestBuilder.post(RequestBody.create(MediaType.parse("application/json"), objectMapper.writeValueAsString(batch)));
try (Response response = client.newCall(requestBuilder.build()).execute()) {
int code = response.code();
if (code >= 200 && code <= 299) {
ResponseBody body = response.body();
if (body == null) {
// FIXME evaluate HTTP response and return whether to retry
return new BatchResponse(new ImportResult(ImportResult.ResultType.ERROR));
}
Map<String, Object> responseData = objectMapper.readValue(body.bytes(), Map.class);
return new BatchResponse(new ImportResult(ImportResult.ResultType.OK), (List<Map<String, Object>>) responseData.get("responses"));
} else {
// FIXME evaluate HTTP response and return whether to retry
return new BatchResponse(new ImportResult(ImportResult.ResultType.ERROR));
}
}
} catch (IOException e) {
// TODO log
e.printStackTrace();
return new BatchResponse(new ImportResult(ImportResult.ResultType.ERROR, "Error batching request: " + e.getMessage()));
}
}
use of org.dataportabilityproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class SmugMugPhotosImporter method importItem.
@Override
public ImportResult importItem(UUID jobId, AuthData authData, PhotosContainerResource data) {
try {
String folder = null;
if (!data.getAlbums().isEmpty()) {
SmugMugResponse<SmugMugUserResponse> userResponse = smugMugInterface.makeUserRequest(smugMugInterface.USER_URL);
folder = userResponse.getResponse().getUser().getUris().get("Folder").getUri();
}
for (PhotoAlbum album : data.getAlbums()) {
importSingleAlbum(jobId, folder, album);
}
for (PhotoModel photo : data.getPhotos()) {
importSinglePhoto(jobId, photo);
}
} catch (IOException e) {
// TODO(olsona): we should retry on individual errors
return new ImportResult(ResultType.ERROR, e.getMessage());
}
return ImportResult.OK;
}
use of org.dataportabilityproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class GoogleContactsImporter method importItem.
@Override
public ImportResult importItem(UUID jobId, TokensAndUrlAuthData authData, ContactsModelWrapper data) {
JCardReader reader = new JCardReader(data.getVCards());
try {
// TODO(olsona): address any other problems that might arise in conversion
List<VCard> vCardList = reader.readAll();
for (VCard vCard : vCardList) {
Person person = convert(vCard);
getOrCreatePeopleService(authData).people().createContact(person).execute();
}
return ImportResult.OK;
} catch (IOException e) {
return new ImportResult(ImportResult.ResultType.ERROR, e.getMessage());
}
}
use of org.dataportabilityproject.spi.transfer.provider.ImportResult in project data-transfer-project by google.
the class FlickrPhotosImporter method importItem.
@Override
public ImportResult importItem(UUID jobId, AuthData authData, PhotosContainerResource data) {
Auth auth;
try {
auth = FlickrUtils.getAuth(authData, flickr);
} catch (FlickrException e) {
return new ImportResult(ImportResult.ResultType.ERROR, "Error authorizing Flickr Auth: " + e.getErrorMessage());
}
RequestContext.getRequestContext().setAuth(auth);
// Store any album data in the cache because Flickr only allows you to create an album with a
// photo in it, so we have to wait for the first photo to create the album
TempPhotosData tempPhotosData = jobStore.findData(TempPhotosData.class, jobId);
if (tempPhotosData == null) {
tempPhotosData = new TempPhotosData(jobId);
jobStore.create(jobId, tempPhotosData);
}
for (PhotoAlbum album : data.getAlbums()) {
tempPhotosData.addAlbum(CACHE_ALBUM_METADATA_PREFIX + album.getId(), album);
}
jobStore.update(jobId, tempPhotosData);
for (PhotoModel photo : data.getPhotos()) {
try {
String photoId = uploadPhoto(photo);
String oldAlbumId = photo.getAlbumId();
TempPhotosData tempData = jobStore.findData(TempPhotosData.class, jobId);
String newAlbumId = tempData.lookupNewAlbumId(oldAlbumId);
if (Strings.isNullOrEmpty(newAlbumId)) {
// This means that we havent created the new album yet, create the photoset
PhotoAlbum album = tempData.lookupAlbum(CACHE_ALBUM_METADATA_PREFIX + oldAlbumId);
Photoset photoset = photosetsInterface.create(COPY_PREFIX + album.getName(), album.getDescription(), photoId);
tempData.addAlbumId(oldAlbumId, photoset.getId());
} else {
// We've already created a new album, add the photo to the new album
photosetsInterface.addPhoto(newAlbumId, photoId);
}
jobStore.update(jobId, tempData);
} catch (FlickrException | IOException e) {
// TODO: figure out retries
return new ImportResult(ImportResult.ResultType.ERROR, "Error importing item: " + e.getMessage());
}
}
return new ImportResult(ImportResult.ResultType.OK);
}
Aggregations