RESTful way to make a choice

I have a simple api that works as follows:

  • The user creates a request ( POST /requests )
  • Another user receives all requests ( GET /requests )
  • Then adds the sentence to the request ( POST /requests/123/offers )
  • The original user can now see all the offers made for the request ( GET /requests/123/offers )

What I want to do allows the embedded user to accept the request, but I cannot find a better way to do this RESTfuly.

Should I do this using the verb PATCH? How do PATCH /requests/123 require the patch to contain a valid offer identifier?

+5
source share
2 answers

Accepting a proposal five times should have the same effect as accepting it once. This is idempotent. Therefore, it must be PUT.

You might want to choose a different name for your "requests." When I do GET /requests/123 , I request a response, which is a request? This can be a bit confusing for customers.

Also, try to avoid nesting resource identifiers. This may create problems for you later. The offer really should not be β€œunder” the corresponding request. What happens when you later want to offer offers that match multiple requests?

A good rule of thumb is that if you consider a Gizmo object as an entity in the entity-relationship model , it must be the -level URI root, as in GET /gizmos/17 , and not GET /widgets/54/gizmos/17 . A common mistake is to say: "Each Gizmo has exactly one related widget, so I have to embed the Gizmo URIs as extensions of the Widget URIs."

Below I suggest what the operations will look like. You might want to replace some of the identification links with a URI, but that is up to you.

 POST /requests ---> 201 Created Location: /requests/123 GET /requests ---> 200 OK [ { "requestId": 123, "offersUri": "/offers?requestId=123", ... }, ... ] POST /offers ---> 201 Created { Location: /offers/456 "requestId": 123, "amount": 300, ... } GET /offers?requestId=123 ---> 200 OK [ { "requestId": 123, "amount": 300, ... } ] PUT /offers/456/approval ---> 204 No Content PUT /offers/456/approval ---> 204 No Content PUT /offers/456/approval ---> 204 No Content 
+7
source

Depends on the nature of the Reception.

If Acceptance is a simple attribute of an offer, I would set the offer using the Acceptance parameter to True.

If Acceptance is more complex and, therefore, your own resource, I would put the acceptance in the proposal ( PUT /requests/123/offers/acceptance ).

If there is such a thing as a refusal or request for clarification of the proposal, I can consider the corresponding resource as the answer, and not an acceptance, and PUT ( put /requests/123/offers/response ).

+1
source

Source: https://habr.com/ru/post/1206644/


All Articles