[Python] Suggestions for overlapping functionality between Python Endpoints

Discussão em 'Python' iniciado por Stack, Fevereiro 25, 2025.

  1. Stack

    Stack Membro Participativo

    Currently I'm working on FastAPI endpoints in Python which basically handle two types of entities:

    /run/entityA
    /run/entityB


    Now based on the type of entity, some of the arguments change but a lot of the processing of other arguments overlap.

    Current approach:

    @router.post("/run/entityA")
    def _run_a(args_a):
    # Common processing
    # Entity A related processing
    # Common processing

    @router.post("/run/entityB")
    def _run_a(args_b):
    # Common processing
    # Entity B related processing
    # Common processing



    Previous Approach

    Previously, I kept one set of args, keeping the entity related args as Optional in FastAPI and doing an if check inside the function:

    @router.post("/run")
    def _run(args):
    # Common Processing
    if args.entityA_arg:
    # Entity A related processing
    if args.entityB_arg:
    # Entity B related processing
    # Common processing


    I see downsides in both. What are some suggestions to make the code cleaner?

    Continue reading...

Compartilhe esta Página