Skip to content

Commit

Permalink
Adding support for My Drive in Gdrive
Browse files Browse the repository at this point in the history
  • Loading branch information
lasryaric committed Sep 6, 2023
1 parent d849aba commit e77d4f0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
56 changes: 41 additions & 15 deletions connectors/src/connectors/google_drive/temporal/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ export async function getDrivesIds(nangoConnectionId: string): Promise<
{
id: string;
name: string;
sharedDrive: boolean;
}[]
> {
const drive = await getDriveClient(nangoConnectionId);
let nextPageToken = undefined;
const ids = [];
const ids: { id: string; name: string; sharedDrive: boolean }[] = [];
do {
const res = await drive.drives.list({
pageSize: 100,
Expand All @@ -152,12 +153,23 @@ export async function getDrivesIds(nangoConnectionId: string): Promise<
}
for (const drive of res.data.drives) {
if (drive.id && drive.name) {
ids.push({ id: drive.id, name: drive.name });
ids.push({ id: drive.id, name: drive.name, sharedDrive: true });
}
}
nextPageToken = res.data.nextPageToken;
} while (nextPageToken);

const myDriveRes = await drive.files.get({ fileId: "root" });
if (myDriveRes.status !== 200) {
throw new Error(
`Error getting my drive. status_code: ${myDriveRes.status}. status_text: ${myDriveRes.statusText}`
);
}
if (!myDriveRes.data.id) {
throw new Error("My drive id is undefined");
}
ids.push({ id: myDriveRes.data.id, name: "My Drive", sharedDrive: false });

return ids;
}

Expand Down Expand Up @@ -490,6 +502,7 @@ export async function incrementalSync(
nangoConnectionId: string,
dataSourceConfig: DataSourceConfig,
driveId: string,
sharedDrive: boolean,
startSyncTs: number,
nextPageToken?: string
): Promise<string | undefined> {
Expand All @@ -506,7 +519,8 @@ export async function incrementalSync(
nextPageToken = await getSyncPageToken(
connectorId,
nangoConnectionId,
driveId
driveId,
sharedDrive
);
}

Expand All @@ -515,15 +529,21 @@ export async function incrementalSync(
const authCredentials = await getAuthObject(nangoConnectionId);
const driveClient = await getDriveClient(authCredentials);

const changesRes: GaxiosResponse<drive_v3.Schema$ChangeList> =
await driveClient.changes.list({
let opts: drive_v3.Params$Resource$Changes$List = {
pageToken: nextPageToken,
pageSize: 100,
fields: "*",
};
if (sharedDrive) {
opts = {
...opts,
driveId: driveId,
pageToken: nextPageToken,
pageSize: 100,
fields: "*",
includeItemsFromAllDrives: true,
supportsAllDrives: true,
});
};
}
const changesRes: GaxiosResponse<drive_v3.Schema$ChangeList> =
await driveClient.changes.list(opts);

if (changesRes.status !== 200) {
throw new Error(
Expand Down Expand Up @@ -644,7 +664,8 @@ export async function incrementalSync(
async function getSyncPageToken(
connectorId: ModelId,
nangoConnectionId: string,
driveId: string
driveId: string,
sharedDrive: boolean
) {
const last = await GoogleDriveSyncToken.findOne({
where: {
Expand All @@ -658,10 +679,14 @@ async function getSyncPageToken(
const driveClient = await getDriveClient(nangoConnectionId);
let lastSyncToken = undefined;
if (!lastSyncToken) {
const startTokenRes = await driveClient.changes.getStartPageToken({
driveId: driveId,
supportsAllDrives: true,
});
let opts = {};
if (sharedDrive) {
opts = {
driveId: driveId,
supportsAllDrives: true,
};
}
const startTokenRes = await driveClient.changes.getStartPageToken(opts);
if (startTokenRes.status !== 200) {
throw new Error(
`Error getting start page token. status_code: ${startTokenRes.status}. status_text: ${startTokenRes.statusText}`
Expand Down Expand Up @@ -842,7 +867,8 @@ export async function populateSyncTokens(connectorId: ModelId) {
const lastSyncToken = await getSyncPageToken(
connectorId,
connector.connectionId,
drive.id
drive.id,
drive.sharedDrive
);
await GoogleDriveSyncToken.upsert({
connectorId: connectorId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export async function googleDriveIncrementalSync(
nangoConnectionId,
dataSourceConfig,
googleDrive.id,
googleDrive.sharedDrive,
startSyncTs,
nextPageToken
);
Expand Down

0 comments on commit e77d4f0

Please sign in to comment.