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