11from flask import Blueprint , Response , request
2+ from werkzeug .exceptions import NotFound , InternalServerError , BadRequest
23import json
34
45from policyengine_api .constants import COUNTRY_PACKAGE_VERSIONS
1415household_service = HouseholdService ()
1516
1617
17- @household_bp .route ("/<country_id>/household/<int:household_id>" , methods = ["GET" ])
18+ @household_bp .route (
19+ "/<country_id>/household/<int:household_id>" , methods = ["GET" ]
20+ )
1821@validate_country
1922def get_household (country_id : str , household_id : int ) -> Response :
2023 """
@@ -31,15 +34,7 @@ def get_household(country_id: str, household_id: int) -> Response:
3134 country_id , household_id
3235 )
3336 if household is None :
34- return Response (
35- json .dumps (
36- {
37- "status" : "error" ,
38- "message" : f"Household #{ household_id } not found." ,
39- }
40- ),
41- status = 404 ,
42- )
37+ raise NotFound (f"Household #{ household_id } not found." )
4338 else :
4439 return Response (
4540 json .dumps (
@@ -52,14 +47,8 @@ def get_household(country_id: str, household_id: int) -> Response:
5247 status = 200 ,
5348 )
5449 except Exception as e :
55- return Response (
56- json .dumps (
57- {
58- "status" : "error" ,
59- "message" : f"An error occurred while fetching household #{ household_id } . Details: { str (e )} " ,
60- }
61- ),
62- status = 500 ,
50+ raise InternalServerError (
51+ f"An error occurred while fetching household #{ household_id } . Details: { str (e )} "
6352 )
6453
6554
@@ -77,10 +66,7 @@ def post_household(country_id: str) -> Response:
7766 payload = request .json
7867 is_payload_valid , message = validate_household_payload (payload )
7968 if not is_payload_valid :
80- return Response (
81- status = 400 ,
82- response = f"Unable to create new household; details: { message } " ,
83- )
69+ raise BadRequest (f"Unable to create new household; details: { message } " )
8470
8571 try :
8672 # The household label appears to be unimplemented at this time,
@@ -107,19 +93,14 @@ def post_household(country_id: str) -> Response:
10793 )
10894
10995 except Exception as e :
110- return Response (
111- json .dumps (
112- {
113- "status" : "error" ,
114- "message" : f"An error occurred while creating a new household. Details: { str (e )} " ,
115- }
116- ),
117- status = 500 ,
118- mimetype = "application/json" ,
96+ raise InternalServerError (
97+ f"An error occurred while creating a new household. Details: { str (e )} "
11998 )
12099
121100
122- @household_bp .route ("/<country_id>/household/<int:household_id>" , methods = ["PUT" ])
101+ @household_bp .route (
102+ "/<country_id>/household/<int:household_id>" , methods = ["PUT" ]
103+ )
123104@validate_country
124105def update_household (country_id : str , household_id : int ) -> Response :
125106 """
@@ -134,9 +115,8 @@ def update_household(country_id: str, household_id: int) -> Response:
134115 payload = request .json
135116 is_payload_valid , message = validate_household_payload (payload )
136117 if not is_payload_valid :
137- return Response (
138- status = 400 ,
139- response = f"Unable to update household #{ household_id } ; details: { message } " ,
118+ raise BadRequest (
119+ f"Unable to update household #{ household_id } ; details: { message } "
140120 )
141121
142122 try :
@@ -149,15 +129,7 @@ def update_household(country_id: str, household_id: int) -> Response:
149129 country_id , household_id
150130 )
151131 if household is None :
152- return Response (
153- json .dumps (
154- {
155- "status" : "error" ,
156- "message" : f"Household #{ household_id } not found." ,
157- }
158- ),
159- status = 404 ,
160- )
132+ raise NotFound (f"Household #{ household_id } not found." )
161133
162134 # Next, update the household
163135 household_service .update_household (
@@ -177,13 +149,6 @@ def update_household(country_id: str, household_id: int) -> Response:
177149 mimetype = "application/json" ,
178150 )
179151 except Exception as e :
180- return Response (
181- json .dumps (
182- {
183- "status" : "error" ,
184- "message" : f"An error occurred while updating household #{ household_id } . Details: { str (e )} " ,
185- }
186- ),
187- status = 500 ,
188- mimetype = "application/json" ,
152+ raise InternalServerError (
153+ f"An error occurred while updating household #{ household_id } . Details: { str (e )} "
189154 )
0 commit comments