Reputation: 21
suppose there as an audio server, you can upload Songs, podcasts, or Audiobook. So in the create endpoint i have created 4 endpoints, so i have put an condition if the audio_type is a song, return all audio of that type but unfortunately this return 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')
Upvotes: 0
Views: 580
Reputation: 719
You are defeating the purpose of an API with your implementation. For such an implementation, try passing the value as an argument to your API and based upon that you can bifurcate the flow.
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):
Upvotes: 2