-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
request_handler.py
88 lines (65 loc) · 2.85 KB
/
request_handler.py
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
78
79
80
81
82
83
84
85
86
87
88
#-----------------------------------------------------------------------------
# Copyright (c) 2012 - 2024, Anaconda, Inc., and Bokeh Contributors.
# All rights reserved.
#
# The full license is in the file LICENSE.txt, distributed with this software.
#-----------------------------------------------------------------------------
''' Bokeh Application Handler to look for Bokeh server request callbacks
in a specified Python module.
'''
#-----------------------------------------------------------------------------
# Boilerplate
#-----------------------------------------------------------------------------
from __future__ import annotations
import logging # isort:skip
log = logging.getLogger(__name__)
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
# Standard library imports
from typing import TYPE_CHECKING, Any, Callable
# Bokeh imports
from .handler import Handler
if TYPE_CHECKING:
from tornado.httputil import HTTPServerRequest
#-----------------------------------------------------------------------------
# Globals and constants
#-----------------------------------------------------------------------------
__all__ = (
'RequestHandler',
)
#-----------------------------------------------------------------------------
# General API
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Dev API
#-----------------------------------------------------------------------------
class RequestHandler(Handler):
''' Load a script which contains server request handler callbacks.
'''
_process_request: Callable[[HTTPServerRequest], dict[str, Any]]
def __init__(self) -> None:
super().__init__()
self._process_request = _return_empty
# Public methods ----------------------------------------------------------
def process_request(self, request: HTTPServerRequest) -> dict[str, Any]:
''' Processes incoming HTTP request returning a dictionary of
additional data to add to the session_context.
Args:
request: HTTP request
Returns:
A dictionary of JSON serializable data to be included on
the session context.
'''
return self._process_request(request)
@property
def safe_to_fork(self) -> bool:
return True
#-----------------------------------------------------------------------------
# Private API
#-----------------------------------------------------------------------------
def _return_empty(request: HTTPServerRequest) -> dict[str, Any]:
return {}
#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------