Search code examples
restapi-design

REST API design for a Alert system


Im new to REST API design and I was wondering how one goes about designing for a problem defined as below. I have also outlined what I have in mind currently but obviously I see many problems with it

  • A Cluster has Alerts
  • Alerts are instances of Alert_Type
  • An Alert_Type can be assigned to Cluster(s). In this case the Cluster is Registered for Alerts of Alert_Type
  • An Alert_Type can also be assigned to no Cluster
  • When an Alert of instance Alert_Type occurs in a Cluster, some action occurs if and only if Alert_Type is registered with a Cluster and Alert_Type exists

For purposes of this question I am concerned with the REST API design and routes for the Alert_Type Object and Cluster Object

These are a subset of REST actions I can perform on Cluster Object:

POST: Create a new Cluster Object. In the object a field called registered_alerts could contain the Alert_Type objects registered for this Cluster

DELETE: Delete an existing Cluster Object

GET (on /alert_types) : Will return a list of Alert_Type objects registered with this Cluster

These are a subset of REST actions I can perform on Alert_Type Object:

POST: Create a new Alert_Type object.

DELETE: Delete an existing Alert_Type object

GET: Get an existing Alert_Type object

Now my questions:

  1. Obviously I need to tie the Cluster and Alert_Type objects - I know its not good design to duplicate the Alert_Type objects in the Cluster objects. So maybe I should generate an id for each Alert_Type object and require that the Cluster POST send an id instead? And then maintain a list of id's in the Cluster Object
  2. If the Cluster POST has a bunch of Alert_Type ID's some that are valid and some that are not, how do I handle that?
  3. The unique id for an Alert_Type must be generated at the server, and returned back in the response of the POST of Alert_Type - whats the correct way to do this in a REST compatible way ?
  4. Do you think it would be required to even hold the Cluster ID's in the Alert_Type objects? The only reason i can see the need for this is when an Alert_Type is deleted - and then that should update the Cluster objects that are watching for this Alert_Type

Solution

  • I try to give you a response for every point:

    1. Yes you should use the id in the Cluster object.
    2. You must return an error end don't save anything.
    3. I should return the id of the created Alert_Type in the POST response (eg. json {'id': 'theId'}).
    4. It depends on the db implementation: if you can simply retrive the Cluster connect to the deleting Alert_type i suggest you to not keep inverse connection because if you do that you have one more field to manage.

    Hope can i help.