Reputation: 4516
I'm working on a project with a friend and we want to send an email to a user when they move on to my next round in a lottery game. I wanted to ask some advice from the community on what is a good route name and hierarchy for a particular scenario.
On my end I have a database and an API. In my database I have a table that contains users. There is a flag called IsProcessedForLottery on this user table. Every night I send my users over to my friend's database table (only users where IsProcessedForLottery = false). His cron job will run code that performs the calculations on the data (on his side), and then the idea is for him to invoke my REST API to tell me who won the lottery. This call should do 3 things:
Assume that I can't run the calculations on my own, that he can't send the emails on his side, and more importantly that I can't invoke his API to ask if a particular user is the winner or not. The data has to be sent to him, and he has to send the winner back to me after some period of time.
So in summary, I send him the data, his algorithms run, and he calls my API to tell me who won. I then tell the user they won via email, update the user, and insert a record in a table.
What could be some route(s) to achieve this? Does a route necessarily have to map back to a user or to a class? For example
HTTP POST /users/1/lottery
Does this mean user 1 wins the lottery? Even though there is no lottery collection beneath user? Lottery isn't even a class in my application. There is no such concept as /users/1/lottery/1, etc. Do all routes have to map back to classes? In this action I can do all 3 things above (email, update user, and add the row in the LotteryWinners table all in 1 shot).
Or is the REST approach to say two calls need to occur:
1. HTTP PUT /users/1
Update with the IsProcessedForLottery value set to true. The only problem here is I would need to pass the ENTIRE user object represented as JSON which seems overkill for JUST ONE FIELD I need to update. Followed by ANOTHER call to
2. HTTP POST /lottery/
To insert a new row in lottery winners with a user ID of 1 and then send the email (I can pass some JSON over that contains the user ID or user object)
Any ideas?
Thanks so much everyone, appreciate the time you're taking in reading this :)
Upvotes: 0
Views: 34
Reputation: 652
In this scenario, I think a good REST API design is like this:
HTTP POST /users/1?params
You can set the fields you want to update in the params.
To design a REST API, it is all for yourself how to design, there is no strict rules or syntax, but we should make it precise and easy to understand at a glance. In your proposed two options, there are some problems:
Option 1:
For a REST API Route, there are always good-categorized entities in the main URL, in this case, you just want to update a user and the lottery is is just an attribute of the user not an entity, so it should not be in the main URL.
Option 2:
As you said it is a little too complicated to solve this issue.
Any way, feel free to design a REST API, there is no golden rules for it, you can reference some well-known APIs like AWS.
Here is the S3 API of it: http://docs.aws.amazon.com/AmazonS3/latest/API/APIRest.html
Upvotes: 1