Commit 7635d838 authored by echel0n's avatar echel0n
Browse files

Refactored web handlers to return data and call tornado finish on resp from run_async method

Merged base handler render_string and render methods into render method
Refactored base handlers for API v1 and v2
Fixed redirect issues for auth handlers
parent 98396dc7
......@@ -86,8 +86,6 @@ class ApiV1BaseHandler(RequestHandler):
super(ApiV1BaseHandler, self).__init__(application, request, **kwargs)
self.executor = ThreadPoolExecutor(thread_name_prefix='APIv1-Thread')
class ApiV1Handler(ApiV1BaseHandler):
def prepare(self, *args, **kwargs):
# set the output callback
# default json
......@@ -127,8 +125,7 @@ class ApiV1Handler(ApiV1BaseHandler):
if 'outputType' in out_dict:
output_callback = output_callback_dict[out_dict['outputType']]
method = self.run_async(output_callback)
method(out_dict)
self.finish(output_callback(out_dict))
def run_async(self, method):
@functools.wraps(method)
......@@ -168,7 +165,7 @@ class ApiV1Handler(ApiV1BaseHandler):
:return: api calls
:rtype: Union[dict, object]
"""
return dict((cls._cmd, cls) for cls in ApiCall.__subclasses__() if '_cmd' in cls.__dict__)
return dict((cls._cmd, cls) for cls in ApiV1Handler.__subclasses__() if '_cmd' in cls.__dict__)
def call_dispatcher(self, *args, **kwargs):
""" calls the appropriate CMD class
......@@ -274,11 +271,11 @@ class ApiV1Handler(ApiV1BaseHandler):
return curArgs, curKwargs
class ApiCall(ApiV1Handler):
class ApiV1Handler(ApiV1BaseHandler):
_help = {"desc": "This command is not documented. Please report this to the developers."}
def __init__(self, application, request, *args, **kwargs):
super(ApiCall, self).__init__(application, request)
super(ApiV1Handler, self).__init__(application, request)
self._missing = []
self._requiredParams = {}
self._optionalParams = {}
......@@ -435,7 +432,7 @@ class ApiCall(ApiV1Handler):
value) + "' is out of allowed range '" + str(allowed_values) + "'")
class TVDBShorthandWrapper(ApiCall):
class TVDBShorthandWrapper(ApiV1Handler):
_help = {"desc": "This is an internal function wrapper. Call the help command directly for more information."}
def __init__(self, sid, application, request, *args, **kwargs):
......@@ -592,7 +589,7 @@ class IntParseError(Exception):
"""
class CMD_Help(ApiCall):
class CMD_Help(ApiV1Handler):
_cmd = "help"
_help = {
"desc": "Get help about a given command",
......@@ -616,7 +613,7 @@ class CMD_Help(ApiCall):
return out
class CMD_Backup(ApiCall):
class CMD_Backup(ApiV1Handler):
_cmd = "backup"
_help = {
"desc": "Backup application data files",
......@@ -640,7 +637,7 @@ class CMD_Backup(ApiCall):
return response
class CMD_ComingEpisodes(ApiCall):
class CMD_ComingEpisodes(ApiV1Handler):
_cmd = "future"
_help = {
"desc": "Get the coming episodes",
......@@ -688,7 +685,7 @@ class CMD_ComingEpisodes(ApiCall):
return _responds(RESULT_SUCCESS, data)
class CMD_Episode(ApiCall):
class CMD_Episode(ApiV1Handler):
_cmd = "episode"
_help = {
"desc": "Get detailed information about an episode",
......@@ -757,7 +754,7 @@ class CMD_Episode(ApiCall):
raise InternalApiError("Episode not found")
class CMD_EpisodeSearch(ApiCall):
class CMD_EpisodeSearch(ApiV1Handler):
_cmd = "episode.search"
_help = {
"desc": "Search for an episode. The response might take some time.",
......@@ -807,7 +804,7 @@ class CMD_EpisodeSearch(ApiCall):
return _responds(RESULT_FAILURE, msg='Unable to find episode')
class CMD_EpisodeSetStatus(ApiCall):
class CMD_EpisodeSetStatus(ApiV1Handler):
_cmd = "episode.setstatus"
_help = {
"desc": "Set the status of an episode or a season (when no episode is provided)",
......@@ -901,7 +898,7 @@ class CMD_EpisodeSetStatus(ApiCall):
return _responds(RESULT_SUCCESS, msg='All status set successfully.' + extra_msg)
class CMD_SubtitleSearch(ApiCall):
class CMD_SubtitleSearch(ApiV1Handler):
_cmd = "episode.subtitlesearch"
_help = {
"desc": "Search for an episode subtitles. The response might take some time.",
......@@ -958,7 +955,7 @@ class CMD_SubtitleSearch(ApiCall):
return response
class CMD_Exceptions(ApiCall):
class CMD_Exceptions(ApiV1Handler):
_cmd = "exceptions"
_help = {
"desc": "Get the scene exceptions for all or a given show",
......@@ -993,7 +990,7 @@ class CMD_Exceptions(ApiCall):
return _responds(RESULT_SUCCESS, scene_exceptions)
class CMD_History(ApiCall):
class CMD_History(ApiV1Handler):
_cmd = "history"
_help = {
"desc": "Get the downloaded and/or snatched history",
......@@ -1037,7 +1034,7 @@ class CMD_History(ApiCall):
return _responds(RESULT_SUCCESS, results)
class CMD_HistoryClear(ApiCall):
class CMD_HistoryClear(ApiV1Handler):
_cmd = "history.clear"
_help = {"desc": "Clear the entire history"}
......@@ -1051,7 +1048,7 @@ class CMD_HistoryClear(ApiCall):
return _responds(RESULT_SUCCESS, msg="History cleared")
class CMD_HistoryTrim(ApiCall):
class CMD_HistoryTrim(ApiV1Handler):
_cmd = "history.trim"
_help = {"desc": "Trim history entries older than 30 days"}
......@@ -1065,7 +1062,7 @@ class CMD_HistoryTrim(ApiCall):
return _responds(RESULT_SUCCESS, msg='Removed history entries older than 30 days')
class CMD_Failed(ApiCall):
class CMD_Failed(ApiV1Handler):
_cmd = "failed"
_help = {
"desc": "Get the failed downloads",
......@@ -1092,7 +1089,7 @@ class CMD_Failed(ApiCall):
return _responds(RESULT_SUCCESS, dbData)
class CMD_Backlog(ApiCall):
class CMD_Backlog(ApiV1Handler):
_cmd = "backlog"
_help = {"desc": "Get the backlogged episodes"}
......@@ -1126,7 +1123,7 @@ class CMD_Backlog(ApiCall):
return _responds(RESULT_SUCCESS, shows)
class CMD_Logs(ApiCall):
class CMD_Logs(ApiV1Handler):
_cmd = "logs"
_help = {
"desc": "Get the logs",
......@@ -1175,7 +1172,7 @@ class CMD_Logs(ApiCall):
return _responds(RESULT_SUCCESS, "\n".join(logRegex.findall("\n".join(data))))
class CMD_PostProcess(ApiCall):
class CMD_PostProcess(ApiV1Handler):
_cmd = "postprocess"
_help = {
"desc": "Manually post-process the files in the download folder",
......@@ -1226,7 +1223,7 @@ class CMD_PostProcess(ApiCall):
return _responds(RESULT_SUCCESS, data=data, msg="Started postprocess for {}".format(self.path))
class CMD_SiCKRAGE(ApiCall):
class CMD_SiCKRAGE(ApiV1Handler):
_cmd = "sr"
_help = {"desc": "Get miscellaneous information about SiCKRAGE"}
......@@ -1240,7 +1237,7 @@ class CMD_SiCKRAGE(ApiCall):
return _responds(RESULT_SUCCESS, data)
class CMD_SiCKRAGEAddRootDir(ApiCall):
class CMD_SiCKRAGEAddRootDir(ApiV1Handler):
_cmd = "sr.addrootdir"
_help = {
"desc": "Add a new root (parent) directory to SiCKRAGE",
......@@ -1302,7 +1299,7 @@ class CMD_SiCKRAGEAddRootDir(ApiCall):
return _responds(RESULT_SUCCESS, _get_root_dirs(), msg="Root directories updated")
class CMD_SiCKRAGECheckVersion(ApiCall):
class CMD_SiCKRAGECheckVersion(ApiV1Handler):
_cmd = "sr.checkversion"
_help = {"desc": "Check if a new version of SiCKRAGE is available"}
......@@ -1321,7 +1318,7 @@ class CMD_SiCKRAGECheckVersion(ApiCall):
})
class CMD_SiCKRAGECheckScheduler(ApiCall):
class CMD_SiCKRAGECheckScheduler(ApiV1Handler):
_cmd = "sr.checkscheduler"
_help = {"desc": "Get information about the scheduler"}
......@@ -1340,7 +1337,7 @@ class CMD_SiCKRAGECheckScheduler(ApiCall):
return _responds(RESULT_SUCCESS, data)
class CMD_SiCKRAGEDeleteRootDir(ApiCall):
class CMD_SiCKRAGEDeleteRootDir(ApiV1Handler):
_cmd = "sr.deleterootdir"
_help = {"desc": "Delete a root (parent) directory from SiCKRAGE", "requiredParameters": {
"location": {"desc": "The full path to the root (parent) directory to remove"},
......@@ -1384,7 +1381,7 @@ class CMD_SiCKRAGEDeleteRootDir(ApiCall):
return _responds(RESULT_SUCCESS, _get_root_dirs(), msg="Root directory deleted")
class CMD_SiCKRAGEGetDefaults(ApiCall):
class CMD_SiCKRAGEGetDefaults(ApiV1Handler):
_cmd = "sr.getdefaults"
_help = {"desc": "Get SiCKRAGE's user default configuration value"}
......@@ -1402,7 +1399,7 @@ class CMD_SiCKRAGEGetDefaults(ApiCall):
return _responds(RESULT_SUCCESS, data)
class CMD_SiCKRAGEGetMessages(ApiCall):
class CMD_SiCKRAGEGetMessages(ApiV1Handler):
_cmd = "sr.getmessages"
_help = {"desc": "Get all messages"}
......@@ -1418,7 +1415,7 @@ class CMD_SiCKRAGEGetMessages(ApiCall):
return _responds(RESULT_SUCCESS, messages)
class CMD_SiCKRAGEGetRootDirs(ApiCall):
class CMD_SiCKRAGEGetRootDirs(ApiV1Handler):
_cmd = "sr.getrootdirs"
_help = {"desc": "Get all root (parent) directories"}
......@@ -1431,7 +1428,7 @@ class CMD_SiCKRAGEGetRootDirs(ApiCall):
return _responds(RESULT_SUCCESS, _get_root_dirs())
class CMD_SiCKRAGEPauseDaily(ApiCall):
class CMD_SiCKRAGEPauseDaily(ApiV1Handler):
_cmd = "sr.pausedaily"
_help = {
"desc": "Pause or unpause the daily search",
......@@ -1454,7 +1451,7 @@ class CMD_SiCKRAGEPauseDaily(ApiCall):
return _responds(RESULT_SUCCESS, msg="Daily searcher unpaused")
class CMD_SiCKRAGEPauseBacklog(ApiCall):
class CMD_SiCKRAGEPauseBacklog(ApiV1Handler):
_cmd = "sr.pausebacklog"
_help = {
"desc": "Pause or unpause the backlog search",
......@@ -1477,7 +1474,7 @@ class CMD_SiCKRAGEPauseBacklog(ApiCall):
return _responds(RESULT_SUCCESS, msg="Backlog searcher unpaused")
class CMD_SiCKRAGEPing(ApiCall):
class CMD_SiCKRAGEPing(ApiV1Handler):
_cmd = "sr.ping"
_help = {"desc": "Ping SiCKRAGE to check if it is running"}
......@@ -1492,7 +1489,7 @@ class CMD_SiCKRAGEPing(ApiCall):
return _responds(RESULT_SUCCESS, msg="Pong")
class CMD_SiCKRAGERestart(ApiCall):
class CMD_SiCKRAGERestart(ApiV1Handler):
_cmd = "sr.restart"
_help = {"desc": "Restart SiCKRAGE"}
......@@ -1505,7 +1502,7 @@ class CMD_SiCKRAGERestart(ApiCall):
return _responds(RESULT_SUCCESS, msg="SiCKRAGE is restarting...")
class CMD_SiCKRAGESearchSeriesProvider(ApiCall):
class CMD_SiCKRAGESearchSeriesProvider(ApiV1Handler):
_cmd = "sr.searchindexers"
_help = {
"desc": "Search for a show with a given name on all the indexers, in a specific language",
......@@ -1609,7 +1606,7 @@ class CMD_SiCKRAGESearchTVRAGE(CMD_SiCKRAGESearchSeriesProvider):
return _responds(RESULT_FAILURE, msg="TVRage is disabled, invalid result")
class CMD_SiCKRAGESetDefaults(ApiCall):
class CMD_SiCKRAGESetDefaults(ApiV1Handler):
_cmd = "sr.setdefaults"
_help = {
"desc": "Set SiCKRAGE's user default configuration value",
......@@ -1666,7 +1663,7 @@ class CMD_SiCKRAGESetDefaults(ApiCall):
return _responds(RESULT_SUCCESS, msg="Saved defaults")
class CMD_SiCKRAGEShutdown(ApiCall):
class CMD_SiCKRAGEShutdown(ApiV1Handler):
_cmd = "sr.shutdown"
_help = {"desc": "Shutdown SiCKRAGE"}
......@@ -1681,7 +1678,7 @@ class CMD_SiCKRAGEShutdown(ApiCall):
return _responds(RESULT_FAILURE, msg='SiCKRAGE can not be shut down')
class CMD_SiCKRAGEUpdate(ApiCall):
class CMD_SiCKRAGEUpdate(ApiV1Handler):
_cmd = "sr.update"
_help = {"desc": "Update SiCKRAGE to the latest version available"}
......@@ -1696,7 +1693,7 @@ class CMD_SiCKRAGEUpdate(ApiCall):
return _responds(RESULT_FAILURE, msg="SiCKRAGE is already up to date")
class CMD_Show(ApiCall):
class CMD_Show(ApiV1Handler):
_cmd = "show"
_help = {
"desc": "Get detailed information about a show",
......@@ -1784,7 +1781,7 @@ class CMD_Show(ApiCall):
return _responds(RESULT_SUCCESS, showDict)
class CMD_ShowAddExisting(ApiCall):
class CMD_ShowAddExisting(ApiV1Handler):
_cmd = "show.addexisting"
_help = {
"desc": "Add an existing show in SiCKRAGE",
......@@ -1869,7 +1866,7 @@ class CMD_ShowAddExisting(ApiCall):
return _responds(RESULT_SUCCESS, {"name": series_name}, series_name + " has been queued to be added")
class CMD_ShowAddNew(ApiCall):
class CMD_ShowAddNew(ApiV1Handler):
_cmd = "show.addnew"
_help = {
"desc": "Add a new show to SiCKRAGE",
......@@ -2015,7 +2012,7 @@ class CMD_ShowAddNew(ApiCall):
return _responds(RESULT_SUCCESS, {"name": series_name}, series_name + " has been queued to be added")
class CMD_ShowCache(ApiCall):
class CMD_ShowCache(ApiV1Handler):
_cmd = "show.cache"
_help = {
"desc": "Check SiCKRAGE's cache to see if the images (poster, banner, fanart) for a show are valid",
......@@ -2055,7 +2052,7 @@ class CMD_ShowCache(ApiCall):
return _responds(RESULT_SUCCESS, {"poster": has_poster, "banner": has_banner})
class CMD_ShowDelete(ApiCall):
class CMD_ShowDelete(ApiV1Handler):
_cmd = "delete"
_help = {
"desc": "Delete a show in SiCKRAGE",
......@@ -2091,7 +2088,7 @@ class CMD_ShowDelete(ApiCall):
return _responds(RESULT_SUCCESS, msg='%s has been queued to be deleted' % show_object.name)
class CMD_ShowGetQuality(ApiCall):
class CMD_ShowGetQuality(ApiV1Handler):
_cmd = "show.getquality"
_help = {
"desc": "Get the quality setting of a show",
......@@ -2120,7 +2117,7 @@ class CMD_ShowGetQuality(ApiCall):
return _responds(RESULT_SUCCESS, {"initial": anyQualities, "archive": bestQualities})
class CMD_ShowGetPoster(ApiCall):
class CMD_ShowGetPoster(ApiV1Handler):
_cmd = "show.getposter"
_help = {
"desc": "Get the poster of a show",
......@@ -2146,7 +2143,7 @@ class CMD_ShowGetPoster(ApiCall):
}
class CMD_ShowGetBanner(ApiCall):
class CMD_ShowGetBanner(ApiV1Handler):
_cmd = "show.getbanner"
_help = {
"desc": "Get the banner of a show",
......@@ -2172,7 +2169,7 @@ class CMD_ShowGetBanner(ApiCall):
}
class CMD_ShowGetNetworkLogo(ApiCall):
class CMD_ShowGetNetworkLogo(ApiV1Handler):
_cmd = "show.getnetworklogo"
_help = {
"desc": "Get the network logo of a show",
......@@ -2200,7 +2197,7 @@ class CMD_ShowGetNetworkLogo(ApiCall):
}
class CMD_ShowGetFanArt(ApiCall):
class CMD_ShowGetFanArt(ApiV1Handler):
_cmd = "show.getfanart"
_help = {
"desc": "Get the fan art of a show",
......@@ -2226,7 +2223,7 @@ class CMD_ShowGetFanArt(ApiCall):
}
class CMD_ShowPause(ApiCall):
class CMD_ShowPause(ApiV1Handler):
_cmd = "show.pause"
_help = {
"desc": "Pause or unpause a show",
......@@ -2262,7 +2259,7 @@ class CMD_ShowPause(ApiCall):
return _responds(RESULT_SUCCESS, msg='%s has been %s' % (show_object.name, ('resumed', 'paused')[show_object.paused]))
class CMD_ShowRefresh(ApiCall):
class CMD_ShowRefresh(ApiV1Handler):
_cmd = "show.refresh"
_help = {
"desc": "Refresh a show in SiCKRAGE",
......@@ -2294,7 +2291,7 @@ class CMD_ShowRefresh(ApiCall):
return _responds(RESULT_SUCCESS, msg='%s has queued to be refreshed' % show_object.name)
class CMD_ShowSeasonList(ApiCall):
class CMD_ShowSeasonList(ApiV1Handler):
_cmd = "show.seasonlist"
_help = {
"desc": "Get the list of seasons of a show",
......@@ -2327,7 +2324,7 @@ class CMD_ShowSeasonList(ApiCall):
return _responds(RESULT_SUCCESS, sorted(list(season_list), reverse=self.sort == 'desc'))
class CMD_ShowSeasons(ApiCall):
class CMD_ShowSeasons(ApiV1Handler):
_cmd = "show.seasons"
_help = {
"desc": "Get the list of episodes for one or all seasons of a show",
......@@ -2388,7 +2385,7 @@ class CMD_ShowSeasons(ApiCall):
return _responds(RESULT_SUCCESS, seasons)
class CMD_ShowSetQuality(ApiCall):
class CMD_ShowSetQuality(ApiV1Handler):
_cmd = "show.setquality"
_help = {
"desc": "Set the quality setting of a show. If no quality is provided, the default user setting is used.",
......@@ -2438,7 +2435,7 @@ class CMD_ShowSetQuality(ApiCall):
return _responds(RESULT_SUCCESS, msg=show_object.name + " quality has been changed to " + Qualities(show_object.quality).display_name)
class CMD_ShowStats(ApiCall):
class CMD_ShowStats(ApiV1Handler):
_cmd = "show.stats"
_help = {
"desc": "Get episode statistics for a given show",
......@@ -2550,7 +2547,7 @@ class CMD_ShowStats(ApiCall):
return _responds(RESULT_SUCCESS, episodes_stats)
class CMD_ShowUpdate(ApiCall):
class CMD_ShowUpdate(ApiV1Handler):
_cmd = "show.update"
_help = {
"desc": "Update a show in SiCKRAGE",
......@@ -2582,7 +2579,7 @@ class CMD_ShowUpdate(ApiCall):
return _responds(RESULT_FAILURE, msg="Unable to update " + str(show_object.name))
class CMD_Shows(ApiCall):
class CMD_Shows(ApiV1Handler):
_cmd = "shows"
_help = {
"desc": "Get all shows in SiCKRAGE",
......@@ -2637,7 +2634,7 @@ class CMD_Shows(ApiCall):
return _responds(RESULT_SUCCESS, shows)
class CMD_ShowsStats(ApiCall):
class CMD_ShowsStats(ApiV1Handler):
_cmd = "shows.stats"
_help = {"desc": "Get the global shows and episodes statistics"}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment