2
\$\begingroup\$

I'm not so experienced at developing Fast API apps. As I program in different languages, I often get confused as how things are done in a python way.

I have this piece of code, and I would like to know if this is the correct way to handle endpoints in fastapi. I'm more used to program in an object-oriented way, but maybe for this kind of app, the approach is usually different.

@router.post(
    "/upload", status_code=status.HTTP_201_CREATED, response_model=list[Student]
)
async def upload_csv_file(
    file: UploadFile,
    hasher: Annotated[object, Depends(dependencies.get_hash)],
    db: Annotated[object, Depends(dependencies.get_db)],
):
    try:
        # Check if content-type is a text/csv
        if file.content_type != "text/csv":
            raise HTTPException(
                status_code=status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
                detail="CSV file must be provided",
            )
        repository = StudentRepository(db)
        service = StudentService(repository)
        content = await file.read()
        content = content.decode("utf-8")

        res = service.create_students_from_string(content, hasher)

        return res
    except HTTPException as e:
        raise e
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

import

There's some missing Review Context, which obscures the meaning of the code you posted. Please include import statements; they are an essential part of your source code. There's not even a GitHub link to help fill in the gaps.

annotations

correct way to handle endpoints

@router.post(
    "/upload", status_code=status.HTTP_201_CREATED, response_model=list[Student]
)

Yes, this looks about right. Though I confess I don't recognize the Student annotation -- I would have anticipated "... , response_model: list[Student]", with : colon rather than an = assignment. And maybe with the status code appearing last.

    hasher: Annotated[object, Depends(dependencies.get_hash)],
    db: Annotated[object, Depends(dependencies.get_db)],

Those certainly look like type annotations, but were they perhaps originally written a few years ago, or intended to target an ancient interpreter? I would expect Any rather than object, and the Annotated seems on the redundant side.
from typing import Any

        content = await file.read()
        content = content.decode("utf-8")

type nit: We assign bytes to content, then assign a unicode str to it. Consider inventing two distinct names for those, or just combine it all into a single expression.

useless try / catch

    except HTTPException as e:
        raise e

I can't imagine why you went to the trouble of opening a try block there. Simply let the exception bubble up the call stack normally. It will have the same effect.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks @J_H for the observations. I appreciate all the comments. \$\endgroup\$ Commented Jul 17 at 12:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.