1414
1515logger = logging .getLogger (__name__ )
1616redis_client = Redis (
17- host = os .getenv (EnvironmentVariables .REDIS_HOST ),
17+ host = os .getenv (EnvironmentVariables .REDIS_HOST , RedisConfigs . LOCAL ),
1818 port = RedisConfigs .DEFAULT_PORT ,
1919 decode_responses = True ,
2020)
@@ -26,7 +26,8 @@ def __should_use_cache() -> bool:
2626 Separated the logic in case the cache condition changes
2727 later down the line.
2828 """
29- return os .getenv (EnvironmentVariables .REDIS_HOST ) == RedisConfigs .LOCAL
29+ host_from_env = os .getenv (EnvironmentVariables .REDIS_HOST )
30+ return host_from_env == RedisConfigs .LOCAL
3031
3132
3233def __validate_number_input (value : int ) -> Tuple [bool , str ]:
@@ -41,29 +42,28 @@ def __validate_number_input(value: int) -> Tuple[bool, str]:
4142
4243
4344@router_v0 .get ("/fizzbuzz" )
44- def compute (number : Optional [ int ] = None ) -> FizzBuzzSequence :
45+ def compute (number : int ) -> FizzBuzzSequence :
4546 """Compute the fizzbuzz sequence until the given number."""
4647 try :
4748 is_valid , error_message = __validate_number_input (number )
4849 if not is_valid :
4950 raise HTTPException (
5051 status_code = HTTPStatus .BAD_REQUEST ,
51- detail = error_message ,
52+ detail = { "error" : error_message } ,
5253 )
5354 except HTTPException as http_err :
5455 logger .error (f"Invalid input: { http_err .detail } " )
5556 raise HTTPException (
5657 status_code = http_err .status_code ,
57- detail = f"Invalid input: { http_err .detail } " ,
58+ detail = { "error" : f"Invalid input: { http_err .detail } " } ,
5859 )
5960
60- # Check cache
6161 cache_key = f"fizzbuzz:{ number } "
6262 if __should_use_cache ():
6363 cached_result = redis_client .get (cache_key )
6464 if cached_result :
6565 logger .debug (f"Key '{ cache_key } ' found in redis, using cached value" )
66- return FizzBuzzSequence .model_validate_json (cached_result )
66+ return FizzBuzzSequence .model_validate_json (str ( cached_result ) )
6767
6868 logger .debug (f"Key for number={ number } not found, will calculate" )
6969 raw_output = generate_fizzbuzz_sequence (number )
0 commit comments