Cumulative values ​​in the REST API

I am working on an application that requires some double writing. There are currently two endpoints

/account /transaction 

Although /account handles general account data, /transaction handles transactions for deposits / withdrawals. The account balance is calculated based on the relevant transactions. I left them separated to get consistency in accounting when transferring value from one account to another.

My question is how to present the account balance at the /account endpoint, since it will always be calculated based on the time of the request. If the answer just contains balance as a read-only field? This smells like a bad API design, as all fields are, but this option will be writable / updated.

An alternative, in my opinion, would be to expand the endpoint to

 /account/{id}/balance 

returns only the balance of the corresponding account. However, this always requires a second call to get a balance in addition to the rest of the account information. Perhaps the answer could generalize how to represent aggregated values.

+6
source share
1 answer

Very good question. I often encounter such situations. I would say two things:

  • You probably have other read-only fields like "id"
  • You may not need to take the time necessary to calculate the current balance each time you receive account information.

I think I would choose / account / {id} / balance ... but maybe call it / account / {id} / calculatebalance to indicate that it takes some time to run these methods. And then it is obvious that the value is a calculated value. If you had "several" calculated values, I would reconsider my opinion.

2 cents.

0
source

All Articles