Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is aggregation function bad idea for RESTful?

As title: Why is aggregation function bad idea for RESTful? Although I know CRUD is good for RESTful.

For example, the resource is 'employee', and client needs to retrive sum of total 'salary' of all employees. Shouldn't RESTful service provide such sum function?

Further question: if aggregation function is bad for RESTful, how can a client get sum of total salary? To retrieve all 'employee' records and sum up itself?

like image 384
卢声远 Shengyuan Lu Avatar asked Jul 17 '13 13:07

卢声远 Shengyuan Lu


People also ask

What makes an API RESTful?

A RESTful API is an architectural style for an application program interface (API) that uses HTTP requests to access and use data. That data can be used to GET, PUT, POST and DELETE data types, which refers to the reading, updating, creating and deleting of operations concerning resources.

Which data format does REST API use?

The REST API supports the following data formats: application/json. application/json indicates JavaScript Object Notation (JSON) and is used for most of the resources. application/xml indicates eXtensible Markup Language (XML) and is used for selected resources.


1 Answers

I wouldn't say that exposing the results of operations (i.e. an aggregation function) as resources in REST has to be considered as bad.

From the RESTful Webservices Cookbook (O'Reilly):

One of the most common perceptions of REST’s architectural constraints is that they only apply to resources that are “things” or “entities” in the application domain. Although this may be true in a number of cases, scenarios that involve processing functions challenge that perception.

It is quite common to treat a processing function as a resource, and use HTTP GET to fetch a representation containing the output of the processing function. You can also use query parameters to supply inputs to the processing function.

So why not provide the result of a salary aggregation of a number of employees as a resource, i.e. like this:

GET /employees/aggregation?data=salary

or more general:

GET /aggregator?resource=employee&data=salary

You could even filter the group of employees for which the salaries should be aggregated, i.e. like this:

GET /employees/aggregation?data=salary&divison=sales
like image 186
benjiman Avatar answered Oct 09 '22 12:10

benjiman