RESTful API for entities with two independent primary keys
I have the following entity:
<car>
<carID>7</carID>
<...>...</...>
<externalCarID>23890212</externalCarID>
</car>
The problem is that carID
both externalCarID
are independent primary keys that are used by / for different systems, and API clients need to access the object car
as with carID
xor externalCarID
.
Both keys are int and they don't use disjoint sets:
carID(7) != externalCarID(7)
I thought about the following solutions:
- Accessing it with
/restapi/car/7
and/restapi/externalcar/23890212
- Use parameters like. for example
/restapi/car/7?type=regular
and/restapi/car/23890212?type=ext
- Send information somewhere in the header - but where?
Some hints on how to handle this or feedback on my solutions, preferably with reference to the REST / HTTP specs, would be great!
Background on primary keys:
Let's say that our account systems need carID
and our parent company that controls the system needs externalCarID
. I don't like it at all, but its the system works and I cannot change it right now.
source to share
I would recommend that you select one ID as the "real primary" and access it as suggested:
/restapi/car/7
If you are managing one of the IDs, I would suggest using that as "real primary".
Another id, even if it should have a unique id, using a query parameter:
/restapi/car?extid=23384
There are several similar questions that might help:
Different RESTful views of the same resource
The last thing to consider is redirect your url for "extid" to the canonical URL for that resource.
source to share
Of the three options presented, the first one looks the best to me. Especially considering that each external system will only use one of the base urls. For the second option, you can use /restapi/car/?extid=23384
which looks cleaner. In the third case, in my opinion, headers are more about metadata, so using them not to manipulate the context of the view, but to completely change the object returned by the URL, is not a clean option. Personally, depending on your performance requirements, I would either go for the first solution or the usual one /rastapi/car/7
for id
I would consider the primary on my system and restapi/car/?extid=1234
for the secondary unique key, perhaps redirecting the GET to the canonical url if it doesn't affect too much performance.
source to share