Reputation: 2089
So I have a question regarding REST API design:
I have multiple REST APIs that have specific behaviour, e.g. update vehicle status (e.g. available, on-hire, maintenance, retired) which does some validation to check to see if vehicle currently has any hire bookings before changing status to say retired etc. Another one is to log any instances of damage to the car once a vehicle has been returned from hire, this just records general comments on vehicle condition. And another one which retires a vehicle from use (e.g.e end of life), this duplicates the logic and actions of the other two APIs as part of this RETIRE API.
However I want to prevent the duplication of code/logic by just changing the RETIRE API to call the individual APIs as part of the call to this API, this will help when I need to change the logic of the other APIs and prevent me from the duplicating this logic in the RETIRE api.
So within the current API design there is error handling, e.g. if a specific action causes an error then the API will roll back the transaction and display an error to the user with why it has failed, otherwise commit the changed data. This works great.
However if I call each API within this RETIRE API how can I handle the errors, e.g. if the RETIRE API first calls the 'DAMAGE API' to record any damage and that succeeds it will commit the data, but then the 'VEHICLE STATUS' API fails it would send a response to the user with the relevant error, but this is where the problem is, the damage API has already ran and succeeded so this data is already saved... So if the user tries again and this time everything succeeds I will have duplicate data in the 'damage' section.
So how can I only commit the data for all of the APIs once they have all returned with a success? Or is it better to have the 3 APIs that will still be independent with each other but maybe create functions and call these functions within each API so if I change the logic of a given action they will all follow suit?
Sorry for the story but I just wanted to help get my problem across to you all.
Thanks in advance
Upvotes: 0
Views: 981
Reputation: 59174
Your description indicates that you want the RETIRE API to use some of the logic in the DAMAGE API, but not all of it... So factor out the stuff you want to reuse into an internal method that can be called by both APIs.
Upvotes: 1