Search code examples
httpresthttp-verbsidempotent

How to create a RESTful object with server calculated fields


I have an object:

Account
{
    Id,
    Name,
    CurrentBalance
}

Id is an immutable key, Name is a mutable string, and CurrentBalance is calculated from all of the transactions associated with the account.

I am stuck on the fact that GET \Accounts\{Id} will not be idempotent because changes to a transaction will cause a change in CurrentBalance. Should I remove this field from the object and make a request like

POST \Accounts\{Id}\CurrentBalance

But now I have to make multiple calls to the server to get the CurrentBalance of all objects:

GET \Accounts
POST \Accounts\{Id1}\CurrentBalance
POST \Accounts\{Id2}\CurrentBalance
POST \Accounts\{Id3}\CurrentBalance
....

I guess I am just looking to see if there is already a standard way to handle this that I am missing?

UPDATE

Part 2 if the original object is ok via GET. My only way to update the Account.Name is via a PATCH as I cannot allow an update to CurrentBalance, correct?

NOTE

I realize I could put this on the client to have to get all transactions and calculate it, but I would prefer to do this on the server for multiple reasons


Solution

  • Idempotency does not mean that you must always get the same response back.

    Consider the resource /TodaysWeather. It would be pretty useless if it always returned the same value.

    Idempotency simply states that if a client makes the same request multiple times instead of just once, the impact on the system (from the client's perspective) will be the same.