假设有一个音频服务器,您可以上传歌曲、播客或有声读物。在create endpoint中,我已经创建了4个endpoint,所以我设置了一个条件,如果audio_type是一首歌,则返回该类型的所有音频,但不幸的是,这将返回null
@app.get('/audio/{audio_type}')
def show_all(audio_type):
if audio_type == "Songs":
@app.get("audio/song")
def all(db: Session = Depends(database.get_db)):
songs = db.query(models.Song).all()
print("songs = ", songs)
return songs
elif audio_type == "podcast":
@app.get('audio/podcast')
def all(db: Session = Depends(database.get_db)):
podcast = db.query(models.Podcast).all()
return podcast
elif audio_type == "audiobook":
@app.get('audio/audiobook')
def all(db: Session = Depends(database.get_db)):
audiobook = db.query(models.Audiobook).all()
return audiobook
else:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f' {audio_type} - audio type is not valid')发布于 2021-03-23 00:28:53
您的实现违背了API的目的。对于这样的实现,尝试将该值作为参数传递给您的API,并在此基础上将流分叉。
def all(db: Session = Depends(database.get_db), audio_type):
if audio_type == "Songs":
songs = db.query(models.Song).all()
print("songs = ", songs)
return songs
elif audio_type == "podcast":
podcast = db.query(models.Podcast).all()
return podcast
elif audio_type == "audiobook":
audiobook = db.query(models.Audiobook).all()
return audiobook
else:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f' {audio_type} - audio type is not valid')
@app.get('/audio')
def show_all(audio_type: str):
return all(Depends(database.get_db), audio_type):https://stackoverflow.com/questions/66749744
复制相似问题