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

Migrate household endpoints to new API structure #2038

Merged
merged 27 commits into from
Dec 20, 2024

Conversation

anth-volk
Copy link
Collaborator

@anth-volk anth-volk commented Dec 4, 2024

Fixes #1988.
Fixes #2034.

This PR migrates the household endpoints to the new API structure.

policyengine_api/routes/household_routes.py Outdated Show resolved Hide resolved
policyengine_api/routes/household_routes.py Show resolved Hide resolved
policyengine_api/routes/household_routes.py Outdated Show resolved Hide resolved
policyengine_api/routes/household_routes.py Outdated Show resolved Hide resolved
policyengine_api/routes/household_routes.py Outdated Show resolved Hide resolved
tests/python/test_household.py Show resolved Hide resolved
tests/python/test_household.py Show resolved Hide resolved
tests/python/test_household.py Show resolved Hide resolved
tests/python/test_household.py Outdated Show resolved Hide resolved
tests/python/test_household.py Show resolved Hide resolved
@anth-volk anth-volk changed the title Migrate household endpoints to new API structure Migrate household endpoints to new API structure Dec 6, 2024
@anth-volk
Copy link
Collaborator Author

Hey Mike,

Thanks again for your review on this. Here's what I'd like to propose as a roadmap forward for both this PR and the PR migrating policy endpoints in a similar way (currently open as #2025). Feel free to let me know what you think, then I can proceed:

  • Immediate, as part of PR
    • Bug fixes raised as part of review
    • Migrate to specifying full endpoint address upon registration (as suggested by Mike)
      • Update PR to apply this structure to household endpoints
      • Update policy endpoint PR to also apply this structure
      • Also apply this structure to previously migrated endpoints (economy, AI)
    • Look into basic Flask validators and apply where necessary
    • Create generic exception handlers and use those in place of manually specified error responses (assuming we can easily isolate standard errors; if not, move to “In future”)
  • In progress elsewhere
    • Keep existing print statements, and then migrate these to log statements in logging PR (currently in progress, but no PR open)
    • Test improvements (see Improve testing #2021)
  • In near future (i.e., PR following this and policy endpoint refactor)
    • Chat further about improvements to testing
    • Create a clearer specification regarding keys like “status”, “message”, “result” – may require coordination with relevant stakeholders (e.g., Nikhil)
    • Look into implementing Marshmallow schema validation
  • In longer-term future
    • Discuss how to flatten API structure, especially multi-nested endpoints

@mikesmit
Copy link
Collaborator

mikesmit commented Dec 7, 2024

I approve of your plan. I have added some more discussion above, but I don't want to block this work on it.

@anth-volk anth-volk force-pushed the fix/1988-migrate-household branch from eb430a1 to a3a78fc Compare December 11, 2024 13:47
@anth-volk anth-volk requested a review from mikesmit December 14, 2024 00:37
Copy link
Collaborator

@mikesmit mikesmit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some suggestions/nitts to simplify but nonblocking.

policyengine_api/routes/economy_routes.py Outdated Show resolved Hide resolved
policyengine_api/routes/error_routes.py Outdated Show resolved Hide resolved
policyengine_api/routes/error_routes.py Outdated Show resolved Hide resolved
policyengine_api/routes/household_routes.py Outdated Show resolved Hide resolved
@anth-volk
Copy link
Collaborator Author

@mikesmit I'd like to make these changes an example moving forward, so I will go back and address your suggestions probably tomorrow, and would love to incorporate another round of changes before moving forward. In particular, I would actually like to refactor the error handlers, as they also require a double exception raising pattern within the route blueprints (happy to point out a line specifically if desired) and I think the structure could be better.

@anth-volk anth-volk requested a review from mikesmit December 18, 2024 01:37
Copy link
Collaborator

@mikesmit mikesmit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see quite what I was expecting. Requesting feedback on why that happened in the review (I.e. my feedback was unclear, my feedback was not good, we both agree on the direction but the change will happen later, etc.)

policyengine_api/routes/error_routes.py Show resolved Hide resolved
policyengine_api/routes/household_routes.py Outdated Show resolved Hide resolved
@anth-volk
Copy link
Collaborator Author

This failing test is (I'm guessing) driven by PolicyEngine/policyengine-core#321.

Copy link
Collaborator

@mikesmit mikesmit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few notes for future consideration, but strongly suggest we move forward to unblock and iterate.

policyengine_api/routes/economy_routes.py Show resolved Hide resolved
)
if household is None:
raise NotFound(f"Household #{household_id} not found.")
else:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitt, style - suggest shipping and optionally changing later to unblock me (please:P ), but clean code suggests

if error:
  raise Exception("...")

return success 

instead of unnecessary else's or nesting.

If you don't have the book, this is an ok summary

country_id (str): The country ID.
household_id (int): The household ID.
"""
print(f"Got request for household {household_id} in country {country_id}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussion, nonblocking -
Suggest this as an optimization after introducing "real" logging.

In the theme from yesterday of making our route functions as clean and simple as possible, logging is another function the framework should be able to do for us in many cases.

In this case the blueprint already defines what the path is, the names of the parameters, the method type and has the parsed values from the path, etc. so it should be possible to automate logging all the relevant "got a request" information.

  1. Reduces clutter/duplication
  2. reduces cases where we forget to manually add the log message
  3. makes it easy to add more detail later and get that for every method (I.e. maybe we want to log which user is making the request)

policyengine_api/routes/household_routes.py Show resolved Hide resolved
policyengine_api/routes/household_routes.py Show resolved Hide resolved
response.headers["X-Accel-Buffering"] = "no"
# Set header to prevent buffering on Google App Engine deployment
# (see https://cloud.google.com/appengine/docs/flexible/how-requests-are-handled?tab=python#x-accel-buffering)
response.headers["X-Accel-Buffering"] = "no"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion, non-blocking again strongly suggest we don't iterate again over this, but I think this would be cleaner as a decorator like this. I don't think that is necessarily the right library so that would require a little research.

Suggest we look into how to set headers as part of integrating parsing/serialization automation.

@anth-volk anth-volk merged commit 948f336 into master Dec 20, 2024
4 checks passed
@anth-volk anth-volk deleted the fix/1988-migrate-household branch December 20, 2024 19:36
@anth-volk
Copy link
Collaborator Author

@mikesmit Just merged. Thanks again for your help throughout this process. Hoping this unblocks your upcoming PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants