Persist googlesheet refs in context

This commit is contained in:
Adria Navarro 2024-05-16 14:41:45 +02:00
parent a705fbd497
commit 7807b734bb
2 changed files with 49 additions and 19 deletions

View File

@ -1,4 +1,6 @@
import { IdentityContext, Snippet, VM } from "@budibase/types" import { IdentityContext, Snippet, VM } from "@budibase/types"
import { OAuth2Client } from "google-auth-library"
import { GoogleSpreadsheet } from "google-spreadsheet"
// keep this out of Budibase types, don't want to expose context info // keep this out of Budibase types, don't want to expose context info
export type ContextMap = { export type ContextMap = {
@ -12,4 +14,8 @@ export type ContextMap = {
vm?: VM vm?: VM
cleanup?: (() => void | Promise<void>)[] cleanup?: (() => void | Promise<void>)[]
snippets?: Snippet[] snippets?: Snippet[]
googleSheets?: {
oauthClient: OAuth2Client
clients: Record<string, GoogleSpreadsheet>
}
} }

View File

@ -228,32 +228,56 @@ class GoogleSheetsIntegration implements DatasourcePlus {
private async connect() { private async connect() {
try { try {
await setupCreationAuth(this.config) const bbCtx = context.getCurrentContext()
let oauthClient = bbCtx?.googleSheets?.oauthClient
// Initialise oAuth client if (!oauthClient) {
const googleConfig = await configs.getGoogleDatasourceConfig() await setupCreationAuth(this.config)
if (!googleConfig) {
throw new HTTPError("Google config not found", 400)
}
const oauthClient = new OAuth2Client({ // Initialise oAuth client
clientId: googleConfig.clientID, const googleConfig = await configs.getGoogleDatasourceConfig()
clientSecret: googleConfig.clientSecret, if (!googleConfig) {
}) throw new HTTPError("Google config not found", 400)
}
const tokenResponse = await this.fetchAccessToken({ oauthClient = new OAuth2Client({
client_id: googleConfig.clientID, clientId: googleConfig.clientID,
client_secret: googleConfig.clientSecret, clientSecret: googleConfig.clientSecret,
refresh_token: this.config.auth.refreshToken, })
})
oauthClient.setCredentials({ const tokenResponse = await this.fetchAccessToken({
refresh_token: this.config.auth.refreshToken, client_id: googleConfig.clientID,
access_token: tokenResponse.access_token, client_secret: googleConfig.clientSecret,
}) refresh_token: this.config.auth.refreshToken,
})
oauthClient.setCredentials({
refresh_token: this.config.auth.refreshToken,
access_token: tokenResponse.access_token,
})
if (bbCtx && !bbCtx.googleSheets) {
bbCtx.googleSheets = {
oauthClient,
clients: {},
}
bbCtx.cleanup = bbCtx.cleanup || []
this.client = new GoogleSpreadsheet(this.spreadsheetId, oauthClient) this.client = new GoogleSpreadsheet(this.spreadsheetId, oauthClient)
await this.client.loadInfo() await this.client.loadInfo()
}
}
let client = bbCtx?.googleSheets?.clients[this.spreadsheetId]
if (!client) {
client = new GoogleSpreadsheet(this.spreadsheetId, oauthClient)
await client.loadInfo()
if (bbCtx?.googleSheets?.clients) {
bbCtx.googleSheets.clients[this.spreadsheetId] = client
}
}
this.client = client
} catch (err: any) { } catch (err: any) {
// this happens for xlsx imports // this happens for xlsx imports
if (err.message?.includes("operation is not supported")) { if (err.message?.includes("operation is not supported")) {