Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST: accessing members of a collection through multiple ids

I have a REST service handling video servers on a network.

Each video server can be identified in several ways: by its serial number, by its name, or by its machine number.

For returning a collection of all servers available on my network, things are pretty much simple: I have defined the following route:

[Route("/servers", "GET")]

and the following request class:

public class ServerCollection : IReturn<List<ServerDto>>
{
    ...
}

Now, I'd like to return a specific server from my collection, identifying it either by its serial number, by its machine name, or its machine number.

For doing so, I have defined the following routes:

[Route("/servers/{SerialNumber}", "GET")]
[Route("/servers/machinenumbers/{MachineNumber}", "GET")]
[Route("/servers/machinenames/{MachineName}", "GET")]

and the following request class:

public class Server : IReturn<ServerDto>
{
    public uint SerialNumber { get; set; }
    public uint MachineNumber { get; set; }
    public string MachineName { get; set; }
}

So, I can access my server collection through:

GET /servers

and get a specific server using either:

GET /servers/3
GET /servers/machinenumbers/42
GET /servers/machinenames/supercalifragilisticexpialidocious

Is that the right way to proceed? I have the feeling that this is not very RESTful. Should I consider this as a search in my collection instead of using "artificial" resources?

like image 733
David Brabant Avatar asked Jun 23 '13 11:06

David Brabant


1 Answers

I would represent the service in one way that is always unique (serial-number may be correct).

For the query I would do something like /servers/?name=[name] or /server/?id=[id] or just /servers/[serial] (if you want to use the serial number directly). When requesting the name or id you should change the url in the request to servers/[serial] to keep the url unique.

like image 81
Avi Avatar answered Oct 21 '22 07:10

Avi