api functions for linking, unlinking-plex account, get settings and update settings.
This commit is contained in:
71
src/api.js
71
src/api.js
@@ -254,10 +254,55 @@ const login = (username, password) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getSettings = () => {
|
||||||
|
const settingsExists = (value) => {
|
||||||
|
if (value instanceof Object && value.hasOwnProperty('settings'))
|
||||||
|
return value;
|
||||||
|
throw "Settings does not exist in response object.";
|
||||||
|
}
|
||||||
|
const commitSettingsToStore = (response) => {
|
||||||
|
store.dispatch('userModule/setSettings', response.settings)
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = new URL('v1/user/settings', SEASONED_URL)
|
||||||
|
|
||||||
|
const authorization_token = localStorage.getItem('token')
|
||||||
|
const headers = authorization_token ? {
|
||||||
|
'Authorization': authorization_token,
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
} : {}
|
||||||
|
|
||||||
|
return fetch(url.href, { headers })
|
||||||
|
.then(resp => resp.json())
|
||||||
|
.then(settingsExists)
|
||||||
|
.then(commitSettingsToStore)
|
||||||
|
.then(response => response.settings)
|
||||||
|
.catch(error => { console.log('api error getting user settings'); throw error })
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateSettings = (settings) => {
|
||||||
|
const url = new URL('v1/user/settings', SEASONED_URL)
|
||||||
|
|
||||||
|
const authorization_token = localStorage.getItem('token')
|
||||||
|
const headers = authorization_token ? {
|
||||||
|
'Authorization': authorization_token,
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
} : {}
|
||||||
|
|
||||||
|
return fetch(url.href, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers,
|
||||||
|
body: JSON.stringify(settings)
|
||||||
|
})
|
||||||
|
.then(resp => resp.json())
|
||||||
|
.catch(error => { console.log('api error updating user settings'); throw error })
|
||||||
|
}
|
||||||
|
|
||||||
// - - - Authenticate with plex - - -
|
// - - - Authenticate with plex - - -
|
||||||
|
|
||||||
const plexAuthenticate = (username, password) => {
|
const linkPlexAccount = (username, password) => {
|
||||||
const url = new URL('v1/user/authenticate', SEASONED_URL)
|
const url = new URL('v1/user/link_plex', SEASONED_URL)
|
||||||
const body = { username, password }
|
const body = { username, password }
|
||||||
const headers = {
|
const headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@@ -270,7 +315,22 @@ const plexAuthenticate = (username, password) => {
|
|||||||
body: JSON.stringify(body)
|
body: JSON.stringify(body)
|
||||||
})
|
})
|
||||||
.then(resp => resp.json())
|
.then(resp => resp.json())
|
||||||
.catch(error => { console.error(`api error authentication plex: ${username}`); throw error })
|
.catch(error => { console.error(`api error linking plex account: ${username}`); throw error })
|
||||||
|
}
|
||||||
|
|
||||||
|
const unlinkPlexAccount = (username, password) => {
|
||||||
|
const url = new URL('v1/user/unlink_plex', SEASONED_URL)
|
||||||
|
const headers = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
authorization: storage.token
|
||||||
|
}
|
||||||
|
|
||||||
|
return fetch(url.href, {
|
||||||
|
method: 'POST',
|
||||||
|
headers
|
||||||
|
})
|
||||||
|
.then(resp => resp.json())
|
||||||
|
.catch(error => { console.error(`api error unlinking plex account: ${username}`); throw error })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -345,9 +405,12 @@ export {
|
|||||||
addMagnet,
|
addMagnet,
|
||||||
request,
|
request,
|
||||||
getRequestStatus,
|
getRequestStatus,
|
||||||
plexAuthenticate,
|
linkPlexAccount,
|
||||||
|
unlinkPlexAccount,
|
||||||
register,
|
register,
|
||||||
login,
|
login,
|
||||||
|
getSettings,
|
||||||
|
updateSettings,
|
||||||
getEmoji,
|
getEmoji,
|
||||||
elasticSearchMoviesAndShows
|
elasticSearchMoviesAndShows
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user