-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_handler_module.py
More file actions
77 lines (54 loc) · 2.66 KB
/
api_handler_module.py
File metadata and controls
77 lines (54 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from webapp.utils.session_handler import get_cookie_dict
from webapp.url_config import url_handler, url_strip
from webapp.utils.session_handler import check_validity_of_session_id
import json
def api_application(environ, start_response, status=None, response_headers=None):
html_response_body = ''
path = environ.get('PATH_INFO')
path = url_strip(path)
# url resolve
view, kwargs_to_views = url_handler(path)
start_response_headers: dict = {}
html_response_body, start_response_headers = view(environ, **kwargs_to_views)
status_basic = '200 OK'
status = start_response_headers.get('status', status_basic)
response_header_basic = [
('Content-type', 'application/json'),
('Content-length', str(len(html_response_body))),
]
response_headers = start_response_headers.get('response_headers', response_header_basic)
start_response(status, response_headers)
print(
"\n\n\n\n________________________________________ COMPLETED ONE REQUEST RESPONSE________________________________________________\n\n\n\n"
)
if type(html_response_body) == str:
html_response_body = html_response_body.encode('utf-8')
return [html_response_body]
def api_handler(path, environ, start_response):
# "API HANDLING"
wrapped_app = api_application
status = "200 OK"
response_headers = [
('Content-type', 'application/json'),
]
cookie_string = environ.get('HTTP_COOKIE')
SESSION_KEY_NAME = "session_key"
if path == "api/login" or path == "api/logout" or path.startswith('static/login'):
wrapped_app_response: list = wrapped_app(environ, start_response)
return iter(wrapped_app_response)
cookie_dict = get_cookie_dict(cookie_string)
session_key_value = cookie_dict.get(SESSION_KEY_NAME)
if session_key_value is None or check_validity_of_session_id(session_key_value) is False:
status = "401 Unauthorized"
response_body = {'message': 'Invalid Credentials', 'status': status}
response_body = json.dumps(response_body)
response_headers.append(('Content-length', str(len(response_body))))
start_response_headers: dict = {'status': status, 'response_headers': response_headers}
print(
"\n\n\n\n________________________________________ COMPLETED ONE REQUEST RESPONSE________________________________________________\n\n\n\n"
)
# start response should be called before returning anything to webserver
start_response(status, response_headers)
return iter([response_body.encode('utf-8')])
wrapped_app_response = wrapped_app(environ, start_response)
return iter(wrapped_app_response)