|
16 | 16 | from six.moves.BaseHTTPServer import HTTPServer
|
17 | 17 | from six.moves.BaseHTTPServer import BaseHTTPRequestHandler
|
18 | 18 | from six.moves.socketserver import ThreadingMixIn
|
19 |
| -from typing import List, Any, Dict, Tuple, Callable, Optional, Union # noqa |
| 19 | +from typing import ( |
| 20 | + List, |
| 21 | + Any, |
| 22 | + Dict, |
| 23 | + Tuple, |
| 24 | + Callable, |
| 25 | + Optional, |
| 26 | + Union, |
| 27 | + cast, |
| 28 | +) # noqa |
20 | 29 |
|
21 | 30 | from chalice.app import Chalice # noqa
|
22 | 31 | from chalice.app import CORSConfig # noqa
|
@@ -47,7 +56,9 @@ def time(self):
|
47 | 56 |
|
48 | 57 | def create_local_server(app_obj, config, host, port):
|
49 | 58 | # type: (Chalice, Config, str, int) -> LocalDevServer
|
50 |
| - return LocalDevServer(app_obj, config, host, port) |
| 59 | + local_app_obj = LocalChalice(app_obj) |
| 60 | + casted_local_app_obj = cast(Chalice, local_app_obj) |
| 61 | + return LocalDevServer(casted_local_app_obj, config, host, port) |
51 | 62 |
|
52 | 63 |
|
53 | 64 | class LocalARNBuilder(object):
|
@@ -661,3 +672,32 @@ def shutdown(self):
|
661 | 672 | # type: () -> None
|
662 | 673 | if self._server is not None:
|
663 | 674 | self._server.shutdown()
|
| 675 | + |
| 676 | + |
| 677 | +class LocalChalice(object): |
| 678 | + def __init__(self, chalice): |
| 679 | + # type: (Chalice) -> None |
| 680 | + self._current_request_lookup = {} # type: Dict[int, Optional[Request]] |
| 681 | + self._chalice = chalice |
| 682 | + |
| 683 | + @property |
| 684 | + def current_request(self): # noqa |
| 685 | + # type: () -> Optional[Request] |
| 686 | + thread_id = threading.current_thread().ident |
| 687 | + assert thread_id is not None |
| 688 | + return self._current_request_lookup.get(thread_id, None) |
| 689 | + |
| 690 | + @current_request.setter |
| 691 | + def current_request(self, value): # noqa |
| 692 | + # type: (Optional[Request]) -> None |
| 693 | + thread_id = threading.current_thread().ident |
| 694 | + assert thread_id is not None |
| 695 | + self._current_request_lookup[thread_id] = value |
| 696 | + |
| 697 | + def __getattr__(self, name): |
| 698 | + # type: (str) -> Any |
| 699 | + return getattr(self._chalice, name) |
| 700 | + |
| 701 | + def __call__(self, *args, **kwargs): |
| 702 | + # type: (Any, Any) -> Any |
| 703 | + return self._chalice(*args, **kwargs) |
0 commit comments