mirror of
https://github.com/KevinMidboe/seasoned.git
synced 2026-03-11 11:55:38 +00:00
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:
94
src/api.js
94
src/api.js
@@ -25,7 +25,8 @@ const getMovie = (id, credits=false) => {
|
|||||||
url.searchParams.append('credits', true)
|
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 })
|
.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)
|
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 })
|
.catch(error => { console.error(`api error getting show: ${id}`); throw error })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches tmdb list by path.
|
* Fetches tmdb list by name.
|
||||||
* @param {string} listPath Path of list
|
* @param {string} name List the fetch
|
||||||
* @param {number} [page=1]
|
* @param {number} [page=1]
|
||||||
* @returns {object} Tmdb list response
|
* @returns {object} Tmdb list response
|
||||||
*/
|
*/
|
||||||
const getTmdbListByPath = (listPath, page=1) => {
|
const getTmdbMovieListByName = (name, page=1) => {
|
||||||
const url = new URL(listPath, SEASONED_URL)
|
const url = new URL('v2/movie/' + name, SEASONED_URL)
|
||||||
url.searchParams.append('page', page)
|
url.searchParams.append('page', page)
|
||||||
// TODO - remove. this is temporary fix for user-requests endpoint (also import)
|
|
||||||
const headers = { authorization: storage.token }
|
const headers = { authorization: storage.token }
|
||||||
|
|
||||||
return axios.get(url.href, { headers: headers })
|
return fetch(url.href, { headers: headers })
|
||||||
.catch(error => { console.error(`api error getting list: ${listPath}, page: ${page}`); throw error })
|
.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('query', query)
|
||||||
url.searchParams.append('page', page)
|
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 })
|
.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
|
* @returns {object} Torrent response
|
||||||
*/
|
*/
|
||||||
const searchTorrents = (query, authorization_token) => {
|
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)
|
url.searchParams.append('query', query)
|
||||||
|
|
||||||
const headers = { authorization: storage.token }
|
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 })
|
.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 }
|
const headers = { authorization: storage.token }
|
||||||
|
|
||||||
return axios.post(url.href, body, { headers: headers })
|
return fetch(url.href, { method: 'POST', headers, body })
|
||||||
.catch(error => { console.error(`api error adding magnet: ${name}`); throw error })
|
.then(resp => resp.json())
|
||||||
|
.catch(error => { console.error(`api error adding magnet: ${name} ${error}`); throw error })
|
||||||
}
|
}
|
||||||
|
|
||||||
// - - - Plex/Request - - -
|
// - - - Plex/Request - - -
|
||||||
@@ -175,9 +206,7 @@ const getRequestStatus = (id, type, authorization_token=undefined) => {
|
|||||||
// - - - Authenticate with plex - - -
|
// - - - Authenticate with plex - - -
|
||||||
|
|
||||||
const plexAuthenticate = (username, password) => {
|
const plexAuthenticate = (username, password) => {
|
||||||
const url = new URL('https://plex.tv/users/sign_in.json')
|
const url = new URL('https://plex.tv/api/v2/users/signin')
|
||||||
url.searchParams.append('user[login]', username)
|
|
||||||
url.searchParams.append('user[password]', password)
|
|
||||||
|
|
||||||
const headers = {
|
const headers = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
@@ -188,7 +217,17 @@ const plexAuthenticate = (username, password) => {
|
|||||||
'X-Plex-Client-Identifier': '123'
|
'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 })
|
.catch(error => { console.error(`api error authentication plex: ${username}`); throw error })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,7 +237,8 @@ const plexAuthenticate = (username, password) => {
|
|||||||
const getEmoji = () => {
|
const getEmoji = () => {
|
||||||
const url = new URL('v1/emoji', SEASONED_URL)
|
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 })
|
.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
|
||||||
|
}
|
||||||
|
|||||||
@@ -146,8 +146,7 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
parseResponse(resp) {
|
parseResponse(movie) {
|
||||||
let movie = resp.data;
|
|
||||||
this.movie = { ...movie }
|
this.movie = { ...movie }
|
||||||
this.title = movie.title
|
this.title = movie.title
|
||||||
this.poster = movie.poster
|
this.poster = movie.poster
|
||||||
|
|||||||
@@ -54,9 +54,7 @@ export default {
|
|||||||
searchTmdb(query, page)
|
searchTmdb(query, page)
|
||||||
.then(this.parseResponse)
|
.then(this.parseResponse)
|
||||||
},
|
},
|
||||||
parseResponse(response) {
|
parseResponse(data) {
|
||||||
const data = response.data
|
|
||||||
|
|
||||||
if (this.results.length > 0) {
|
if (this.results.length > 0) {
|
||||||
this.results.push(...data.results)
|
this.results.push(...data.results)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ export default {
|
|||||||
|
|
||||||
elasticSearchMoviesAndShows(this.query)
|
elasticSearchMoviesAndShows(this.query)
|
||||||
.then(resp => {
|
.then(resp => {
|
||||||
const data = resp.data.hits.hits
|
const data = resp.hits.hits
|
||||||
|
|
||||||
this.elasticSearchResults = data.map(item => {
|
this.elasticSearchResults = data.map(item => {
|
||||||
const index = item._index.slice(0, -1)
|
const index = item._index.slice(0, -1)
|
||||||
|
|||||||
@@ -75,12 +75,15 @@ export default {
|
|||||||
let password = this.plexPassword
|
let password = this.plexPassword
|
||||||
|
|
||||||
plexAuthenticate(username, password)
|
plexAuthenticate(username, password)
|
||||||
.then((resp) => {
|
.then(resp => {
|
||||||
let data = resp.data;
|
const data = resp.data
|
||||||
this.messages.push({ type: 'success', title: 'Authenticated with plex', message: 'Successfully linked plex account with seasoned request' })
|
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 })
|
this.messages.push({ type: 'error', title: 'Something went wrong', message: error.message })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user