2021-03-10 09:50:12 -08:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
2020-06-24 11:16:48 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make an API request to Spotify
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} body
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
export async function spotifyApiRequest(this: IHookFunctions | IExecuteFunctions,
|
|
|
|
method: string, endpoint: string, body: object, query?: object, uri?: string): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'n8n',
|
|
|
|
'Content-Type': 'text/plain',
|
|
|
|
'Accept': ' application/json',
|
|
|
|
},
|
|
|
|
body,
|
|
|
|
qs: query,
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
const credentials = this.getCredentials('spotifyOAuth2Api');
|
|
|
|
|
|
|
|
if (credentials === undefined) {
|
|
|
|
throw new Error('No credentials got returned!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.helpers.requestOAuth2.call(this, 'spotifyOAuth2Api', options);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.statusCode === 401) {
|
2020-10-01 05:01:39 -07:00
|
|
|
// Return a clear error
|
|
|
|
throw new Error('The Spotify credentials are not valid!');
|
|
|
|
}
|
2020-06-24 11:16:48 -07:00
|
|
|
|
2020-10-01 05:01:39 -07:00
|
|
|
if (error.error && error.error.error && error.error.error.message) {
|
|
|
|
// Try to return the error prettier
|
|
|
|
throw new Error(`Spotify error response [${error.error.error.status}]: ${error.error.error.message}`);
|
|
|
|
}
|
2020-06-24 11:16:48 -07:00
|
|
|
|
2020-10-01 05:01:39 -07:00
|
|
|
// If that data does not exist for some reason return the actual error
|
|
|
|
throw error;
|
2020-06-24 11:16:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function spotifyApiRequestAllItems(this: IHookFunctions | IExecuteFunctions,
|
|
|
|
propertyName: string, method: string, endpoint: string, body: object, query?: object): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
let uri: string | undefined;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await spotifyApiRequest.call(this, method, endpoint, body, query, uri);
|
|
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
|
|
uri = responseData.next;
|
|
|
|
|
|
|
|
} while (
|
|
|
|
responseData['next'] !== null
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|