2023-03-09 09:13:15 -08:00
|
|
|
import type {
|
|
|
|
IExecuteFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IDataObject,
|
|
|
|
JsonObject,
|
2024-02-14 07:29:09 -08:00
|
|
|
IHttpRequestMethods,
|
|
|
|
IRequestOptions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2023-01-27 03:22:44 -08:00
|
|
|
import { NodeApiError, NodeOperationError } from 'n8n-workflow';
|
2023-06-06 09:19:24 -07:00
|
|
|
import { getGoogleAccessToken } from '../../GenericFunctions';
|
2023-01-13 09:11:56 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function googleApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
resource: string,
|
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
headers: IDataObject = {},
|
|
|
|
): Promise<any> {
|
|
|
|
const authenticationMethod = this.getNodeParameter(
|
|
|
|
'authentication',
|
|
|
|
0,
|
|
|
|
'serviceAccount',
|
|
|
|
) as string;
|
2022-04-18 09:46:50 -07:00
|
|
|
|
2024-02-14 07:29:09 -08:00
|
|
|
const options: IRequestOptions = {
|
2021-04-17 06:04:25 -07:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `https://bigquery.googleapis.com/bigquery${resource}`,
|
2021-04-17 06:04:25 -07:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
if (Object.keys(headers).length !== 0) {
|
|
|
|
options.headers = Object.assign({}, options.headers, headers);
|
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(body as IDataObject).length === 0) {
|
2021-04-17 06:04:25 -07:00
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
2022-04-18 09:46:50 -07:00
|
|
|
if (authenticationMethod === 'serviceAccount') {
|
|
|
|
const credentials = await this.getCredentials('googleApi');
|
2021-04-17 06:04:25 -07:00
|
|
|
|
2022-04-18 09:46:50 -07:00
|
|
|
if (credentials === undefined) {
|
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
|
|
|
}
|
|
|
|
|
2023-06-06 09:19:24 -07:00
|
|
|
const { access_token } = await getGoogleAccessToken.call(this, credentials, 'bigquery');
|
2022-04-18 09:46:50 -07:00
|
|
|
|
|
|
|
options.headers!.Authorization = `Bearer ${access_token}`;
|
2022-12-23 10:09:52 -08:00
|
|
|
return await this.helpers.request(options);
|
2022-04-18 09:46:50 -07:00
|
|
|
} else {
|
|
|
|
return await this.helpers.requestOAuth2.call(this, 'googleBigQueryOAuth2Api', options);
|
2021-04-17 06:04:25 -07:00
|
|
|
}
|
2022-04-18 09:46:50 -07:00
|
|
|
} catch (error) {
|
|
|
|
if (error.code === 'ERR_OSSL_PEM_NO_START_LINE') {
|
|
|
|
error.statusCode = '401';
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new NodeApiError(this.getNode(), error as JsonObject);
|
2021-04-17 06:04:25 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function googleApiRequestAllItems(
|
|
|
|
this: IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
propertyName: string,
|
2024-02-14 07:29:09 -08:00
|
|
|
method: IHttpRequestMethods,
|
2022-08-17 08:50:24 -07:00
|
|
|
endpoint: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2021-04-17 06:04:25 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
query.maxResults = 100;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await googleApiRequest.call(this, method, endpoint, body, query);
|
2022-12-02 12:54:28 -08:00
|
|
|
query.pageToken = responseData.pageToken;
|
2023-02-27 19:39:43 -08:00
|
|
|
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
2022-12-02 12:54:28 -08:00
|
|
|
} while (responseData.pageToken !== undefined && responseData.pageToken !== '');
|
2021-04-17 06:04:25 -07:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function simplify(rows: IDataObject[], fields: string[]) {
|
|
|
|
const results = [];
|
|
|
|
for (const row of rows) {
|
|
|
|
const record: IDataObject = {};
|
|
|
|
for (const [index, field] of fields.entries()) {
|
|
|
|
record[field] = (row.f as IDataObject[])[index].v;
|
|
|
|
}
|
|
|
|
results.push(record);
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|