sendRequest(params, sessionID) { let requestInfo = { method: 'POST', headers: { 'Authorization': `Token ${sessionID}`, 'Content-Type': 'application/json; charset=UTF-8' }, body: JSON.stringify(params) }; let promise = fetch(this.apiUrl, requestInfo).then((response) => { switch (response.status) { case 400: this.debugLog('Invalid JSON sent to ListenBrainz', 'error'); throw new ServiceCallResult(ServiceCallResult.ERROR_AUTH); case 401: this.debugLog('Invalid Authorization sent to ListenBrainz', 'error'); throw new ServiceCallResult(ServiceCallResult.ERROR_AUTH); } return response.text(); }).then((text) => { this.debugLog(text); return new ServiceCallResult(ServiceCallResult.OK); }).catch((error) => { this.debugLog(error.text(), 'warn'); throw new ServiceCallResult(ServiceCallResult.ERROR_OTHER); }); let timeout = BaseScrobbler.REQUEST_TIMEOUT; return Util.timeoutPromise(timeout, promise).catch(() => { this.debugLog('TIMED OUT', 'warn'); throw new ServiceCallResult(ServiceCallResult.ERROR_OTHER); }); }
async fetchSession(url) { this.debugLog(`Use ${url}`); // NOTE: Use 'same-origin' credentials to fix login on Firefox ESR 60. let promise = fetch(url, { method: 'GET', credentials: 'same-origin' }); let timeout = BaseScrobbler.REQUEST_TIMEOUT; let response = await Util.timeoutPromise(timeout, promise); let $doc = $(await response.text()); let sessionName = $doc.find('.page-title').text(); let sessionID = $doc.find('#auth-token').val(); if (sessionID && sessionName) { return { sessionID, sessionName }; } return null; }