Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get_policy_search endpoint to return 404 when no policy is found #1854

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- bump: minor
changes:
added:
- added fix to return correct status codes for no policies found in get_policy_search endpoint

101 changes: 61 additions & 40 deletions policyengine_api/endpoints/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,24 @@ def set_policy(
)


def get_policy_search(country_id: str) -> list:
def get_policy_search(country_id: str) -> dict:
"""
Search for policies.
Search for policies for a specified country
Args:
country_id (str): The country ID.
query (str): The search query.
Query Parameters:
query (str): Optional search term to filter policies
unique_only (bool): If true, return only unique policy-label combinations
Returns:
list: The search results.
Response: Json response with:
- On success: list of policies with id and label
- On failure: error message and appropriate status code
Example:
GET /api/policies/us?query=tax&unique_only=true
"""
query = request.args.get("query", "")
# The "json.loads" default type is added to convert lowercase
Expand All @@ -198,45 +206,58 @@ def get_policy_search(country_id: str) -> list:
if country_not_found:
return country_not_found

results = database.query(
"SELECT id, label, policy_hash FROM policy WHERE country_id = ? AND label LIKE ?",
(country_id, f"%{query}%"),
)
if results is None:
return dict(
status="error",
message=f"Policy not found in {country_id}",
try:
results = database.query(
"SELECT id, label, policy_hash FROM policy WHERE country_id = ? AND label LIKE ?",
(country_id, f"%{query}%"),
)
else:

results = results.fetchall()

# If unique_only is true, filter results to only include
# items where everything except ID is unique
if unique_only:
processed_vals = set()
new_results = []

# Compare every label and hash to what's contained in processed_vals
# If a label-hash set aren't already in processed_vals,
# add them to new_results
for policy in results[:]:
comparison_vals = policy["label"], policy["policy_hash"]
if comparison_vals not in processed_vals:
new_results.append(policy)
processed_vals.add(comparison_vals)

# Overwrite results with new_results
results = new_results

# Format into: [{ id: 1, label: "My policy" }, ...]
policies = [
dict(id=result["id"], label=result["label"]) for result in results
]
return dict(
status="ok",
message=None,
result=policies,
)
if not results:
tawandamoyo marked this conversation as resolved.
Show resolved Hide resolved
body = dict(
status="error",
message=f"No policies found for country {country_id} for query '{query}",
)
return Response(
json.dumps(body), status=404, mimetype="application/json"
)

# If unique_only is true, filter results to only include
# items where everything except ID is unique
if unique_only:
processed_vals = set()
new_results = []

# Compare every label and hash to what's contained in processed_vals
# If a label-hash set aren't already in processed_vals,
# add them to new_results
for policy in results[:]:
comparison_vals = policy["label"], policy["policy_hash"]
if comparison_vals not in processed_vals:
new_results.append(policy)
processed_vals.add(comparison_vals)

# Overwrite results with new_results
results = new_results

# Format into: [{ id: 1, label: "My policy" }, ...]
policies = [
dict(id=result["id"], label=result["label"]) for result in results
]
body = dict(
status="ok",
message="Policies found",
result=policies,
)
return Response(
tawandamoyo marked this conversation as resolved.
Show resolved Hide resolved
json.dumps(body), status=200, mimetype="application/json"
)
except Exception as e:
body = dict(status="error", message=f"Internal server error: {e}")
return Response(
json.dumps(body), status=500, mimetype="application/json"
)


def get_current_law_policy_id(country_id: str) -> int:
Expand Down
Loading