kapsikkum-unmanic – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3  
4 """
5 unmanic.api_request_router.py
6  
7 Written by: Josh.5 <jsunnex@gmail.com>
8 Date: 25 Oct 2020, (8:26 PM)
9  
10 Copyright:
11 Copyright (C) Josh Sunnex - All Rights Reserved
12  
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19  
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22  
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
27 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
28 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
29 OR OTHER DEALINGS IN THE SOFTWARE.
30  
31 """
32  
33 import importlib
34 import tornado.routing
35 import tornado.web
36 import tornado.log
37  
38 from unmanic import config
39  
40  
41 class Handle404(tornado.web.RequestHandler):
42 def get(self):
43 self.set_status(404)
44 self.write('404 Not Found')
45  
46  
47 class APIRequestRouter(tornado.routing.Router):
48 app = None
49 config = None
50  
51 def __init__(self, app, **kwargs):
52 self.app = app
53 self.config = config.Config()
54  
55 def find_handler(self, request, **kwargs):
56 api_version = request.path.split('/')[3] # Set API version
57 endpoint = request.path.split('/')[4] # Set the endpoint
58 params = list(filter(None, request.path.split('/')[4:])) # Set the request params
59  
60 endpoint_handler = 'Api%sHandler' % endpoint.title()
61  
62 # Check if the handler exists - Otherwise set it to 404
63 try:
64 # Fetch handler class from api module matching api version
65 handler = getattr(importlib.import_module("unmanic.webserver.api_{}".format(api_version)), endpoint_handler)
66 except KeyError:
67 tornado.log.app_log.warning("Unable to find handler for path: {}".format(endpoint_handler), exc_info=True)
68 handler = Handle404
69  
70 # Return handler
71 return self.app.get_handler_delegate(request, handler,
72 target_kwargs=dict(
73 params=params,
74 ),
75 path_args=[request.path])