rouble
rouble

Reputation: 18221

RESTFul way to reference resource with unique fields

One of the requirements for our REST interface is that each resource be identifiable by unique fields (aside from the primary identifier). The reason for this is that we want to be able to handle bulk importing data - in which case the client can't know the system generated primary identifiers.

This means we have to be able to reference our resources by unique fields. Using a primary key our read requests look like this:

GET example.com/rest/customers/1

and to get orders related to that customer

GET example.com/rest/customers/1/orders

Now, lets assume two fields in customer identify it uniquely, name ("foo") and businessId ("bar"). Given that, I came up with the following URI to get the orders for this customer:

GET example.com/someotherpath/customers/foo,bar/orders

But I don't like that I have a different path to identify that this is a resource being accessed via unique fields. How would you structure the above query in a RESTful way using unique fields instead of the primary key?

Further, an order looks like this:

{
    <SNIP>
    "orderId" : "42"
    "_links": {
        "customer": {
            "href" : "rest/customers/1"
            "key": [ "foo", "bar" ]
        }
    },
}

Any issues with allowing client to interchangeably specify href OR key when communicating with the interface?

Upvotes: 0

Views: 166

Answers (1)

Eric Stein
Eric Stein

Reputation: 13682

For the first bit, I just wouldn't do it. If a customer has a unique id (and they should), I wouldn't allow end users to specify N other fields that happen to also uniquely identify the customer. It's messy for the user (which field goes first?) and also messy for you on the back end.

For the second bit, the issue is: what happens when they specify both? Which takes precedence? Are they going to remember? Do you want to have to support both? It's generally a good idea to only allow one way to do any particular thing if you can get away with it.

Upvotes: 1

Related Questions