API V2 #3642
Replies: 5 comments 11 replies
-
|
Very cool to see work on a new API version. I am looking forward to this 🙂 Here are some things that crossed my mind when reading this. I am not an experienced API programmer (and an even less experienced API developer 😉) though, so please feel free to educate me on obvious errors and false assumptions I make. HTTP verbsI am a big fan of using different verbs for creating and updating entities. I think this nicely prevents accidentally overwriting existing experiments etc. If I understood correctly, creating and updating an experiment would work as follows:
Did I get that right? EndpointsToggles vs. explicitly setting statesWhat would you propose for changing boolean states of entities? Update: After reading up on this, toggles seem to be inherently unRESTful, since they are not idempotent. So probably using just one |
Beta Was this translation helpful? Give feedback.
-
|
API V2 seems to be a great chance to also split the uploads table in two parts, one for items and one for experiments as outlined in #3393 if that is still desirable. The current API requires all upload ids in one table in order to That lets me think if it should be possible to request only a filed like body or next_step via |
Beta Was this translation helpful? Give feedback.
-
|
So the APIv2 rework is going well, currently here is how it looks:
Creating stuff:POST which replies a 201 (Created) and the location of the created entity in the Some creative actions like Duplicate also goes through POST with an "action: duplicate" parameter. Reading stuff:GET which replies in JSON. You can use the Updating stuffPATCH which replies with the updated entry (complete). You can patch one or several columns at the same time. The default action of patch is update, but some other actions are possible like "lock" or "timestamp". For that you send a patch with "action: timestamp" in the body for instance. Deleting stuffDELETE which replies 204. Endpoints:Might be easier to show current code: elabftw/src/controllers/Apiv2Controller.php Lines 189 to 252 in a6cf80d Basically you have more endpoints than in v1, and you have subendpoints/submodels. So to create a comment you can POST experiments/42/comments, and update it with PATCH experiments/42/comments/23 It's not finished but the heavy work has been done already :) |
Beta Was this translation helpful? Give feedback.
-
|
Documentation is taking shape slowly but surely. Swagger/Openapi is pretty great TBH. |
Beta Was this translation helpful? Give feedback.
-
|
Nearly complete documentation can be found here: https://doc.elabftw.net/api/v2/ |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When I created the API v1 back in the day, it was very much hectic and grew organically. It is now time for a V2 with a better design to stick to modern/best practices when it comes to APIs.
The idea would be to fully use all HTTP verbs properly, and to have a clean interface that actually makes sense. To be honest the V1 is not too shabby, but there is room for improvements and there are many actions that are unavailable.
The end goal would be to have the frontend interface actually use the API in XHR requests directly. This would be nice.
So instead of directly starting coding as I often do. This time I'll try to think before I do. And share it publicly because your comments will be welcome.
Design for API V2
OUTDATED: see updated design here: #3642 (comment)
Creating stuff
request: POST /experiments
response: 201 Created with the URL to new entity in Location header
Reading stuff
This is similar to v1.
request: GET /items/12
response (you'd get the full entity as json)
{ "title": "entity title", "body": "blah..." }Without an id you get an array of items (like v1).
Updating stuff
PATCH /experiments/12
{ "title": "new title" }And the response would be the full entity. Using PUT you'll need to provide a full entity.
Delete stuff
request: DELETE /templates/12
response: 204
Errors
{ "code": 12434, "message": "Something bad", "description": "rasiteriuse truiste ", "errors": [ { "code": 444, "field": "date", "message": "Incorrect date format", } ], }Doing an action
Like timestamping an entity
POST /experiments/123/timestamps
201 Created
Endpoints
Similar to v1 with experiments, items, experiments_templates, items_types, but with new ones like users, teams, etc....
Actions would be CRUD for all and some would get more like comments and lock toggle or archive actions.
Conclusion
There is no "one true way" to design an api, and every approach has shortcomings. But I think this looks pretty neat, what do you think?
Beta Was this translation helpful? Give feedback.
All reactions