REST API URI for entities with two different keys -


i must design api manage document entity: originality of entity can have 2 different ids:

  • id1 (number, i.e. 1234)
  • id2 (number, i.e. 89)

for each document, 1 , 1 id available (id1 or id2, not both)

usually solve issue using query parameters perform kind of "search" feature:

get /documents?id1=1234 /documents?id2=89 

but works if there no sub-entity...

let's want authors of documents :

get /documents/1234/authors 

impossible because can't know type of id get: id1 or id2 ?

get /documents/authors?id1=1234 

not rest think because id1 refers "author" entity, not "document" anymore...

get /id1-documents/1234/authors /id2-documents/1234/authors 

then create 2 uris return same entity (/author) not rest compliant.

get /documents/id1=1234/authors /documents/id2=89/authors 

it looks composite key created api, has no "backend" meaning. me sounds strange create "composite" key on fly.

get /document-authors?id1=1234 /document-authors?id2=89 

in case lose notion of tree... end api contains root entities.

do see alternative ? 1 looks best ?

thank much.

it seems me you're conflating 2 different resources here - documents , authors. document has relationship author, should separate resources because authors have existence individual document. in mind need ask whether clients searching authors or documents. if it's authors, should querying authors api rather documents api.

e.g.for authors of documents id1 89 or id1 1234 or id2 4444 might query this...

get /authors?docid1=89&docid1=1234&docid2=4444

that should return list of author representations. if people care documents themselves, author representations contain links documents.

alternatively, if you're looking documents should querying directly...

get /documents?id1=89&id1=1234&id2=4444

what you're modelling sub-resource isn't really subresource. it's relationship between 2 independent resources , should modelled set of links. each document returned documents api should contain set of authors links (if people care authors) , vice versa authors documents.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -