1
1
# -*- coding: utf-8 -*-
2
2
3
+ from typing import Any , Callable , Dict
4
+
3
5
import json
4
6
from logging import getLogger
5
7
import re
6
8
9
+ from chalice import Chalice
7
10
from chalice .config import Config
8
11
from chalice .local import LocalGateway
9
12
16
19
17
20
class InternalLocalGateway (LocalGateway ):
18
21
def __init__ (self , * args , ** kwargs ):
22
+ # type: (Any, Any) -> None
19
23
self .custom_context = {}
20
24
super (InternalLocalGateway , self ).__init__ (* args , ** kwargs )
21
25
22
26
@property
23
27
def custom_context (self ):
28
+ # type: () -> Dict[str, Any]
24
29
return self .__custom_context
25
30
26
31
@custom_context .setter
27
32
def custom_context (self , context ):
33
+ # type: (Dict[str, Any]) -> None
28
34
self .__custom_context = context
29
35
30
36
def _generate_lambda_event (self , * args , ** kwargs ):
37
+ # type: (Any, Any) -> Dict[str, Any]
31
38
event = super (InternalLocalGateway , self )._generate_lambda_event (* args , ** kwargs )
32
39
event ['requestContext' ].update (self .custom_context )
33
40
return event
@@ -38,6 +45,7 @@ def _generate_lambda_event(self, *args, **kwargs):
38
45
39
46
class ResponseHandler (object ):
40
47
def __init__ (self , values ):
48
+ # type: (Dict[str, Any]) -> None
41
49
self .values = {}
42
50
43
51
for key , value in values .items ():
@@ -46,10 +54,11 @@ def __init__(self, values):
46
54
47
55
try :
48
56
self .values ['json' ] = json .loads (self .values ['body' ])
49
- except JSONDecodeError :
57
+ except JSONDecodeError : # type: ignore
50
58
logger .info ('Response body is NOT JSON decodable: {}' .format (self .values ['body' ]))
51
59
52
60
def __getattr__ (self , key ):
61
+ # type: (str) -> Any
53
62
try :
54
63
return self .values [key ]
55
64
except KeyError :
@@ -60,10 +69,12 @@ class RequestHandler(object):
60
69
METHODS = ('get' , 'head' , 'post' , 'options' , 'put' , 'delete' , 'trace' , 'patch' , 'link' , 'unlink' )
61
70
62
71
def __init__ (self , app ):
72
+ # type: (Chalice) -> None
63
73
self .local_gateway = InternalLocalGateway (app , Config ())
64
74
65
75
@property
66
76
def custom_context (self ):
77
+ # type: () -> Dict[str, Any]
67
78
return self .local_gateway .custom_context
68
79
69
80
# As of Chalice version 1.8.0,
@@ -75,13 +86,16 @@ def custom_context(self):
75
86
# Not only for this purpose, it's an interface provided to allow custom contexts in unit tests.
76
87
@custom_context .setter
77
88
def custom_context (self , context ):
89
+ # type: (Dict[str, Any]) -> None
78
90
self .local_gateway .custom_context = context
79
91
80
92
def __getattr__ (self , method ):
93
+ # type: (str) -> Callable
81
94
if method not in self .METHODS :
82
95
raise AttributeError ("'{}' object has no attribute '{}'" .format (self .__class__ .__name__ , method ))
83
96
84
97
def request (path , headers = {}, body = '' ):
98
+ # type: (str, Dict[str, str], str) -> ResponseHandler
85
99
response = self .local_gateway .handle_request (method = method .upper (), path = path , headers = headers , body = body )
86
100
return ResponseHandler (response )
87
101
0 commit comments