Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Damien BATTISTELLA committed Sep 12, 2019
1 parent 0f98663 commit cfcaccf
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 28 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
"homepage": "https://github.com/Wifsimster/pavie#readme",
"dependencies": {
"axios": "^0.19.0",
"uuid": "^3.3.2"
"uuid": "^3.3.3"
}
}
153 changes: 129 additions & 24 deletions pavie.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = class {
return this.user.authToken
}
console.warn(`You are not yet signin !`)
return null
return new Error(response)
}

/**
Expand Down Expand Up @@ -68,6 +68,8 @@ module.exports = class {

if (response.status < 400) {
return response.data
} else {
return new Error(response)
}
}

Expand All @@ -89,6 +91,8 @@ module.exports = class {

if (response.status < 400) {
return response.data
} else {
return new Error(response)
}
}

Expand All @@ -97,10 +101,14 @@ module.exports = class {
* Return: [machineIdentifier, version]
*/
async getIdentity() {
const response = await this.instance.get(`/identity`)
const response = await this.instance.get(`/identity`).catch(err => {
return new Error(err)
})

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

Expand All @@ -114,17 +122,23 @@ module.exports = class {

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

/**
* Refresh a section
*/
async refresh(library = "sections", sectionId = 2) {
const response = await this.instance.get(`/library/${library}/${sectionId}/refresh`)
const response = await this.instance.get(
`/library/${library}/${sectionId}/refresh`
)

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

Expand All @@ -133,43 +147,59 @@ module.exports = class {
* Return: [name, host, address, port, machineIdentifier, version]
*/
async getServers() {
const response = await this.instance.get("/servers")
const response = await this.instance.get("/servers").catch(err => {
return new Error(err)
})

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

/**
* Get synchronize info
*/
async getSynchronize() {
const response = await this.instance.get("/video/trakt/sync")
const response = await this.instance.get("/video/trakt/sync").catch(err => {
return new Error(err)
})

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

/**
* Synchronize Plex and Trakt.tv
*/
async synchronize(accoundId = "1&amp;t=1565171925.59") {
const response = await this.instance.get(`/video/trakt/sync/synchronize?account_id=${accoundId}`)
const response = await this.instance.get(
`/video/trakt/sync/synchronize?account_id=${accoundId}`
)

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

/**
* Get all playlists
*/
async getPlaylists() {
const response = await this.instance.get(`/playlists`)
const response = await this.instance.get(`/playlists`).catch(err => {
return new Error(err)
})

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

Expand All @@ -181,6 +211,8 @@ module.exports = class {

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

Expand All @@ -192,6 +224,8 @@ module.exports = class {

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

Expand All @@ -206,32 +240,44 @@ module.exports = class {
},
data
)
const response = await this.instance.post(`/playlists?${querystring.stringify(data)}`)
const response = await this.instance.post(
`/playlists?${querystring.stringify(data)}`
)

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

/**
* Update playlist
*/
async updatePlaylist(ratingKey, data) {
const response = await this.instance.put(`/playlists/${ratingKey}?${querystring.stringify(data)}`)
const response = await this.instance.put(
`/playlists/${ratingKey}?${querystring.stringify(data)}`
)

if (response.status < 400) {
return response.data
} else {
return new Error(response)
}
}

/**
* Update playlist files
*/
async updatePlaylistFiles(ratingKey, uri) {
const response = await this.instance.put(`/playlists/${ratingKey}/items?uri=${uri}`)
const response = await this.instance.put(
`/playlists/${ratingKey}/items?uri=${uri}`
)

if (response.status < 400) {
return response.data
} else {
return new Error(response)
}
}

Expand All @@ -243,6 +289,8 @@ module.exports = class {

if (response.status < 400) {
return response.data
} else {
return new Error(response)
}
}

Expand All @@ -251,21 +299,29 @@ module.exports = class {
*/
async getHistory(filters = {}) {
filters = Object.assign({ sort: "viewedAt:desc" }, filters)
const response = await this.instance.get(`/status/sessions/history/all?${querystring.stringify(filters)}`)
const response = await this.instance.get(
`/status/sessions/history/all?${querystring.stringify(filters)}`
)

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

/**
* Get metadata of a media
*/
async getMedatadata(id) {
const response = await this.instance.get(`/library/metadata/${id}?includePreferences=1`)
const response = await this.instance.get(
`/library/metadata/${id}?includePreferences=1`
)

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

Expand All @@ -275,19 +331,34 @@ module.exports = class {
*/
async getMedatadataChildren(id, options = {}) {
options = Object.assign({ excludeAllLeaves: 1 }, options)
const response = await this.instance.get(`/library/metadata/${id}/children?${querystring.stringify(options)}`)
const response = await this.instance.get(
`/library/metadata/${id}/children?${querystring.stringify(options)}`
)

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

async getRelated(id, options = {}) {
options = Object.assign({ excludeFields: "summary", includeExternalMetadata: 1, asyncAugmentMetadata: 1 }, options)
const response = await this.instance.get(`/hubs/metadata/${id}/related?${querystring.stringify(options)}`)
options = Object.assign(
{
excludeFields: "summary",
includeExternalMetadata: 1,
asyncAugmentMetadata: 1
},
options
)
const response = await this.instance.get(
`/hubs/metadata/${id}/related?${querystring.stringify(options)}`
)

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

Expand All @@ -296,6 +367,8 @@ module.exports = class {

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

Expand All @@ -315,49 +388,81 @@ module.exports = class {
options
)

const response = await this.instance.get(`/photo/:/transcode?${querystring.stringify(options)}`)
const response = await this.instance.get(
`/photo/:/transcode?${querystring.stringify(options)}`
)

if (response.status < 400) {
return response.data
} else {
return new Error(response)
}
}

/**
* Global search
*/
async search(query, options = {}) {
options = Object.assign({ includeCollections: 1, sectionId: null, limit: null, query: query }, options)
const response = await this.instance.get(`/hubs/search?${querystring.stringify(options)}`)
options = Object.assign(
{ includeCollections: 1, sectionId: null, limit: null, query: query },
options
)
const response = await this.instance.get(
`/hubs/search?${querystring.stringify(options)}`
)

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

async getTvShows(options = {}) {
options = Object.assign({ type: 2, unwatchedLeaves: 0, sort: null }, options)
const response = await this.instance.get(`/library/sections/2/all?${querystring.stringify(options)}`)
options = Object.assign(
{ type: 2, unwatchedLeaves: 0, sort: null },
options
)
const response = await this.instance.get(
`/library/sections/2/all?${querystring.stringify(options)}`
)

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

async getSeasons(options = {}) {
options = Object.assign({ type: 3, includeCollections: 1, sort: null }, options)
const response = await this.instance.get(`/library/sections/2/all?${querystring.stringify(options)}`)
options = Object.assign(
{ type: 3, includeCollections: 1, sort: null },
options
)
const response = await this.instance.get(
`/library/sections/2/all?${querystring.stringify(options)}`
)

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}

async getEpisodes(options = {}) {
options = Object.assign({ type: 4, includeCollections: 1, sort: null }, options)
const response = await this.instance.get(`/library/sections/2/all?${querystring.stringify(options)}`)
options = Object.assign(
{ type: 4, includeCollections: 1, sort: null },
options
)
const response = await this.instance.get(
`/library/sections/2/all?${querystring.stringify(options)}`
)

if (response.status < 400) {
return response.data.MediaContainer
} else {
return new Error(response)
}
}
}

0 comments on commit cfcaccf

Please sign in to comment.