Enforce the free-tier daily API quota

Python · Python · intermediate · greenfield

Adds the daily-quota gate: a plan-limit lookup, an in-memory usage counter, and is_allowed that blocks a user once they reach their plan's limit. Ships with a unit test for the free-tier cap; external usage lookup is mocked. Test passes.

usage_today reads a per-user counter from the cache (Redis in prod); the in-memory dict here is the local stand-in.

Requirements

Files touched

--- app/quota.py
+DAILY_LIMITS = {"free": 100, "pro": 10_000}
+
+
+def daily_limit(plan: str) -> int:
+    return DAILY_LIMITS.get(plan, DAILY_LIMITS["free"])
+
--- app/usage.py
+# In-memory stand-in for the per-user daily counter (Redis in production).
+_counters: dict[str, int] = {}
+
+
+def usage_today(user_id: str) -> int:
+    """Number of API calls the user has made so far today."""
+    return _counters.get(user_id, 0)
+
--- app/gate.py
+from app.quota import daily_limit
+from app.usage import usage_today
+
+
+def is_allowed(user) -> bool:
+    used = usage_today(user.id)
+    return used < daily_limit(user.plan)
+
--- tests/test_gate.py
+from types import SimpleNamespace
+from unittest.mock import patch
+
+from app.gate import is_allowed

Review this PR

Python practice