matomo-org / matomo

Empowering People Ethically with the leading open source alternative to Google Analytics that gives you full control over your data. Matomo lets you easily collect data from websites & apps and visualise this data and extract insights. Privacy is built-in. Liberating Web Analytics. Star us on Github? +1. And we love Pull Requests!
https://matomo.org/
GNU General Public License v3.0
19.29k stars 2.59k forks source link

Build a RESTful API for Piwik #6000

Open tsteur opened 9 years ago

tsteur commented 9 years ago

The idea for this would be to write a new plugin on top of the current API. This would bring the advantage that the old API stays untouched / backwards compatible (and maybe we could deprecate it in the next major version 3.0).

Since we want to separate the back end from the front end in the long term having a RESTful API becomes more important.

When we talked about this last week we figured out one of the challenges would be rewriting the URL from /api/... to the actual plugin file or index.php because not all users are using Apache (easy with .htaccess if rewrite is enabled). Unless we do not find a better solution people will have to modify their server config in case they want to use it.

czolnowski commented 9 years ago

So module and action should placed like /api/:module/:action and rest of required parameters (most of reports required idSite, date, period) should be set as GET parameters after "?" ?

tsteur commented 9 years ago

It wouldn't be exactly :module/:action I reckon. It be probably rather something like /api/users/ . Haven't thought about it so much yet

diosmosis commented 9 years ago

RESTful APIs are built around resources so /api/:module/:action would still be RPC. I think something like /users/:id_or_name, /reports/visitsbybrowser or /visits/reports/bybrowser (ie, /:entity/reports/:dimension where reports is a resource related to visits) would work. /api seems unnecessary to me.

mattab commented 9 years ago

Maybe we can push this for Piwik 3.0. It's related to other powerful ideas:

mattab commented 9 years ago

I'm watching this nice writeup video about REST+JSON API design (changed speed to 1.5x :+1: )

there are two types of resources

Behavior is specified using HTTP methods: GET, PUT, POST, DELETE, HEAD

How to choose the API version?

other best practises mentioned in the video:

tsteur commented 9 years ago

If someone is not aware of this topic there is also this GitHub project providing a summary and some links to more information: https://github.com/WhiteHouse/api-standards

mattab commented 7 years ago

from https://github.com/piwik/piwik/issues/6158

The link in the title is a list of HTTP API guidelines used by Heroku. It may have many useful ideas that can be implemented in Piwik.

Foundations
    Require TLS
    Version with Accepts header
    Support caching with Etags
    Trace requests with Request-Ids
    Paginate with ranges
Requests
    Return appropriate status codes
    Provide full resources where available
    Accept serialized JSON in request bodies
    Use consistent path formats
    Downcase paths and attributes
    Support non-id dereferencing for convenience
    Minimize path nesting
Responses
    Provide resource (UU)IDs
    Provide standard timestamps
    Use UTC times formatted in ISO8601
    Nest foreign key relations
    Generate structured errors
    Show rate limit status
    Keep JSON minified in all responses
Artifacts
    Provide machine-readable JSON schema
    Provide human-readable docs
    Provide executable examples
    Describe stability