News for hackers. Stuff that matters.
Re:FastAPI framework, high perf, easy to learn, fast to code... by TeeWEE on Monday February 01, 2021 @02:45PM (#25992783)
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/ 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-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.

Re:FastAPI framework, high perf, easy to learn, fast to code... by zmmmmm on Monday February 01, 2021 @05:48PM (#25995198)
I 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.

Re:FastAPI framework, high perf, easy to learn, fast to code... by ivalm on Monday February 01, 2021 @02:50PM (#25992828)
Just 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.
Re:FastAPI framework, high perf, easy to learn, fast to code... by trulyme on Monday February 01, 2021 @03:25PM (#25993358)
I 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.
Re:FastAPI framework, high perf, easy to learn, fast to code... by BerislavLopac on Monday February 01, 2021 @03:10PM (#25993130)
> 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 ;)

Re:FastAPI framework, high perf, easy to learn, fast to code... by advance512 on Monday February 01, 2021 @05:25PM (#25994936)
Great! We're looking for a replacement for bravado. Thanks for this awesome project.
Re:FastAPI framework, high perf, easy to learn, fast to code... by BerislavLopac on Tuesday February 02, 2021 @03:55AM (#25998707)
Thank 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! :)

Re:FastAPI framework, high perf, easy to learn, fast to code... by BerislavLopac on Monday February 01, 2021 @03:12PM (#25993178)
> 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.

Re:FastAPI framework, high perf, easy to learn, fast to code... by zmmmmm on Monday February 01, 2021 @05:42PM (#25995131)
Don't you value having type checking in your editor etc? Or is there a way you can get that without ever generating the code?
Re:FastAPI framework, high perf, easy to learn, fast to code... by abiogenesis on Monday February 01, 2021 @08:24PM (#25996463)
Type hints?
Re:FastAPI framework, high perf, easy to learn, fast to code... by zmmmmm on Monday February 01, 2021 @11:16PM (#25997374)
On what, if there is no code?
Re:FastAPI framework, high perf, easy to learn, fast to code... by BerislavLopac on Tuesday February 02, 2021 @03:54AM (#25998697)
But 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.

Re:FastAPI framework, high perf, easy to learn, fast to code... by devj on Tuesday February 02, 2021 @01:19AM (#25997927)
Though 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.