Simple, a little ugly:
This is a simpler version of my answer to your other question .
I think that you are still within the limits of REST if you do the following. However, I am curious what others think about this situation, so I hope to hear from others.
Your URI will be:
/customer/21/credits
You send a credit resource to the URI (maybe <credit>5</credit> ), then the server can take the client’s balance and += with the value provided. In addition, you can support negative loans (for example, <credit>-10</credit> );
Please note that /customer/21/credits does not support all methods. POST support is only perfectly acceptable.
However, this is a little strange if loans are not a true resource in your system. HTTP spec says:
If the resource was created on the source server, the response MUST be 201 (created) and contain an object that describes the status of the request and refers to the new resource and the Location header.
Technically, you are not creating a resource here, you are adding to the client’s balance (which is really the totality of all previous loans in the system). Since you do not support the loan (presumably), you really cannot return the link to the newly created loan resource. Perhaps you could return the client’s balance or <customer> , but this is a little unintuitive for customers. That is why I believe that considering each loan as a new resource in the system is easier to work with (see below).
My preferred solution:
It depends on my answer in another question. Here I will try to approach it in terms of what the client / server does:
Customer:
Creates a new credit resource:
<credit> <amount>5</amount> </credit>
POST Resources for /customer/21/credits
POSTing here means: "add this new <credit> , which I am providing to the <credit> list for this client.
Server:
This gives you the following benefits:
- Access to loans can be obtained later using id (with GET
/customer/21/credits/[id] ) - You have a complete credit history audit trail.
- Clients can, if you support it, update or delete loans by id (using PUT or DELETE)
Customers can get an ordered list of loans if you support it; e.g. GET /customer/21/credits may return:
<credits href="/customer/21/credits"> <credit href="/customer/21/credits/credit-id-7382134"> <amount>13</amount> ... </credit> <credit href="/customer/21/credits/credit-id-134u482"> ... </credit> ... </credits>
- It makes sense, because the client balance is indeed the end result of all loans applied to this client.
source share