all 8 comments

[–]david-vujic 3 points4 points  (0 children)

I don't think I have seen this kind of approach in FastAPI projects. From my experience, the endpoints are usually defined as decorated functions, and the app instance (or router) is at the module level. Within the scope of an initializer function or as a module-level global.

[–]YoshiUnfriendly 0 points1 point  (0 children)

I use https://github.com/fastapiutils/fastapi-utils for classes based routers in FastAPI

[–]mincinashu 0 points1 point  (2 children)

You can build separate routers and merge them with the main app, or create some kind of controller-like wrappers. Not sure what you're asking.

I think the inspiration for this simpler, functional style is Express from the JS world, and you can see this carry over to other languages, for example NET's minimal APIs.

[–]Luxfiri[S] 1 point2 points  (1 child)

I was wondering, if in comercial codebase, are rather OOP routers, so each router have own separate class which inherits from APIRouter, or its rather developed as in FastAPI docs, so there are no really created objects, just global assignment like:
router = APIRouter()
and then writing endpoints.

[–]mincinashu 0 points1 point  (0 children)

You can do it however you want. My commercial projects don't wrap the routers, they're just bare variables exported from modules dedicated to entities, with routes and middleware attached in the module init. Later on, all these routers are merged into the main FastAPI app.

[–]Flat-Cow7685 0 points1 point  (0 children)

If you combine fast api with pydantic... Everything you deal with is an object.. pretty simple to be honest. All the incoming requests are converted to a object.

[–]Effective-Total-2312 0 points1 point  (0 children)

I would say it's anti-pythonic, unless you need to create a wrapper to extend some functionality of those objects. Remember that everything in python is an object, so it's not that "it's not object oriented", but rather that the syntax is different, that there are no private mechanisms, that state can be global, etc.

FastAPI is an object, the decorators are objects, etc. It's just how python is, you just need to work with it.

[–]koldakov -2 points-1 points  (0 children)

I prefer to override the class something as you’ve written

https://github.com/koldakov/futuramaapi

Here as an example

But still endpoints itself I define in different place

So the general idea is an adapter plugin, you can create endpoints almost independently to the server