REST API: How we implement resource handlers for PATCH req:

Valid Actions performed on the Invitation Entity
Valid Actions performed on the Invitation Entity.
PATCH /invitations/{id} : Update the Invitation Entity

Actions

There are 3 actions associated with the Invitation entity, End user can either Accept, Reject or Request for a change in the current invitation. Frontend application will make the appropriate HTTP API request to the backend to update the status of the “ Invitation ” entity.

Need for Consistency

  1. Simple HTTP API contract (request body) across every user action.
  2. Backend must easily identify the type of action performed and able to validate values sent from the frontend.
  3. Able to add new action against the “ Invitation ” entity with less efforts.
  4. Backend must be able to raise exceptions or success response through common channel.

Request Body

Below json shows the request body which we follow across every Entity and its corresponding PATCH HTTP API 💂.

{
"action": "accept|reject|request_change",
"action_arguments": {
"id": 123,
"comments": "Requesting for Change",
"user_email": "change@local.com"
}
}

Backend Resource Handler

Below is a Pseudo code, on how we implement the resource handler for the “ PATCH ” HTTP request, which accepts only the above sent body 😆

Benefits

  1. Above approach shows a simple, clean and re-usable approach of writing resource handlers for PATCH HTTP requests.
  2. Request validation can be done in the respective actionHandler for that action.
  3. Adding a new action is easy as most of the boiler plate is ready, only thing remains adding a new key=>value pair in the action resolver array and defining the business logic of the function.
  4. API Resource Handlers are clean and follows its basic responsibility which is to understand the request, delegate to appropriate handler and sending back the response.

Final Notes

We find above to be a better implementation approach in defining the resource handlers for the PATCH HTTP requests.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vishwa Chikate

Vishwa Chikate

Drupal Lead works @ Srijan Technologies