#Single get endpoint with multiple paths / parameters

1 messages · Page 1 of 1 (latest)

tiny elk
#

Hello,

I wanted to know if it's possible to create a single endpoint that will perform the get of the entity by either an id or a slug?

I can see that the path parameter of the get endpoint can take a sequence of str as path (so maybe it was intendend to work like that)

This is working fine for the moment, but the thing is that I would like all subsequent routes to take as parent parent parameter either an id or the slug of the competition.

# Endpoint by ID
@get(
        operation_id="GetCompetition",
        name="competitions:get",
        path=urls.COMPETITION_DETAIL,
        summary="Retrieve the details of a competition.",
    )
    async def get_competition(
        self,
        competitions_service: CompetitionService,
        competition_id: Annotated[
            UUID,
            Parameter(
                title="Competition ID",
                description="The competition to retrieve.",
            ),
        ],
    ) -> Competition:
        """Get a competition."""
        db_obj = await competitions_service.get(competition_id)
        return competitions_service.to_schema(db_obj, schema_type=Competition)

# Endpoint by slug
@get(
        operation_id="GetCompetitionBySlug",
        name="competitions:get-by-slug",
        path=urls.COMPETITION_DETAIL_SLUG,
        summary="Retrieve the details of a competition from a slug.",
    )
    async def get_competition_by_slug(
            self,
            competitions_service: CompetitionService,
            competition_slug: Annotated[
                str,
                Parameter(
                    title="Competition Slug",
                    description="The competition to retrieve.",
                ),
            ],
    ) -> Competition:
        """Get a competition."""
        db_obj = await competitions_service.get(item_id=competition_slug, id_attribute="slug")
        return competitions_service.to_schema(db_obj, schema_type=Competition)

Thanks

autumn notchBOT
#
Notes for Single get endpoint
At your assistance

@tiny elk

No Response?

If no response in a reasonable time, ping @Member.

Closing

To close, type !solve or byte solve.

MCVE

Please include an MCVE so that we can reproduce your issue locally.

tiny elk
#

Single get endpoint with multiple paths / parameters

#

By subsequent routes, I can have a list of teams inside the competition so I would like to be able to do /competitions/{id}/teams or /competitions/{slug}/teams without duplicating code. But maybe its a bad thing to do that (Thank you the limit of characters from Discord #FreeUser)