Re-wrote most all api calls to use fetch over axios. There is still a problem with form authentication with plex. The response we get does not seem to be a json object. Updated what is expected to return from altered api methods in each component that uses them

This commit is contained in:
2019-10-23 00:30:37 +02:00
parent 4a46bbd2be
commit 39cd5ce04a
5 changed files with 84 additions and 30 deletions

View File

@@ -25,7 +25,8 @@ const getMovie = (id, credits=false) => {
url.searchParams.append('credits', true)
}
return axios.get(url.href)
return fetch(url.href)
.then(resp => resp.json())
.catch(error => { console.error(`api error getting movie: ${id}`); throw error })
}
@@ -42,24 +43,51 @@ const getShow = (id, credits=false) => {
url.searchParams.append('credits', true)
}
return axios.get(url.href)
return fetch(url.href)
.then(resp => resp.json())
.catch(error => { console.error(`api error getting show: ${id}`); throw error })
}
/**
* Fetches tmdb list by path.
* @param {string} listPath Path of list
* Fetches tmdb list by name.
* @param {string} name List the fetch
* @param {number} [page=1]
* @returns {object} Tmdb list response
*/
const getTmdbListByPath = (listPath, page=1) => {
const url = new URL(listPath, SEASONED_URL)
const getTmdbMovieListByName = (name, page=1) => {
const url = new URL('v2/movie/' + name, SEASONED_URL)
url.searchParams.append('page', page)
// TODO - remove. this is temporary fix for user-requests endpoint (also import)
const headers = { authorization: storage.token }
return axios.get(url.href, { headers: headers })
.catch(error => { console.error(`api error getting list: ${listPath}, page: ${page}`); throw error })
return fetch(url.href, { headers: headers })
.then(resp => resp.json())
// .catch(error => { console.error(`api error getting list: ${name}, page: ${page}`); throw error })
}
/**
* Fetches requested items.
* @param {number} [page=1]
* @returns {object} Request response
*/
const getRequests = (page=1) => {
const url = new URL('v2/request', SEASONED_URL)
url.searchParams.append('page', page)
const headers = { authorization: storage.token }
return fetch(url.href, { headers: headers })
.then(resp => resp.json())
// .catch(error => { console.error(`api error getting list: ${name}, page: ${page}`); throw error })
}
const getUserRequests = (page=1) => {
const url = new URL('v1/user/requests', SEASONED_URL)
url.searchParams.append('page', page)
const headers = { authorization: localStorage.getItem('token') }
return fetch(url.href, { headers })
.then(resp => resp.json())
}
/**
@@ -73,7 +101,8 @@ const searchTmdb = (query, page=1) => {
url.searchParams.append('query', query)
url.searchParams.append('page', page)
return axios.get(url.href)
return fetch(url.href)
.then(resp => resp.json())
.catch(error => { console.error(`api error searching: ${query}, page: ${page}`); throw error })
}
@@ -86,12 +115,13 @@ const searchTmdb = (query, page=1) => {
* @returns {object} Torrent response
*/
const searchTorrents = (query, authorization_token) => {
const url = new URL('v1/pirate/search', SEASONED_URL)
const url = new URL('/api/v1/pirate/search', SEASONED_URL)
url.searchParams.append('query', query)
const headers = { authorization: storage.token }
return axios.get(url.href, { headers: headers })
return fetch(url.href, { headers: headers })
.then(resp => resp.json())
.catch(error => { console.error(`api error searching torrents: ${query}`); throw error })
}
@@ -112,8 +142,9 @@ const addMagnet = (magnet, name, tmdb_id) => {
}
const headers = { authorization: storage.token }
return axios.post(url.href, body, { headers: headers })
.catch(error => { console.error(`api error adding magnet: ${name}`); throw error })
return fetch(url.href, { method: 'POST', headers, body })
.then(resp => resp.json())
.catch(error => { console.error(`api error adding magnet: ${name} ${error}`); throw error })
}
// - - - Plex/Request - - -
@@ -175,9 +206,7 @@ const getRequestStatus = (id, type, authorization_token=undefined) => {
// - - - Authenticate with plex - - -
const plexAuthenticate = (username, password) => {
const url = new URL('https://plex.tv/users/sign_in.json')
url.searchParams.append('user[login]', username)
url.searchParams.append('user[password]', password)
const url = new URL('https://plex.tv/api/v2/users/signin')
const headers = {
'Content-Type': 'application/json',
@@ -188,7 +217,17 @@ const plexAuthenticate = (username, password) => {
'X-Plex-Client-Identifier': '123'
}
return axios.post(url.href, { headers: headers })
let formData = new FormData()
formData.set('login', username)
formData.set('password', password)
formData.set('rememberMe', false)
return axios({
method: 'POST',
url: url.href,
headers: headers,
data: formData
})
.catch(error => { console.error(`api error authentication plex: ${username}`); throw error })
}
@@ -198,7 +237,8 @@ const plexAuthenticate = (username, password) => {
const getEmoji = () => {
const url = new URL('v1/emoji', SEASONED_URL)
return axios.get(url.href)
return fetch(url.href)
.then(resp => resp.json())
.catch(error => { console.log('api error getting emoji'); throw error })
}
@@ -252,4 +292,18 @@ const elasticSearchMoviesAndShows = (query) => {
export { getMovie, getShow, getTmdbListByPath, searchTmdb, searchTorrents, addMagnet, request, getRequestStatus, plexAuthenticate, getEmoji, elasticSearchMoviesAndShows }
export {
getMovie,
getShow,
getTmdbMovieListByName,
searchTmdb,
getUserRequests,
getRequests,
searchTorrents,
addMagnet,
request,
getRequestStatus,
plexAuthenticate,
getEmoji,
elasticSearchMoviesAndShows
}

View File

@@ -146,8 +146,7 @@ export default {
}
},
methods: {
parseResponse(resp) {
let movie = resp.data;
parseResponse(movie) {
this.movie = { ...movie }
this.title = movie.title
this.poster = movie.poster

View File

@@ -54,9 +54,7 @@ export default {
searchTmdb(query, page)
.then(this.parseResponse)
},
parseResponse(response) {
const data = response.data
parseResponse(data) {
if (this.results.length > 0) {
this.results.push(...data.results)
} else {

View File

@@ -104,7 +104,7 @@ export default {
elasticSearchMoviesAndShows(this.query)
.then(resp => {
const data = resp.data.hits.hits
const data = resp.hits.hits
this.elasticSearchResults = data.map(item => {
const index = item._index.slice(0, -1)

View File

@@ -75,12 +75,15 @@ export default {
let password = this.plexPassword
plexAuthenticate(username, password)
.then((resp) => {
let data = resp.data;
.then(resp => {
const data = resp.data
this.messages.push({ type: 'success', title: 'Authenticated with plex', message: 'Successfully linked plex account with seasoned request' })
// console.log('response from plex:', data.user)
console.log('response from plex:', data.username)
})
.catch((error) => {
.catch(error => {
console.error(error);
this.messages.push({ type: 'error', title: 'Something went wrong', message: error.message })
})
}