Hi,
It's been a little time since I did any real API hacking but I'll give
some feedback. :-)
On Fri, Feb 27, 2015 at 10:00 AM, Adrian Lynch
<
adrian...@concreteplatform.com> wrote:
> Create a filter resource under the collection you're attempting to filter
> (users in this case):
An interesting approach to filters. I'll comment on it further down.
I have usually used query parameters for filtering and using a
specifically named query parameter, e.g. "filter-by", and a few
syntactical conventions for specifying filtering criteria:
http://api.example.org/acollectionofstuff?filter-by=size=12,color=blue
This is to be understood as filter out all items with size=12 and color=blue.
Refining the filter is simply achieved by adding more filter query
parameters, and using some simple syntax for supporting boolean
conditions:
http://api.example.org/acollectionofstuff?filter-by=size=~12,color=|blue;green
This is to be understood as filter out all items with size /=12 and
color=blue or color=green.
The filter parameter syntax could also include support for numeric
intervals and wild cards or regexps in string values.
Pros:
* Easy to understand - I think!
* Easy to test and play with in web browser.
* Caching (of filter and the resulting collection) on server side is
feasible for reasonably sized filtered collections (presumably
implemented with some time-to-live policy).
* Can easily be combined with support for sorting the filtered
collection, e.g. with a query parameter like "sort-by".
Cons:
* The query parameter "filter-by" and its filter-syntax is
"out-of-band" information. And so is "sort-by" as well.
* HTTP cache does not work in proxies.
As for Nicks question on where to do the filtering, once the
(representation of the) filtered collection is with the client, it
seems natural to me that the client does refined filtering on that
set, without involving the server.
Adrians proposal of elevating filters to resources is interesting,
especially for cases with more complex filters.
Pros:
* No "out-of-band" information with specially named query parameters
or syntax.
* Can easily handle complex filters.
* Resulting filtered collections can also be provided as separate
resources (presumably implemented with some time-to-live policy).
* A more elaborate filter syntax can be developed than is possible
with query parameters.
* HTTP cache works in proxies.
Cons:
* Can't test and play with in web browser.
* Need for specifically implementing support for filter resources on
all collection resources for which you want to have filters. In
Adrians proposal he has dedicated filters for "users".
* No support for sorting the resulting collection. This is okay if
we don't want the server to do the sorting.
Another approach with filters as resources would be to:
a) have them independent from specific collection resources and use a
generic filter syntax for the collection media types you support.
b) allow filters to be applied to any collection resource by a HTTP
header, e.g. filtering the collection resource "/users" with filter
"/filters/567":
GET /users
Headers:
X-filter-by: /filters/567
Filters referring to keys/attributes etc that don't exist in a
specific collection resource should simply be ignored when applied to
that collection resource (Postel's law).
/Paul
--
Paul Cohen
www.seibostudios.se
mobile: +46 730 787 035
e-mail:
paul....@seibostudios.se