@@ -16,7 +16,7 @@ class ProxyEventType(Enum):
1616 api_gateway = http_api_v1
1717
1818
19- class RouteEntry :
19+ class Route :
2020 def __init__ (
2121 self , method : str , rule : Any , func : Callable , cors : bool , compress : bool , cache_control : Optional [str ]
2222 ):
@@ -34,7 +34,7 @@ class ApiGatewayResolver:
3434
3535 def __init__ (self , proxy_type : Enum = ProxyEventType .http_api_v1 ):
3636 self ._proxy_type = proxy_type
37- self ._routes : List [RouteEntry ] = []
37+ self ._routes : List [Route ] = []
3838
3939 def get (self , rule : str , cors : bool = False , compress : bool = False , cache_control : str = None ):
4040 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
4848 def delete (self , rule : str , cors : bool = False , compress : bool = False , cache_control : str = None ):
4949 return self .route (rule , "DELETE" , cors , compress , cache_control )
5050
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+
5154 def route (self , rule : str , method : str , cors : bool = False , compress : bool = False , cache_control : str = None ):
5255 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 )
5457 return func
5558
5659 return register_resolver
5760
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 ]:
5970 self .current_event = self ._as_data_class (event )
6071 self .lambda_context = context
6172
@@ -88,22 +99,14 @@ def resolve(self, event: Dict, context: LambdaContext) -> Dict:
8899
89100 return {"statusCode" : status_code , "headers" : headers , "body" : body , "isBase64Encoded" : base64_encoded }
90101
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-
99102 def _as_data_class (self , event : Dict ) -> BaseProxyEvent :
100103 if self ._proxy_type == ProxyEventType .http_api_v1 :
101104 return APIGatewayProxyEvent (event )
102105 if self ._proxy_type == ProxyEventType .http_api_v2 :
103106 return APIGatewayProxyEventV2 (event )
104107 return ALBEvent (event )
105108
106- def _find_route (self , method : str , path : str ) -> Tuple [RouteEntry , Dict ]:
109+ def _find_route (self , method : str , path : str ) -> Tuple [Route , Dict ]:
107110 method = method .upper ()
108111 for route in self ._routes :
109112 if method != route .method :
0 commit comments