Say that I have a REST endpoint for a chess server. If I'm not logged in and do a GET on /games
I could get all running games like:
{
running_games: [
.....
]
}
but if I'm logged in I could get a different representation for that same endpoint /games
, something like:
{
your_games: {
waiting_your_move: [ .... ],
waiting_for_opponent_move: [ ... ]
},
running_games: [ ... ]
}
Basically when I'm logged in from all running games I known the ones in which I'm participating, while when anonymous I can only get the whole list of undifferentiated games.
So my questions are:
- Is this a responsibility for the frontend? I would say maybe, but sometimes the frontend doesn't know about the user itself because encrypted JWT that is only dealt in the backend.
- Is it OK to return one of those representations for the same endpoint or should I created different endpoints for signed or unsigned? What are best practices or trade offs?
I got this doubt when coding this API in OpenApi 3 where uniqueness is defined solely by the pair path+verb not being able to use the authentication header parameter to point to different response body for each situation. So I either create a oneOf
response that to me looks "ugly" or I create the before mentioned different paths.
Maybe this is a constraint from OpenApi 3 itself but those constraints have guided me well in the past so I don't know if this is "bug or feature".