BlackSheep icon indicating copy to clipboard operation
BlackSheep copied to clipboard

Router blueprint

Open Kokoserver opened this issue 1 year ago • 4 comments

Note: consider using Discussions to open a conversation about new features…

🚀 Feature Request i don't know, though have tried to check, but i don't know if there is anything like blueprint like of flask or Router like of Fastapi, will really make sense, rather than creating new app instances and mounting it can be really costly.

Thanks for blacksheep

Kokoserver avatar Aug 02 '22 17:08 Kokoserver

Hi @Kokoserver Sorry for replying late, in the last days I dedicated time to another open source thing (will also be useful for BlackSheep).

And thank you for your kind words!

Right now the word "BluePrint" is not used in BlackSheep, but this doesn't mean that the framework lacks features that are equivalent to BluePrints as they are presented in the FastAPI documentation.

Note that even FastAPI doesn't use the word "BluePrint" in its source code. They use this word in the documentation because it's familiar for people who used Flask. (The note at the top of the page: If you come from Flask, this would be the equivalent of Flask's Blueprints.).

In BlackSheep you can define a Controller class with base path getting access to very similar features / tools (modularization in different files, own dependencies resolved by dependency injection, base route, automatic tags when generating OpenAPI Documentation).

See for example:

class AlbumsController(ApiController):
    def __init__(self, manager: AlbumsHandler) -> None:
        super().__init__()

        self.manager = manager

    @classmethod
    def class_name(cls) -> str:
        return "albums"

    @get("/")
    async def get_albums(self) -> List[Album]:
        """
        Gets the list of albums configured in the system.
        """
        return await self.manager.get_albums()

    @get("/:album_id")
    async def get_album_details(self, album_id: UUID) -> Album:
        """
        Gets details about an album, by its id.
        """
        return await self.manager.get_album(album_id)

    @auth(Roles.ADMIN)
    @post("/:album_id")
    async def update_album(self, album_id: UUID, data: UpdateAlbumInput) -> Album:
        """
        Updates an album by id, with the given input.
        """
        return await self.manager.update_album(data)

The handled routes are actually:

  • "/api/albums/"
  • "/api/albums/{{album_id}}"

It would be even possible to pack controllers in dedicated Python packages, for more complex scenarios (example: having a library with common endpoints to handle OAuth endpoints).

RobertoPrevato avatar Aug 12 '22 14:08 RobertoPrevato

Thanks alot

Kokoserver avatar Aug 12 '22 18:08 Kokoserver

I reopen this issue because the documentation can be improved. 😃

RobertoPrevato avatar Aug 13 '22 07:08 RobertoPrevato

Yes definitely

Kokoserver avatar Sep 16 '22 18:09 Kokoserver