Closed mhauru closed 9 months ago
Turns out there's a plethora of tools for this. The key thing is the OpenAPI standard, and there are several tools for specifying your API in this standard and having the code and the docs generated, or for reading this standard from your API code and having docs generated. Selecting from the tools available, I would prefer one that
One that survives both of those filters is apispec, but it doesn't seem as fully featured and I'm not sure Flask-pluggability is as good, although I might be wrong on the latter. There's also flask-apispec, a Flask extension that uses apispec, but that's unmaintained and doesn't support OpenAPI 3.0.
flask-apispec also uses Marshmallow for serializing Python objects to JSON, and webargs for parsing webargs. We should probably start using webargs, it seems really handy. Maybe on the frontend too.
When you read about this, a common recommendation is to switch from Flask to FastAPI. Might be annoyingly big of an effort and not worth the switching cost, but worth keeping in mind as an option, it could also work out quite cleanly.
I spent some time reading on switching to FastAPI, trying to decide if it would be worth it.
Benefits:
Cons:
See here for a guide to transitioning from Flask to FastAPI: https://engineering.forethought.ai/blog/2022/12/01/migrating-from-flask-to-fastapi-part-1/#
For the cost of switching, I think transforming most of the API code would be very easy and done in an afternoon. The two parts that give me pause are the JWT authentication, and possibly some of the testing and pytest fixtures, although the latter could turn out to be trivially easy. We would also lose Flask-sqlalchemy, which I think we are only using to give a new database session for each API call and that would be very easy replace, and Flask-migration, which we import but I think don't use at all.
Currently doing a little trial of how easily we could switch to FastAPI.
Markus has been working on this for a few days. He has been blocked by a bug. If bug is not resolved within a few hours of coding time, then we will archive this for now.
Got the bug cleared on Tuesday, finished all the hard parts, now most of the way there in doing the migration to FastAPI. Will finish next week.
There's a lot of repetitive code and documentation for the API. Almost all of the following things are done for almost all of the endpoints: Define the JSON schema of the input payload, define the JSON schema of the return payload, which status codes should be returned in which cases, what should the error messages be if the input is malformed, convert between JSON and python object, document this in the function docstring and in the API docs in
backend/REAMDE.md
.There must be tools for automating this, so that we could just write the JSON schema definitions and some error responses in a concise format, and the tool would turn that into an endpoint. Look into this, and if we find a good tool, use it, otherwise maybe write our own automation.