5 ms·
FAST Api is a nice library, but it uses code-based api specifications. It's generates the spec from code. So you need to write your API code by hand first, and
by TeeWEE 6y ago
FAST Api is a nice library, but it uses code-based api specifications. It's generates the spec from code. So you need to write your API code by hand first, and do serialization manually...
This is an important distinction and might not be what you want.
Using spec first API design makes more sense IMHO.
You first spec your API (Either in OpenAPI or GRPC or whatever Interface Definition Language you use). This allows Backend and Frontend developers to change the API definition.
From there you generate all type-save model objects, client and server stubs, and fill in the implementation with full type hinting.
At my current company, we wrote OpenAPI specs first, no need to know Python. And use https://openapi-generator.tech/ https://openapi-generator.tech/ to generate both the Python server, python client and typescript client. We're looking into generate Kotlin and Swift clients too...
I'm a big proponent of spec first API design. It's much cheaper to agree on a spec first before you start coding.
But it depends on what you are doing.
Personally I like GRPC best, since the code generators are top notch.
Very good article on this principle:
https://medium.com/apis-and-digital-transformation/openapi-and-grpc-side-by-side-b6afb08f75ed https://medium.com/apis-and-digital-transformation/openapi-a...
First, stop writing interface code for your APIs by hand.
Second, stop writing interface code for your APIs by hand.
Third, stop writing interface code for your APIs by hand.
We're living in 2021... Every time I see somebody manually mapping an objects to a JSON dictionary I'm crying in the inside. It's like writing machine code by hand.
- ivalm 6y agoJust to further this point. In my group we use several languages (python, java, node) for various services. It is extremely useful to define our spec via openapi and then generate servers/clients that conform to this. We use swaggerhub and their code gen and it's decent for multiple languages.
- BerislavLopac 6y ago> FAST Api is a nice library, but it uses code-based api specifications. It's generates the spec from code. Shameless plug: https://pyotr.readthedocs.io https://pyotr.readthedocs.io ;)
- advance512 6y agoGreat! We're looking for a replacement for bravado. Thanks for this awesome project.
- BerislavLopac 6y agoThank you for the kind words! Please keep in mind that it is still quite a new project (less than two years old); any issue reports and comments are more than welcome! :)
- BerislavLopac 6y ago> the code generators are top notch Possibly, but I think that Python code generation is kind of pointless. CG makes sense with compiled languages, but in Python everything can be constructed on the fly at runtime, without any loss of performance.
- zmmmmm 6y agoDon't you value having type checking in your editor etc? Or is there a way you can get that without ever generating the code?
- abiogenesis 6y agoType hints?
- zmmmmm 6y agoOn what, if there is no code?
- BerislavLopac 6y agoBut there is code, and it can be type hinted and checked just as easily. First of all, any constructed classes usually inherit from some parent class, which can be checked for easily. Second, types can be constructed on the fly as well. Agreed, they will not show up in an IDE, but a) they can (and should!) be unit tested, and b) while I do value editor type-checking I value my time, and streamlined process (i.e. not having to have the extra step of code generation each time I change the spec) much more. And finally, if you version both spec and the generated code you run into a danger of them diverging without realising. If you absolutely must have the code generation, make sure not to commit the generated code, and rebuild each time you change the spec.
- trulyme 6y agoI agree with API-spec first approach, yet I still love FastAPI. I try to get first iteration written in Markdown so we can change it easily. Once it is decided upon, FastAPI lets you build the actual implementation easily. It is priceless that the docs match the implementation by default.
- zmmmmm 6y agoI agree with this in principle but in practice specifications seem a quite hard to write and maintain because you have to learn an obscure syntax for a new language that nobody on the team knows. So as a devils advocate question: what is the advantage of writing your spec in a spec language vs having it written in a (sufficiently well typed) language and generating the spec from that? Especially when you have one dominant language that most of your code is interfacing with, having the the interface be hand written with idiomatic code for that language is a lot nicer than everyone having to work with auto-generated code that is often not as nice.
- devj 6y agoThough we do not generate the entire server code, we do follow a spec first design paradigm for APIs as well as DB. We have adopted hexagonal architecture for our web framework and therefore, code generation is limited to fit in it. We use Starlette and SQLAlchemy.