Skip to content

Commit

Permalink
feat: Refactor household endpoints to match new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
anth-volk committed Dec 11, 2024
1 parent 2129cda commit 6bd8420
Show file tree
Hide file tree
Showing 4 changed files with 419 additions and 3 deletions.
8 changes: 5 additions & 3 deletions policyengine_api/routes/household_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
household_service = HouseholdService()


@validate_country
@household_bp.route("/<household_id>", methods=["GET"])
@validate_country
def get_household(country_id: str, household_id: str) -> Response:
"""
Get a household's input data with a given ID.
Expand All @@ -24,6 +24,7 @@ def get_household(country_id: str, household_id: str) -> Response:
country_id (str): The country ID.
household_id (str): The household ID.
"""
print(f"Got request for household {household_id} in country {country_id}")

# Ensure that household ID is a number
try:
Expand All @@ -38,6 +39,7 @@ def get_household(country_id: str, household_id: str) -> Response:
household: dict | None = household_service.get_household(
country_id, household_id
)
print(household)
if household is None:
return Response(
json.dumps(
Expand Down Expand Up @@ -71,8 +73,8 @@ def get_household(country_id: str, household_id: str) -> Response:
)


@validate_country
@household_bp.route("", methods=["POST"])
@validate_country
def post_household(country_id: str) -> Response:
"""
Set a household's input data.
Expand Down Expand Up @@ -127,8 +129,8 @@ def post_household(country_id: str) -> Response:
)


@validate_country
@household_bp.route("/<household_id>", methods=["PUT"])
@validate_country
def update_household(country_id: str, household_id: str) -> Response:
"""
Update a household's input data.
Expand Down
5 changes: 5 additions & 0 deletions policyengine_api/services/household_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ def get_household(self, country_id: str, household_id: int) -> dict | None:
(household_id, country_id),
).fetchone()

print(row)
print(type(row))

# If row is present, we must JSON.loads the household_json
household = None
if row is not None:
print("Row is not None")
household = dict(row)
if household["household_json"]:
print("household_json is not None")
household["household_json"] = json.loads(
household["household_json"]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def validate_country(func):
def validate_country_wrapper(
country_id: str, *args, **kwargs
) -> Union[None, Response]:
print("Validating country")
if country_id not in COUNTRIES:
body = dict(
status="error",
Expand Down
Loading

0 comments on commit 6bd8420

Please sign in to comment.