Skip to content

Commit e9cd98b

Browse files
committed
fix: tidy up endpoint logic
1 parent 5d0a44d commit e9cd98b

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

api/core/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class EnvironmentVariables:
1010
@dataclass(frozen=True)
1111
class RedisConfigs:
1212
LOCAL: Final[str] = "localhost"
13+
DEFAULT_HOST: Final[str] = "redis-cache"
1314
DEFAULT_PORT: Final[int] = 6379
1415

1516

api/routes/v0/fizzbuzz.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
logger = logging.getLogger(__name__)
1616
redis_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

3233
def __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

Comments
 (0)