Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.datatransferproject.datatransfer.google.calendar;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.auth.oauth2.TokenErrorResponse;
import com.google.api.client.auth.oauth2.TokenResponseException;
import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.model.Event;
Expand All @@ -12,6 +14,7 @@
import org.datatransferproject.spi.transfer.idempotentexecutor.IdempotentImportExecutor;
import org.datatransferproject.spi.transfer.provider.ImportResult;
import org.datatransferproject.spi.transfer.provider.Importer;
import org.datatransferproject.spi.transfer.types.InvalidTokenException;
import org.datatransferproject.types.common.models.calendar.CalendarAttendeeModel;
import org.datatransferproject.types.common.models.calendar.CalendarContainerResource;
import org.datatransferproject.types.common.models.calendar.CalendarEventModel;
Expand Down Expand Up @@ -113,26 +116,42 @@ public ImportResult importItem(UUID jobId,

@VisibleForTesting
String importSingleCalendar(TokensAndUrlAuthData authData, CalendarModel calendarModel)
throws IOException {
throws IOException, InvalidTokenException {
com.google.api.services.calendar.model.Calendar toInsert = convertToGoogleCalendar(
calendarModel);
com.google.api.services.calendar.model.Calendar calendarResult =
getOrCreateCalendarInterface(authData).calendars().insert(toInsert).execute();
return calendarResult.getId();
try {
com.google.api.services.calendar.model.Calendar calendarResult =
getOrCreateCalendarInterface(authData).calendars().insert(toInsert).execute();
return calendarResult.getId();
} catch (TokenResponseException e) {
TokenErrorResponse details = e.getDetails();
if (details != null && "invalid_grant".equals(details.getError())) {
throw new InvalidTokenException("Unable to refresh token.", e);
}
throw e;
}
}

@VisibleForTesting
String importSingleEvent(IdempotentImportExecutor idempotentImportExecutor,
TokensAndUrlAuthData authData,
CalendarEventModel eventModel)
throws IOException {
throws IOException, InvalidTokenException {
Event event = convertToGoogleCalendarEvent(eventModel);
String newCalendarId = idempotentImportExecutor.getCachedValue(eventModel.getCalendarId());
return getOrCreateCalendarInterface(authData)
.events()
.insert(newCalendarId, event)
.execute()
.getId();
try {
return getOrCreateCalendarInterface(authData)
.events()
.insert(newCalendarId, event)
.execute()
.getId();
} catch (TokenResponseException e) {
TokenErrorResponse details = e.getDetails();
if (details != null && "invalid_grant".equals(details.getError())) {
throw new InvalidTokenException("Unable to refresh token.", e);
}
throw e;
}
}

private Calendar getOrCreateCalendarInterface(TokensAndUrlAuthData authData) {
Expand Down
Loading