diff --git a/lib/actions/musicSearch.js b/lib/actions/musicSearch.js index 5625284c..867616c6 100644 --- a/lib/actions/musicSearch.js +++ b/lib/actions/musicSearch.js @@ -1,8 +1,16 @@ 'use strict'; -const request = require('request-promise'); const fs = require("fs"); const isRadioOrLineIn = require('../helpers/is-radio-or-line-in'); +function httpGet({ url, json, headers }) { + return fetch(url, { headers }).then((res) => { + if (!res.ok) { + return Promise.reject(new Error(`HTTP ${res.status} ${res.statusText} for ${url}`)); + } + return json ? res.json() : res.text(); + }); +} + const appleDef = require('../music_services/appleDef'); const spotifyDef = require('../music_services/spotifyDef'); const deezerDef = require('../music_services/deezerDef'); @@ -41,7 +49,7 @@ function getAccountId(player, service) accountId = ''; if (service != 'library') { - return request({url: player.baseUrl + '/status/accounts',json: false}) + return httpGet({url: player.baseUrl + '/status/accounts',json: false}) .then((res) => { var actLoc = res.indexOf(player.system.getServiceType(serviceNames[service])); @@ -118,19 +126,19 @@ function doSearch(service, type, term) return Promise.resolve(libraryDef.searchlib(type, newTerm)); } else if ((serviceDef.country != '') && (country == '')) { - return request({url: 'http://ipinfo.io', + return httpGet({url: 'http://ipinfo.io', json: true}) .then((res) => { country = res.country; url += serviceDef.country + country; - return authenticate().then(() => request(getRequestOptions(serviceDef, url))); + return authenticate().then(() => httpGet(getRequestOptions(serviceDef, url))); }); } else { if (serviceDef.country != '') { url += serviceDef.country + country; } - - return authenticate().then(() => request(getRequestOptions(serviceDef, url))); + + return authenticate().then(() => httpGet(getRequestOptions(serviceDef, url))); } }