From 171f35ceee0225d9a1ac70ae4b379ef3434d5284 Mon Sep 17 00:00:00 2001 From: Sirius Nottin <30043962+siriusnottin@users.noreply.github.com> Date: Fri, 14 Apr 2023 16:42:18 +0200 Subject: [PATCH 1/5] style and refactor --- pack.ts | 56 +++----------------------------------------------------- 1 file changed, 3 insertions(+), 53 deletions(-) diff --git a/pack.ts b/pack.ts index b32d996..0d3827f 100644 --- a/pack.ts +++ b/pack.ts @@ -1,7 +1,7 @@ import * as coda from "@codahq/packs-sdk"; export const pack = coda.newPack(); -const ApiBaseUrl = "https://photoslibrary.googleapis.com/v1" +const ApiBaseUrl = "https://photoslibrary.googleapis.com/v1"; pack.addNetworkDomain("googleapis.com"); @@ -120,11 +120,7 @@ pack.addSyncTable({ formula: { name: "SyncPhotos", description: "Sync all photos from the user's library.", - parameters: [ - MediaDateRangeParam, - MediaCategoriesParam, - MediaFavoritesParam, - ], + parameters: [MediaDateRangeParam, MediaCategoriesParam, MediaFavoritesParam], execute: async function ([dateRange, categories, favorite], context) { let url = `${ApiBaseUrl}/mediaItems:search`; @@ -182,9 +178,7 @@ pack.addSyncTable({ featureFilter: (favorite) ? { includedFeatures: ["FAVORITES"] } : undefined, contentFilter: (categories) ? { includedContentCategories: categories.map(category => (MediasContentCategoriesList[category])) } : undefined, }, - } - if (context.sync.continuation?.nextPageToken) { - payload.pageToken = context.sync.continuation.nextPageToken; + pageToken: (context.sync.continuation?.nextPageToken) ? context.sync.continuation.nextPageToken : undefined, } let response = await context.fetcher.fetch({ method: "POST", @@ -217,35 +211,6 @@ pack.addSyncTable({ }, }); -// pack.addFormula({ -// name: "GetMedia", -// description: "Get a media from user's library.", -// parameters: [ -// coda.makeParameter({ -// type: coda.ParameterType.String, -// name: "mediaId", -// description: "The id of the media." -// }), -// ], -// resultType: coda.ValueType.Object, -// schema: MediaSchema, -// cacheTtlSecs: 0, -// execute: async function ([mediaId], context) { -// let url = `${ApiBaseUrl}/mediaItems/${mediaId}`; -// let response = await context.fetcher.fetch({ -// method: "GET", -// url: url, -// cacheTtlSecs: 0, -// }); -// return response.body; -// } -// }) - -// pack.addColumnFormat({ -// name: "Photo", -// formulaName: "GetMedia", -// }); - const MediaReferenceSchema = coda.makeReferenceSchemaFromObjectSchema(MediaSchema, "Photo"); const AlbumSchema = coda.makeObjectSchema({ @@ -331,18 +296,3 @@ pack.addSyncTable({ } } }); - -// const MediaAlbumParam = coda.makeParameter({ -// type: coda.ParameterType.String, -// name: "album", -// description: "Filter by album.", -// autocomplete: async function (context, search) { -// let url = `${ApiBaseUrl}/albums`; -// let response = await context.fetcher.fetch({ -// method: "GET", -// url: url, -// }); -// let albums = response.body.albums; -// return coda.autocompleteSearchObjects(search, albums, "title", "id") -// } -// }); From 5722ec5b66e9984c9a3b0fac82a089adeeb71d5b Mon Sep 17 00:00:00 2001 From: Sirius Nottin <30043962+siriusnottin@users.noreply.github.com> Date: Fri, 14 Apr 2023 16:43:22 +0200 Subject: [PATCH 2/5] wip: trying stuff for the api pagination --- pack.ts | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/pack.ts b/pack.ts index 0d3827f..de27765 100644 --- a/pack.ts +++ b/pack.ts @@ -252,46 +252,46 @@ pack.addSyncTable({ parameters: [], execute: async function ([], context) { let baseUrl = `${ApiBaseUrl}/albums`; - let continuation = context.sync.continuation; - if (continuation) { - baseUrl = coda.withQueryParams(baseUrl, { pageToken: continuation.nextAlbumsPageToken }) + if (context.sync.continuation) { + baseUrl = coda.withQueryParams(baseUrl, { pageToken: context.sync.continuation.albumsToken }) }; - let response = await context.fetcher.fetch({ + const response = await context.fetcher.fetch({ method: "GET", url: baseUrl, }); - let albums = response.body.albums; - for (let album of albums) { + + let continuation; + if (response.body.nextPageToken) { + continuation = { + albumsToken: response.body.nextPageToken, + AlbumItemsToken: undefined, + }; + }; + + const albums = await response.body.albums; + for (const album of albums) { // we want to search for all medias in the current album. - let baseUrl = `${ApiBaseUrl}/mediaItems:search` - let body = { albumId: album.id, pageToken: '' }; - if (continuation && continuation.nextAlbumsMediaItemsPageToken) { - body.pageToken = continuation.nextAlbumsMediaItemsPageToken; - } - let response = await context.fetcher.fetch({ + let baseUrl = coda.withQueryParams(`${ApiBaseUrl}/mediaItems:search`, { pageSize: 5 }); + if (context.sync.continuation) { + baseUrl = coda.withQueryParams(baseUrl, { pageToken: context.sync.continuation.AlbumItemsToken }) + }; + debugger; + const body = { albumId: album.id }; + const mediaItemsInAlbum = await context.fetcher.fetch({ method: "POST", url: baseUrl, headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) }); - album.photos = response.body.mediaItems; - album.coverPhoto = album.coverPhotoBaseUrl + "=w2048-h1024" - } - let nextAlbumsPageToken = response.body.nextPageToken; - let nextAlbumsMediaItemsPageToken = null; - if (nextAlbumsPageToken) { - nextAlbumsMediaItemsPageToken = albums[albums.length - 1].photos[albums[albums.length - 1].photos.length - 1].id; - } - let continuationTokens = null; - if (nextAlbumsPageToken || nextAlbumsMediaItemsPageToken) { - continuationTokens = { - nextAlbumsPageToken, - nextAlbumsMediaItemsPageToken, + if (mediaItemsInAlbum.body.nextPageToken) { + continuation.AlbumItemsToken = mediaItemsInAlbum.body.nextPageToken; }; + album.photos = []; + album.coverPhoto = album.coverPhotoBaseUrl + "=w2048-h1024" } return { result: albums, - continuation: continuationTokens, + continuation: continuation, }; } } From ff4900b0eac4049812520fbe015717f0ee481dd3 Mon Sep 17 00:00:00 2001 From: Sirius Nottin <30043962+siriusnottin@users.noreply.github.com> Date: Sat, 15 Apr 2023 16:44:27 +0200 Subject: [PATCH 3/5] style: remove word all since it syncs medias with at least one filter. --- pack.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pack.ts b/pack.ts index de27765..2d9f361 100644 --- a/pack.ts +++ b/pack.ts @@ -119,7 +119,7 @@ pack.addSyncTable({ identityName: "Photo", formula: { name: "SyncPhotos", - description: "Sync all photos from the user's library.", + description: "Sync photos from the user's library.", parameters: [MediaDateRangeParam, MediaCategoriesParam, MediaFavoritesParam], execute: async function ([dateRange, categories, favorite], context) { let url = `${ApiBaseUrl}/mediaItems:search`; From ae3ecbe75459189ab58e3eb07a4b6278eb0db77b Mon Sep 17 00:00:00 2001 From: Sirius Nottin <30043962+siriusnottin@users.noreply.github.com> Date: Sat, 15 Apr 2023 16:50:45 +0200 Subject: [PATCH 4/5] refactor: const in PascalCase & renamed url baseurl was renamed because its content changes overtime. --- pack.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/pack.ts b/pack.ts index 2d9f361..0be2157 100644 --- a/pack.ts +++ b/pack.ts @@ -251,25 +251,24 @@ pack.addSyncTable({ description: "Sync all albums.", parameters: [], execute: async function ([], context) { - let baseUrl = `${ApiBaseUrl}/albums`; + let url = `${ApiBaseUrl}/albums`; + if (context.sync.continuation) { - baseUrl = coda.withQueryParams(baseUrl, { pageToken: context.sync.continuation.albumsToken }) + url = coda.withQueryParams(url, { pageToken: context.sync.continuation }) }; - const response = await context.fetcher.fetch({ + + const AlbumsResponse = await context.fetcher.fetch({ method: "GET", - url: baseUrl, + url, }); - let continuation; - if (response.body.nextPageToken) { - continuation = { - albumsToken: response.body.nextPageToken, - AlbumItemsToken: undefined, - }; + let albumsContinuation; + if (AlbumsResponse.body.nextPageToken) { + albumsContinuation = AlbumsResponse.body.nextPageToken }; - const albums = await response.body.albums; - for (const album of albums) { + const Albums = await AlbumsResponse.body.albums; + for (const album of Albums) { // we want to search for all medias in the current album. let baseUrl = coda.withQueryParams(`${ApiBaseUrl}/mediaItems:search`, { pageSize: 5 }); if (context.sync.continuation) { @@ -290,8 +289,8 @@ pack.addSyncTable({ album.coverPhoto = album.coverPhotoBaseUrl + "=w2048-h1024" } return { - result: albums, - continuation: continuation, + result: Albums, + continuation: albumsContinuation, }; } } From 426536e6d04a8797d126c975125c312dff7228e1 Mon Sep 17 00:00:00 2001 From: Sirius Nottin <30043962+siriusnottin@users.noreply.github.com> Date: Sat, 15 Apr 2023 16:53:01 +0200 Subject: [PATCH 5/5] fix: remove photos in schema (temporarily) --- pack.ts | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/pack.ts b/pack.ts index 0be2157..15d2dcb 100644 --- a/pack.ts +++ b/pack.ts @@ -220,10 +220,10 @@ const AlbumSchema = coda.makeObjectSchema({ fromKey: "id", }, title: { type: coda.ValueType.String }, - photos: { - type: coda.ValueType.Array, - items: MediaReferenceSchema - }, + // photos: { + // type: coda.ValueType.Array, + // items: MediaReferenceSchema + // }, url: { type: coda.ValueType.String, description: "Google Photos URL for the album.", @@ -270,21 +270,20 @@ pack.addSyncTable({ const Albums = await AlbumsResponse.body.albums; for (const album of Albums) { // we want to search for all medias in the current album. - let baseUrl = coda.withQueryParams(`${ApiBaseUrl}/mediaItems:search`, { pageSize: 5 }); - if (context.sync.continuation) { - baseUrl = coda.withQueryParams(baseUrl, { pageToken: context.sync.continuation.AlbumItemsToken }) - }; - debugger; - const body = { albumId: album.id }; - const mediaItemsInAlbum = await context.fetcher.fetch({ - method: "POST", - url: baseUrl, - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(body) - }); - if (mediaItemsInAlbum.body.nextPageToken) { - continuation.AlbumItemsToken = mediaItemsInAlbum.body.nextPageToken; - }; + // let url = coda.withQueryParams(`${ApiBaseUrl}/mediaItems:search`, { pageSize: 5 }); + // let body = { albumId: album.id }; + // let mediaItemsInAlbum = []; + // let mediaItemsNextPageToken; + + // const mediaItemsInAlbumResponse = await context.fetcher.fetch({ + // method: "POST", + // url, + // headers: { "Content-Type": "application/json" }, + // body: JSON.stringify(body) + // }); + // if (mediaItemsInAlbumResponse.body.nextPageToken) { + // continuation.AlbumMediaItemsNextPageToken = mediaItemsInAlbumResponse.body.nextPageToken; + // }; album.photos = []; album.coverPhoto = album.coverPhotoBaseUrl + "=w2048-h1024" }