@@ -16,7 +16,7 @@ class ProxyEventType(Enum):
16
16
api_gateway = http_api_v1
17
17
18
18
19
- class RouteEntry :
19
+ class Route :
20
20
def __init__ (
21
21
self , method : str , rule : Any , func : Callable , cors : bool , compress : bool , cache_control : Optional [str ]
22
22
):
@@ -34,7 +34,7 @@ class ApiGatewayResolver:
34
34
35
35
def __init__ (self , proxy_type : Enum = ProxyEventType .http_api_v1 ):
36
36
self ._proxy_type = proxy_type
37
- self ._routes : List [RouteEntry ] = []
37
+ self ._routes : List [Route ] = []
38
38
39
39
def get (self , rule : str , cors : bool = False , compress : bool = False , cache_control : str = None ):
40
40
return self .route (rule , "GET" , cors , compress , cache_control )
@@ -48,14 +48,25 @@ def put(self, rule: str, cors: bool = False, compress: bool = False, cache_contr
48
48
def delete (self , rule : str , cors : bool = False , compress : bool = False , cache_control : str = None ):
49
49
return self .route (rule , "DELETE" , cors , compress , cache_control )
50
50
51
+ def patch (self , rule : str , cors : bool = False , compress : bool = False , cache_control : str = None ):
52
+ return self .route (rule , "PATCH" , cors , compress , cache_control )
53
+
51
54
def route (self , rule : str , method : str , cors : bool = False , compress : bool = False , cache_control : str = None ):
52
55
def register_resolver (func : Callable ):
53
- self ._append (func , rule , method , cors , compress , cache_control )
56
+ self ._add (func , rule , method , cors , compress , cache_control )
54
57
return func
55
58
56
59
return register_resolver
57
60
58
- def resolve (self , event : Dict , context : LambdaContext ) -> Dict :
61
+ def _add (self , func : Callable , rule : str , method : str , cors : bool , compress : bool , cache_control : Optional [str ]):
62
+ self ._routes .append (Route (method , self ._build_rule_pattern (rule ), func , cors , compress , cache_control ))
63
+
64
+ @staticmethod
65
+ def _build_rule_pattern (rule : str ):
66
+ rule_regex : str = re .sub (r"(<\w+>)" , r"(?P\1.+)" , rule )
67
+ return re .compile ("^{}$" .format (rule_regex ))
68
+
69
+ def resolve (self , event , context ) -> Dict [str , Any ]:
59
70
self .current_event = self ._as_data_class (event )
60
71
self .lambda_context = context
61
72
@@ -88,22 +99,14 @@ def resolve(self, event: Dict, context: LambdaContext) -> Dict:
88
99
89
100
return {"statusCode" : status_code , "headers" : headers , "body" : body , "isBase64Encoded" : base64_encoded }
90
101
91
- def _append (self , func : Callable , rule : str , method : str , cors : bool , compress : bool , cache_control : Optional [str ]):
92
- self ._routes .append (RouteEntry (method , self ._build_rule_pattern (rule ), func , cors , compress , cache_control ))
93
-
94
- @staticmethod
95
- def _build_rule_pattern (rule : str ):
96
- rule_regex : str = re .sub (r"(<\w+>)" , r"(?P\1.+)" , rule )
97
- return re .compile ("^{}$" .format (rule_regex ))
98
-
99
102
def _as_data_class (self , event : Dict ) -> BaseProxyEvent :
100
103
if self ._proxy_type == ProxyEventType .http_api_v1 :
101
104
return APIGatewayProxyEvent (event )
102
105
if self ._proxy_type == ProxyEventType .http_api_v2 :
103
106
return APIGatewayProxyEventV2 (event )
104
107
return ALBEvent (event )
105
108
106
- def _find_route (self , method : str , path : str ) -> Tuple [RouteEntry , Dict ]:
109
+ def _find_route (self , method : str , path : str ) -> Tuple [Route , Dict ]:
107
110
method = method .upper ()
108
111
for route in self ._routes :
109
112
if method != route .method :
0 commit comments