I'm fairly new to REST web services and very used to RPC. I know the advantages of REST by reading several posts like this one.
I am developing the server in django using django-rest-framework.
Although have this question (or questions):
I have this model:
class Poll(models.Model):
questionString = models.CharField(max_length=500, blank=True)
timeToAnswer = models.IntegerField(default=30)
startDate = models.DateTimeField(null=True, db_column='startDate', blank=True)
token = models.CharField(max_length=20, blank=True, unique=True)
class PollAggregator(models.Model):
name = models.CharField(max_length=135)
description = models.CharField(max_length=500, blank=True)
votersToken = models.CharField(max_length=20, null=True, blank=True)
class PollPollAggregatorRel(models.Model):
pollAggregator = models.ForeignKey(PollAggregator, null=True, db_column='pollAggregatorId', blank=True)
poll = models.ForeignKey(Poll, null=True, db_column='pollId', blank=True)
So I can have a single poll or I can have a bunch of polls aggregated in a poll aggregator (i.e. room).
So I created the rest calls: pollList, pollDetail, pollAggregatorList, pollAggregatorDetail. But I have problems to design for PollPollAgregatorRel. Of course I can have PollPollAgregatorRelList and PollPollAgregatorRelDetail and make the normal post, get, update, delete. So if I want to make a new relation between a poll and poll aggregator in REST style, I do:
- Check if PollPollAgregator (list) exists with the poll id with a get and filtered by pollId
- If so, I update this item to have my new pollAggregator id
- If not I create a new PollPollAgregator with a post
My first question is is there any easier and simpler way to do this?
If I use a RPC like web service I do something like:
- Relate poll with pollAggregator and use a get_or_create for PollPollAggregatorRel. So I update or create a new PollPollAggregatorRel object.
So using RPC like, the client uses only one call versus REST that needs to call 2 times. In this case seems to be much simpler to use RPC for both server and client side.
Second question is: Is it bad practice to use both REST and RPC in the same API?