Question

Let us consider we have an api for zoos such that ../api/zoos which goes all the way to accessing individual animals in a given zoo such as ../api/zoos/123/elephants/234 i.e there are well defined apis for each animal type.
    Now if we want to query at the super type ( abstract class Animal where class Elephant extends Animal) , is exposing an api of the sort ../api/zoos/123/animals?type="mammal"&legs=4 for such meta-queries a good idea ?
    What makes me think twice about such an approach is while super-type api is practically useful for metaqueries across the concrete types but this essentially is a read-only query, also from the extensibility point of view say between the super type Animal and concrete type Elephant there can be many other types like Mammal, FourLegged etc. which can be individually queried then a call needs to be taken on whether or not read-only apis needs to be exposed on these as well or we have a type parameter on animal query that does the job. Please suggest.

Was it helpful?

Solution

Do you have seperate IDs for each species of animal?
I mean, is there an Elephant ID=4 and a Tiger ID=4?

If not, I would structure this as /zoos/123/animals/234

You could then do /zoos/123/animals?species=elephant to list all elephants, and get back an array of links with IDs that might look somewhat random: { /zoos/123/animals/5, /zoos/123/animals/19, /zoos/123/animals/34, /zoos/123/animals/35, /zoos/123/animals/72 }

If you do have conflicting IDs across species, then I would suggest creating a new resource mapping of Animal IDs to Species IDs:

+----------------------------+
|animal_id|species|species_id|
+----------------------------+
|       1 |  duck |        1 |  => the first duck is /animals/1
|       2 |  frog |        1 |  => the first frog is /animals/2
+----------------------------+

Then you can still use the URL structure /zoos/123/animals/234. You would have no need to refer to the frog as /frogs/1 as a page such as /animals?order=anura&page=1&limit=10 would have given you a link directly to /animals/2

In summary, IDs belong in your superclass

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top