Skip to content

Commit

Permalink
Adding support for My Drive in Gdrive (#1286)
Browse files Browse the repository at this point in the history
* Adding support for My Drive in Gdrive

* Put My Drive at the top of the list
  • Loading branch information
lasryaric authored and philipperolet committed Sep 6, 2023
1 parent a43e986 commit 9d2c279
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
55 changes: 40 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,22 @@ 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 }[] = [];
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 });
do {
const res = await drive.drives.list({
pageSize: 100,
Expand All @@ -152,7 +163,7 @@ 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;
Expand Down Expand Up @@ -490,6 +501,7 @@ export async function incrementalSync(
nangoConnectionId: string,
dataSourceConfig: DataSourceConfig,
driveId: string,
sharedDrive: boolean,
startSyncTs: number,
nextPageToken?: string
): Promise<string | undefined> {
Expand All @@ -506,7 +518,8 @@ export async function incrementalSync(
nextPageToken = await getSyncPageToken(
connectorId,
nangoConnectionId,
driveId
driveId,
sharedDrive
);
}

Expand All @@ -515,15 +528,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 +663,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 +678,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 +866,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 9d2c279

Please sign in to comment.