0

I am designing an API where-

1. There can be multiple devices under a gateway.
2. There are multiple sensors on every device.

GET /devices/d1/sensors/s1 returns the status of sensor s1 on device d1. Moving forward, I need to allow querying sensors across multiple devices.

Something like- GET /devices/d1,d2,d7,d30,d45,d500/sensors/s1,s5 would not scale as I'd be dealing with a lot of devices+sensors and increasing the URL length linearly would be bad design.

How can I approach this more elegantly?

1
  • Are sensors available on multiple devices with the same id? If that is the case don't you need some kind of sensor type? Generally what you want is not great for this kind of matrix things. Some times it helps to put a resource in between like a group of sensors maybe. Depends on your data: /sensorsgroups/s1 could get sensor s1 from all devices for example. Commented Oct 16, 2014 at 6:35

1 Answer 1

3

One of the core ideas behind REST is that each URL provides a representation for a single resource (although that resource can be a collection that gives information about other resources).

If you want to get information about multiple sensors, where you don't know/care which device each sensor is connected to, the canonical way is to provide a top-level sensors resource: GET /sensors. This would give you all sensors in the system. If you wan't to restrict the list in some way, you use query parameters:

GET /sensors?device=d1,d2,d7,d30,d45,d500&type=temperature

would give you the temperature sensors connected to the listed devices.

If you do care which device each sensor is connected to, there is no way around performing multiple requests, one for each device.

1
  • »One of the core ideas behind REST is that each URL represents a single resource« That reads like "one URI=one Ressource" I would rather say, that each URI is one representation of a ressource. So, that /sensors?device=d1&type=temperature and /devices/d1/sensors/temperature refer to the same ressource. Commented Oct 16, 2014 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.