chore(performance): Removes NetflixSession from the core addon & adds a HTTP proxy...
[plugin.video.netflix.git] / resources / lib / NetflixHttpRequestHandler.py
1 import BaseHTTPServer
2 import json
3 from types import FunctionType
4 from urlparse import urlparse, parse_qs
5 from resources.lib.KodiHelper import KodiHelper
6 from resources.lib.NetflixSession import NetflixSession
7 from resources.lib.NetflixHttpSubRessourceHandler import NetflixHttpSubRessourceHandler
8
9 kodi_helper = KodiHelper()
10
11 netflix_session = NetflixSession(
12     cookie_path=kodi_helper.cookie_path,
13     data_path=kodi_helper.data_path,
14     verify_ssl=kodi_helper.get_ssl_verification_setting(),
15     log_fn=kodi_helper.log
16 )
17
18 # get list of methods & instance form the sub ressource handler
19 methods = [x for x, y in NetflixHttpSubRessourceHandler.__dict__.items() if type(y) == FunctionType]
20 sub_res_handler = NetflixHttpSubRessourceHandler(kodi_helper=kodi_helper, netflix_session=netflix_session)
21
22 class NetflixHttpRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
23
24     def do_GET(self):
25         url = urlparse(self.path)
26         params = parse_qs(url.query)
27         method = params.get('method', [None])[0]
28
29         # not method given
30         if method == None:
31             self.send_error(500, 'No method declared')
32             return
33
34         # no existing method given
35         if method not in methods:
36             self.send_error(404, 'Method "' + str(method) + '" not found. Available methods: ' + str(methods))
37             return
38
39         # call method & get the result
40         result = getattr(sub_res_handler, method)(params)
41         self.send_response(200)
42         self.send_header('Content-type', 'application/json')
43         self.end_headers()
44         self.wfile.write(json.dumps({'method': method, 'result': result}));
45         return
46
47     def log_message(self, format, *args):
48         """Disable the BaseHTTPServer Log"""
49         return