[DO NOT MERGE | PROOF OF CONCEPT] Convert the API to use FastAPI instead of APIFlask #204
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This will not be merged, this is a proof-of-concept and may become its own separate repo from the flask repo (an alternative? a replacement? not sure yet).
I realize this is a massive set of changes, it may be easier to just check the code out yourself and look around instead.
Changes
Modified the API to use FastAPI rather than APIFlask
Updated Pydantic to 2.x which changes the naming on a few things like the settings classes.
Context for reviewers
This is a work-in-progress, there's a lot of TODOs that I still need to cleanup.
FastAPI docs: https://fastapi.tiangolo.com/
FastAPI is an alternative to Flask (and the many libraries built ontop of Flask like APIFlask or Connexion). It is built ontop of Starlette, and has a few noteworthy differences from Flask (from my experience of setting this up):
A few downsides I've found in my setup so far:
/users/{user_id}
) to every request, but this value isn't known until much later in processing. I figured out a way to automate this using dependencies, but it does mean the first logs (eg.start request
) don't have this value, but there's nothing that can be done about that.first_name
that is not nullable in the DB. In a POST/PUT endpoint you'd just make the Pydantic request model have a type likestr
and it'll make sure it rejects anything that is null. But for a PATCH endpoint you're fine with it either being a str OR just not set at all, which you can only know if you give Pydantic a default. There's nopartial
model you can do like we were able to with Marshmallow. But, even with Marshmallow supporting that, I found PATCH endpoints to be difficult to work with__str__
method on the validation error class, but that seems questionable (and was actually only added recently so was a deliberate decision).ApiResponse[YourResponseClassHere]
), but I'm also not certain if we want that. Adding that would be feasible, and allow us to have a message/warning object on every response as well.TODO:
Testing
Rewrote any Flask-reliant test to work with FastAPI, thankfully there were minimal changes necessary (mostly switching the test client / response formats).
You can run the API locally by doing
make init run-logs
- might want to nuke whatever you have locally as a lot of dependencies were updated. The swagger docs can be accessed athttp://localhost:8080/docs