How to GET with a provided Range?


#1

I have not found any discussion regarding filtering across a range. For instance, what if you need to GET a list of resources between a start and end date?

I propose the following:

?range[startDate,endDate]=2015-05-01,2015-06-01

Any feedback would be appreciated.


#2

I solved this condition using filters in this way:

  • ?filter[pricing_min]=100
  • ?filter[pricing_max]=200
  • ?filter[pricing_min]=100&filter[pricing_max]=200

In this way filters and values are parsed following the standard


#3

I appreciate the feedback.

That is a good use of filters. I think I’ll take your approach.

Worth noting, if I apply your example to my use case, I end up with:

?filter[startDate_min]=2015-05-01&filter[endDate_max]=2015-06-01

With our API, I enforce the standard that the key needs to be one that is present on the resource attributes. The only issue one could raise with you approach is that the assumption becomes the resource attribute precedes the underscore [startDate_min].

Basically, the following is the equivalent to an opening range.

_min]=

Overall, I like it.


#4

Glad to help :slight_smile:

I wanted to follow your same condition:

key needs to be one that is present on the resource attributes

but I though that was easier to break this condition in favour of a more linear approach to filters.

This because I implemented a parameters-applier module which applies a query given the attribute. By default it’s a where() clause with equality operator to the attribute but, if specified by an input schema, the default query for the given attribute is replaced with the custom one.
In this way I just had to write a where() clause with less-than/greater-then operator for the xyz_min/xyz_max attributes and the condition is automatically applied to the main query.