Search code examples
restaveragedashboard

REST API for processed data


It may be opinionated and not belong here, but I don't seem to find any info about this.

I have a 'sales' resource so I can GET, POST, PUT, DELETE sales of my store.

I want to do a dashboard with information about those sales: e.g the average sales per day of the last month.

Since REST is resource-oriented, that means I have to manually retrieve all sales of the last month and calculate the average per day on client using GET /sales?sale_date>=...? That doesn't seem optimal, since I could have thousands of sales in that period of time.

Also, I don't think REST can allow a URL like GET /sales/average-per-day-last-month. What is the alternative of doing this?


Solution

  • I don't think REST can allow a URL like GET /sales/average-per-day-last-month

    Change your thinking - that's a perfectly satisfactory URL for a perfectly satisfactory resource.

    Any information that can be named can be a resource -- Fielding, 2000

    "Any information that can be named" of course includes "any sales report that you can imagine".

    GET /sales/average-per-day-last-month HTTP/x.y
    

    That's perfect. Depending on your needs, you might also want to have other resources that are the "average-per-day" report for specific months.

    GET /sales/2021-02/average-per-day HTTP/x.y
    

    REST doesn't constrain your resource model at all; and it doesn't constrain your resource identifier design beyond expecting conformance to the production rules described by RFC 3986.