How to know which call is correct when multiple potential calls to API each new one invalidates the old one
https://softwareengineering.stackexchange.com/questions/350766
-
14-01-2021 - |
Question
I have a react element that will potentially call an API multiple times in a short amount of time. Every new call would invalidate the old one as it is a pricing request to the server. Is there a way to verify that I have the most recently made request when updating the data on the front-end?
I was thinking that it could be done potentially by timing. Values should also be unique based off of some of the data I send to the server. I was thinking of validating return data based off of the the current state on the front-end side.
La solution
You can think of each individual pricing as a resource:
GET /pricing/1234
To request a pricing, you would POST a pricing request
POST /pricing/request
Which will return as its value either:
A pricing request resource url, if the pricing request takes time to complete
// poll to obtain pricing request status // eventually will contain pricing resource url GET /pricing/request/1234
A pricing resource url
GET /pricing/1234
Through a (user) command you will trigger the POST to /pricing/request, which produces a pricing url which you can use to fetch pricing data. Refetching the data is harmless since it will never change at that URL. New requests produce new url's, so all that needs to be updated is the url with the "current" price.