22import re
33from collections .abc import Callable
44from functools import wraps
5- from typing import Any
65
7- from mangum .types import LambdaContext , LambdaEvent
6+ from flask import request
7+ from flask .typing import ResponseReturnValue
88
99from eligibility_signposting_api .common .api_error_response import (
1010 INVALID_CATEGORY_ERROR ,
2121include_actions_pattern = re .compile (r"^\s*([YN])\s*$" , re .IGNORECASE )
2222
2323
24- def validate_query_params (query_params : dict [str , str ]) -> tuple [bool , dict [ str , Any ] | None ]:
24+ def validate_query_params (query_params : dict [str , str ]) -> tuple [bool , ResponseReturnValue | None ]:
2525 conditions = query_params .get ("conditions" , "ALL" ).split ("," )
2626 for condition in conditions :
2727 search = re .search (condition_pattern , condition )
@@ -39,7 +39,7 @@ def validate_query_params(query_params: dict[str, str]) -> tuple[bool, dict[str,
3939 return True , None
4040
4141
42- def validate_nhs_number (path_nhs : str , header_nhs : str ) -> bool :
42+ def validate_nhs_number (path_nhs : str | None , header_nhs : str | None ) -> bool :
4343 logger .info ("NHS numbers from the request" , extra = {"header_nhs" : header_nhs , "path_nhs" : path_nhs })
4444
4545 if not header_nhs or not path_nhs :
@@ -55,28 +55,28 @@ def validate_nhs_number(path_nhs: str, header_nhs: str) -> bool:
5555def validate_request_params () -> Callable :
5656 def decorator (func : Callable ) -> Callable :
5757 @wraps (func )
58- def wrapper (event : LambdaEvent , context : LambdaContext ) -> dict [ str , Any ] | None :
59- path_nhs_no = event . get ( "pathParameters" , {}) .get ("id" )
60- header_nhs_no = event . get ( " headers" , {}) .get (NHS_NUMBER_HEADER )
58+ def wrapper (* args , ** kwargs ) -> ResponseReturnValue : # noqa:ANN002,ANN003
59+ path_nhs_number = str ( kwargs .get ("nhs_number" ) )
60+ header_nhs_no = str ( request . headers .get (NHS_NUMBER_HEADER ) )
6161
62- if not validate_nhs_number (path_nhs_no , header_nhs_no ):
62+ if not validate_nhs_number (path_nhs_number , header_nhs_no ):
6363 message = "You are not authorised to request information for the supplied NHS Number"
6464 return NHS_NUMBER_MISMATCH_ERROR .log_and_generate_response (log_message = message , diagnostics = message )
6565
66- query_params = event . get ( "queryStringParameters" )
66+ query_params = request . args
6767 if query_params :
6868 is_valid , problem = validate_query_params (query_params )
69- if not is_valid :
69+ if not is_valid and problem is not None :
7070 return problem
7171
72- return func (event , context )
72+ return func (* args , ** kwargs )
7373
7474 return wrapper
7575
7676 return decorator
7777
7878
79- def get_include_actions_error_response (include_actions : str ) -> dict [ str , Any ] :
79+ def get_include_actions_error_response (include_actions : str ) -> ResponseReturnValue :
8080 diagnostics = f"{ include_actions } is not a value that is supported by the API"
8181 return INVALID_INCLUDE_ACTIONS_ERROR .log_and_generate_response (
8282 log_message = f"Invalid include actions query param: '{ include_actions } '" ,
@@ -85,14 +85,14 @@ def get_include_actions_error_response(include_actions: str) -> dict[str, Any]:
8585 )
8686
8787
88- def get_category_error_response (category : str ) -> dict [ str , Any ] :
88+ def get_category_error_response (category : str ) -> ResponseReturnValue :
8989 diagnostics = f"{ category } is not a category that is supported by the API"
9090 return INVALID_CATEGORY_ERROR .log_and_generate_response (
9191 log_message = f"Invalid category query param: '{ category } '" , diagnostics = diagnostics , location_param = "category"
9292 )
9393
9494
95- def get_condition_error_response (condition : str ) -> dict [ str , Any ] :
95+ def get_condition_error_response (condition : str ) -> ResponseReturnValue :
9696 diagnostics = (
9797 f"{ condition } should be a single or comma separated list of condition "
9898 f"strings with no other punctuation or special characters"
0 commit comments