Re:Use links not keys to represent relationships in APIs
by rich-tea on Sunday May 12, 2019 @02:00AM
(#19890422)
There shouldn't be separate endpoints that take an owner's ID. That's bad design. The owner endpoint should contain a list of invoices, ie. links to the invoice endpoint.
[ Parent
| on HN ]
Re:Use links not keys to represent relationships in APIs
by tastroder on Sunday May 12, 2019 @04:00AM
(#19890745)
Maybe the data model is used for two use cases, one where you primarily access owner entities and one where you primarily access invoices and occasionally do something with their owner. Just because it does not fit some weird, hypothetical prototypical API it's not inherently "bad design". APIs aren't ER models, sure, they're supposed to make sense but they're also supposed to help their consumers perform a task.
And let's not kid ourselves, much of our world runs on APIs that would really deserve the "bad design" handwave, end of the day that's often an aesthetic question.
Re:Use links not keys to represent relationships in APIs
by viraptor on Sunday May 12, 2019 @02:21AM
(#19890486)
But the invoice endpoint is likely going to be something like "/customer/(id)/invoice/...". So what did we gain from getting it from the customer description first? (vs getting the customer id from the response and the link pattern from the docs)
Re:Use links not keys to represent relationships in APIs
by rich-tea on Sunday May 12, 2019 @02:27AM
(#19890501)
You gained the ability to find the invoice. There is no need to have customer id in the invoice url. Just /invoice/invoice_id is enough.
Re:Use links not keys to represent relationships in APIs
by viraptor on Sunday May 12, 2019 @03:06AM
(#19890595)
I meant a whole tree of methods. Sure, you can get specific invoices from `/invoice/id`, but you probably still want `/customer/id/invoices` or similar for searching through them. You could use extra parameters for `/invoice/...`, but I think often it's nicer to namespace that. (use case dependent)
Re:Use links not keys to represent relationships in APIs
by cs02rm0 on Sunday May 12, 2019 @03:52AM
(#19890721)
If an invoice id is unique across all customers. Which it probably should be, but other ids might reasonably not be.
Re:Use links not keys to represent relationships in APIs
by jeltz on Sunday May 12, 2019 @09:05AM
(#19891625)
Do I misunderstand you or do you suggest that the customer object should include the list of all invoices? That does not scale. Imagine if it is something more common like transactions where a user can have made thousands or tens of thousands.
Re:Use links not keys to represent relationships in APIs
by randallsquared on Sunday May 12, 2019 @09:28AM
(#19891751)
An API should usually prioritize ease of development over faithfully exposing database or architecture internals.
However, you can have it both ways: in the customer response, use
"invoices": "/customer/7sn2J/invoices"
and have the server 307 redirect to "/invoices?customer=7sn2J", or whatever you prefer.
Re:Use links not keys to represent relationships in APIs
by rich-tea on Monday May 13, 2019 @07:54AM
(#19898332)
It can contain a link to the list of all invoices. So you could have /customer/<cid>/invoice which lists all invoices for the customer which are actually just links of the form /customer/<cid>/invoice/<iid>