AndrewShmig
AndrewShmig

Reputation: 4923

What is the correct way in RESTful API paradigm to describe resource statistics?

Lets assume that we have some sort of users collection. Each user (single resource) has some sort of stats - number of comments, number of calls, number of anything. And each users collection may also have some sort of stats - global stats.

These stats can be anything from number of comments to something more complicated.

Currently I am thinking of two main ways of "integrating" stats to my RESTful API:

  1. For each single resource user add special computed field - balance, mediumAgeOfMaleFriends etc. When performing GET request to user resource I'll loop for each field in query parameter fields and calculate corresponding values. Example: .../user/123?fields=balance,name,email,mediumAgeOfFemaleFriends
  2. Under each resource create subresource called reports (or something like that), create report resource templates (with predefined configurations) and perform same GET requests like so: .../users/123/reports/1?from=...&to=...

But these two ways don't look like true RESTful ways.

Upvotes: 0

Views: 799

Answers (1)

Mo A
Mo A

Reputation: 727

I don't think there's a RESTful convention per-se for stats or reports. Both your options seem reasonable to me (leaning more towards Option 2 though).

Another alternative is to have a root resource for 'reports', e.g. /reports/user/123. This might come in handy in the future if you decide to extend reports to another resource, other than 'user'.

You can also have a look at HubSpots Analytics - Reports API for inspiration.

Upvotes: 1

Related Questions