2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-06-24 11:16:48 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IExecuteFunctions, IHookFunctions } from 'n8n-core';
|
2020-06-24 11:16:48 -07:00
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
2020-06-24 11:16:48 -07:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { get } from 'lodash';
|
2021-06-18 14:41:57 -07:00
|
|
|
|
2020-06-24 11:16:48 -07:00
|
|
|
/**
|
|
|
|
* Make an API request to Spotify
|
|
|
|
*
|
|
|
|
*/
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function spotifyApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: object,
|
|
|
|
query?: object,
|
|
|
|
uri?: string,
|
|
|
|
): Promise<any> {
|
2020-06-24 11:16:48 -07:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'n8n',
|
|
|
|
'Content-Type': 'text/plain',
|
2022-08-17 08:50:24 -07:00
|
|
|
Accept: ' application/json',
|
2020-06-24 11:16:48 -07:00
|
|
|
},
|
|
|
|
qs: query,
|
2023-01-13 09:11:56 -08:00
|
|
|
uri: uri ?? `https://api.spotify.com/v1${endpoint}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-06-24 11:16:48 -07:00
|
|
|
};
|
|
|
|
|
2021-04-16 09:33:36 -07:00
|
|
|
if (Object.keys(body).length > 0) {
|
|
|
|
options.body = body;
|
|
|
|
}
|
|
|
|
try {
|
2020-06-24 11:16:48 -07:00
|
|
|
return await this.helpers.requestOAuth2.call(this, 'spotifyOAuth2Api', options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-06-24 11:16:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function spotifyApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
propertyName: string,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
body: object,
|
|
|
|
query?: object,
|
|
|
|
): Promise<any> {
|
2020-06-24 11:16:48 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
let uri: string | undefined;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await spotifyApiRequest.call(this, method, endpoint, body, query, uri);
|
2021-06-18 14:41:57 -07:00
|
|
|
returnData.push.apply(returnData, get(responseData, propertyName));
|
|
|
|
uri = responseData.next || responseData[propertyName.split('.')[0]].next;
|
|
|
|
//remove the query as the query parameters are already included in the next, else api throws error.
|
|
|
|
query = {};
|
2022-01-21 05:10:30 -08:00
|
|
|
if (uri?.includes('offset=1000') && endpoint === '/search') {
|
|
|
|
// The search endpoint has a limit of 1000 so step before it returns a 404
|
2021-06-18 14:41:57 -07:00
|
|
|
return returnData;
|
|
|
|
}
|
2020-06-24 11:16:48 -07:00
|
|
|
} while (
|
2022-12-02 12:54:28 -08:00
|
|
|
(responseData.next !== null && responseData.next !== undefined) ||
|
2022-08-17 08:50:24 -07:00
|
|
|
(responseData[propertyName.split('.')[0]].next !== null &&
|
|
|
|
responseData[propertyName.split('.')[0]].next !== undefined)
|
2020-06-24 11:16:48 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|