Live data from Hacker News

FastAPI – Easily create robust, standardized API endpoints in Rails and Postgres

github.com

11–20 of 26 posts

Re: FastAPI – Easily create robust, standardized API endpoints in Rails and Postgres

#11
post #2

I'm confused as to why view logic is being moved to the Model layer, directly breaking MVC. Why is this better than using the tools in Rails that already exist for every use case here? Is there a performance increase?

I came here to say the exact same thing. After having worked on/scaled an API that served >100k requests per minute, I feel I can say with some experience that defining your JSON in your model is a very bad idea. A classic example that we ran into repeatedly: Assume you want to have an endpoint that populates a user's profile (for the user to see). You probably want id, name, email, age, location, created_at, updated…

I don't think there's actually a good way to solve this (that I know of) for applications with complex enough permission and role hierarchies.

Once you need a list of all the glass marbles with their attributes, but can only see the color of marbles you supervise, and only the radius of a marble if it has a shooter role and only the names and emails of marbles ranked above you... ...and so on... it becomes a situation where each result has to be filtered item by item pretty much. And the filter hierarchies will snowball like tribbles.

Re: FastAPI – Easily create robust, standardized API endpoints in Rails and Postgres

#13
I like the idea, but I would be really reluctant to use a gem that has no tests, especially when the core methods are clearly not trivial, a bunch of them are ~100 lines long. Not that the number of lines tells how complicated a method is, but I think it is a good indicator.

Re: FastAPI – Easily create robust, standardized API endpoints in Rails and Postgres

#14
post #13

I like the idea, but I would be really reluctant to use a gem that has no tests, especially when the core methods are clearly not trivial, a bunch of them are ~100 lines long. Not that the number of lines tells how complicated a method is, but I think it is a good indicator.

Perfectly reasonable. Tests are on the (close) horizon. We'll be rolling out more iterations in the coming weeks on the path to 1.0 (support for other data layers is also on the to-do list). Feedback is much appreciated.

Re: FastAPI – Easily create robust, standardized API endpoints in Rails and Postgres

#15

Earlier quoted context omitted.

I came here to say the exact same thing. After having worked on/scaled an API that served >100k requests per minute, I feel I can say with some experience that defining your JSON in your model is a very bad idea. A classic example that we ran into repeatedly: Assume you want to have an endpoint that populates a user's profile (for the user to see). You probably want id, name, email, age, location, created_at, updated…

I don't think there's actually a good way to solve this (that I know of) for applications with complex enough permission and role hierarchies. Once you need a list of all the glass marbles with their attributes, but can only see the color of marbles you supervise, and only the radius of a marble if it has a shooter role and only the names and emails of marbles ranked above you... ...and so on... it becomes a situatio…

The `djangorestframework-composed-permissions` package for DRF gets pretty close to handling this situation effectively. Basically roles and permissions arbitrarily composable with boolean expressions.

Although you can model permission hierarchies, I think there are enough corner cases that it's worthwhile to declare permissions as explicitly as conveniently possible.

API Docs: https://djangorestframework-composed-permissions.readthedocs...

Re: FastAPI – Easily create robust, standardized API endpoints in Rails and Postgres

#16
There is grape, grape-swagger, grape-swagger rails, and a bunch of other gems for creating API's. They work amazingly well, and have a pretty active community. They have solved a ton of issues: Jsonp, Cors, authentication, rate limiting, versioning, and DOCUMENTATION. The auto-generated Swagger documentation is a real thing of beauty.

Re: FastAPI – Easily create robust, standardized API endpoints in Rails and Postgres

#17

Earlier quoted context omitted.

I came here to say the exact same thing. After having worked on/scaled an API that served >100k requests per minute, I feel I can say with some experience that defining your JSON in your model is a very bad idea. A classic example that we ran into repeatedly: Assume you want to have an endpoint that populates a user's profile (for the user to see). You probably want id, name, email, age, location, created_at, updated…

I don't think there's actually a good way to solve this (that I know of) for applications with complex enough permission and role hierarchies. Once you need a list of all the glass marbles with their attributes, but can only see the color of marbles you supervise, and only the radius of a marble if it has a shooter role and only the names and emails of marbles ranked above you... ...and so on... it becomes a situatio…

View models/decorators have served me very well in this case. The view model would take the marble instance, viewing user instance, etc and decide which attributes it should emit as JSON. The result is frequently very clean, very maintainable code.

Re: FastAPI – Easily create robust, standardized API endpoints in Rails and Postgres

#18
post #2

I'm confused as to why view logic is being moved to the Model layer, directly breaking MVC. Why is this better than using the tools in Rails that already exist for every use case here? Is there a performance increase?

Good question. If you consider each API endpoint a very specific serialization of a model, are the serialization parameters considered part of the model itself? (I would consider them such, and decided to approach the development with that in mind.)

I don't think so. The data as presented to the user through the API is a view concern. The model should never, ever know or care about how it is presented; serialization (which is used to marshal the data as a whole for transport and reconstitution) and presentation (which is used to transform the data into some form acceptable for consumption by an external entity) are separate processes with separate goals.

I think you've conflated serialization and presentation here; if I can't reconstitute the whole object from the serialization provided, then it's not a serialization, it's a presentation. A valid presentation might be "the serialization of this object", but "a subset of this object suitable for display" is not a valid serialization.

Re: FastAPI – Easily create robust, standardized API endpoints in Rails and Postgres

#20
post #18

Earlier quoted context omitted.

Good question. If you consider each API endpoint a very specific serialization of a model, are the serialization parameters considered part of the model itself? (I would consider them such, and decided to approach the development with that in mind.)

I don't think so. The data as presented to the user through the API is a view concern. The model should never, ever know or care about how it is presented; serialization (which is used to marshal the data as a whole for transport and reconstitution) and presentation (which is used to transform the data into some form acceptable for consumption by an external entity) are separate processes with separate goals. I think…

Good points. I think it boils down to an issue of semantics, though. FastAPI effectively eliminates (or hides) the View layer by allowing the Model to determine which fields it should allow the View to access. If you don't believe this is explicit enough (which could be a fair argument!), it's perfectly reasonable to suggest this "breaks" standard MVC conventions (as it can easily be construed as the View being part of the Model!).

I would argue that it merely allows for more automated (albeit implicit) generation of a View.

The goal here was to simplify the API endpoint creation process for the vast majority of use cases, and provide a standard output that could be easily recognized and consistently managed by any external software.

Post reply on HN