From d9aa5d8daef223628992387e35579fa42a197ff2 Mon Sep 17 00:00:00 2001 From: "mike.rode" Date: Wed, 13 Feb 2019 17:37:59 -0600 Subject: [PATCH] Refactor mostWatched query to accept query object instead of individual arguments. --- server/services/plexApi/index.js | 3 +-- server/services/plexApi/plexApi.js | 12 +++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/server/services/plexApi/index.js b/server/services/plexApi/index.js index 6ed402e..120b23d 100644 --- a/server/services/plexApi/index.js +++ b/server/services/plexApi/index.js @@ -8,8 +8,7 @@ const getUsers = async (req, res) => { const getMostWatched = async (req, res) => { const plexApi = plexApiClient(); - console.log('url---', req.query); - const mostWatched = await plexApi.getMostWatched(req.query.type); + const mostWatched = await plexApi.getMostWatched(req.query); res.json(mostWatched); }; diff --git a/server/services/plexApi/plexApi.js b/server/services/plexApi/plexApi.js index 76cb377..6a68250 100644 --- a/server/services/plexApi/plexApi.js +++ b/server/services/plexApi/plexApi.js @@ -31,13 +31,14 @@ PlexApiClient.prototype.getSectionsUrlParams = function() { }; }; -PlexApiClient.prototype.mostWatchedUrlParams = function(type, limit = 10) { +PlexApiClient.prototype.mostWatchedUrlParams = function(query) { return { host: config.plex.plexServerUrl, path: '/library/all/top', queryParams: { - type, - limit, + ...(query.accountID && {accountID: query.accountID}), + ...(query.type && {type: query.type}), + ...((query.limit && {limit: query.limit}) || {limit: 10}), 'X-Plex-Token': this.options.token || config.plex.token, }, }; @@ -52,6 +53,7 @@ PlexApiClient.prototype.buildUrl = function(urlParams) { return buildUrl(host, urlHash); }; +// Move to helper function const formatResponse = response => { const xmlResponse = response.headers['content-type'].includes('xml'); if (xmlResponse) { @@ -97,8 +99,8 @@ PlexApiClient.prototype.getUsers = async function() { return response.MediaContainer.User; }; -PlexApiClient.prototype.getMostWatched = async function(type, limit = 10) { - const urlParams = this.mostWatchedUrlParams(type, limit); +PlexApiClient.prototype.getMostWatched = async function(query) { + const urlParams = this.mostWatchedUrlParams(query); const mostWatchedUrl = this.buildUrl(urlParams); const response = await this.request(mostWatchedUrl); return response.MediaContainer.Metadata;