3

I'm very new to API implementation from ground up and I needed some advice on what the standard or the best approach in my API structure is.

Currently my implementation includes nested routers (drf-nested-routers package) such as

"www.thissite.com/store/21/products/1/"

Now as I dig deeper in django I've uncovered that there are filters that allow me to do the exact same operation above with a little less code like this

"www.thissite.com/products/?store__id=21&id=1"

My question is which one is best practice and why?

2 Answers2

1

Both are best practices, since REST does not constrain URI design. I call www.thissite.com/store/21/products/1/ hierarchical URI design and www.thissite.com/products/?store__id=21&id=1 flat URI design. I like the flat design better, but that is just my personal taste. If you need both store-id and product-id in order to identify a product then these URIs are okay and any URIs are okay with these variables, so for example x/y/z/:pid/q/r/s/:sid, etc... By REST the URI (template) creation is the responsibility of the service and the clients consumes only the URIs it gets from the service in forms of hyperlinks. So from REST client perspective the URI structure does not matter. We tend to design nice URIs only to keep the REST service routing logic clear.

inf3rno
  • 24,976
  • 11
  • 115
  • 197
0

If a product is always related to a store (which seems to be the case, given the names), then it's considered a best practice for REST, to maintain an hierarchical structure by making products a subresource of stores. Thus I would suggest you to follow the first aforementioned approach.

The filtering should be used to filter resources based on some internal characteristics (e.g class attributes), not based on relations to other resources.

stelios
  • 2,679
  • 5
  • 31
  • 41