Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 16 additions & 27 deletions lib/music_services/spotifyDef.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var request = require('request-promise');
const settings = require('../../settings');

var clientId = "";
Expand Down Expand Up @@ -72,28 +71,22 @@ const getHeaders = () => {
};
};

const getOptions = (url) => {
return {
url,
headers: getHeaders(),
json: true,
method: 'POST',
form: {
grant_type: 'client_credentials',
},
};
};

const auth = () => {
const options = getOptions(SPOTIFY_TOKEN_URL);
return new Promise((resolve, reject) => {
request(options).then((response) => {
const responseMapped = mapResponse(response);
resolve(responseMapped);
}).catch((err) => {
reject(new Error(`Unable to authenticate Spotify with client id: ${clientId}`));
return fetch(SPOTIFY_TOKEN_URL, {
method: 'POST',
headers: getHeaders(),
body: new URLSearchParams({ grant_type: 'client_credentials' }).toString(),
})
.then((res) => {
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
return res.json();
})
});
.then(mapResponse)
.catch(() => {
throw new Error(`Unable to authenticate Spotify with client id: ${clientId}`);
});
};

function getTokenHeaders() {
Expand All @@ -106,12 +99,8 @@ function getTokenHeaders() {
}

function authenticateService() {
return new Promise((resolve, reject) => {
auth().then((response) => {
const accessToken = response.accessToken;
clientToken = accessToken;
resolve();
}).catch(reject);
return auth().then((response) => {
clientToken = response.accessToken;
});
}

Expand Down