Implementing the command pattern in a RESTful API

I’m in the process of designing an HTTP API, hopefully making it as RESTful as possible.

There are some actions which functionality spreads over a few resources, and sometime needs to be undone.

I thought to myself, this sounds like a command pattern, but how can I model it into a resource?

I will introduce a new resource named XXAction, like DepositAction, which will be created through something like this

POST /card/{card-id}/account/{account-id}/Deposit
AmountToDeposit=100, different parameters...

this will actually create a new DepositAction and activate it’s Do/Execute method.
In this case, returning a 201 Created HTTP status means the action has been executed successfully.

Later if a client wishes to look at the action details he can

GET /action/{action-id}

Update/PUT should be blocked I guess, because it is not relevant here.

And in order to Undo the action, I thought of using

DELETE /action/{action-id}

which will actually call the Undo method of the relevant object, and change it’s status.

Let’s say I’m happy with only one Do-Undo, I don’t need to Redo.

Is this approach ok?

Are there any pitfalls, reasons not to use it?

Is this understood from the POV of the clients?

5

You’re adding in a layer of abstraction that is confusing

Your API starts off very clean and simple. A HTTP POST creates a new Deposit resource with the given parameters. Then you go off the rails by introducing the idea of “actions” that are an implementation detail rather than a core part of the API.

As an alternative consider this HTTP conversation…

POST /card/{card-id}/account/{account-id}/Deposit

AmountToDeposit=100, different parameters…

201 CREATED

Location=/card/123/account/456/Deposit/789

Now you want to undo this operation (technically this should not be allowed in a balanced accounting system but what the hey):

DELETE /card/123/account/456/Deposit/789

204 NO CONTENT

The API consumer knows that they are dealing with a Deposit resource and is able to determine what operations are permitted on it (usually through OPTIONS in HTTP).

Although the implementation of the delete operation is conducted through “actions” today there is no guarantee that when you migrate this system from, say, C# to Haskell and maintain the front end that the secondary concept of an “action” would continue to add value, whereas the primary concept of Deposit certainly does.

Edit to cover an alternative to DELETE and Deposit

In order to avoid a delete operation, but still effectively remove the Deposit you should do the following (using a generic Transaction to allow for Deposit and Withdrawal):

POST /card/{card-id}/account/{account-id}/Transaction

Amount=-100, different parameters…

201 CREATED

Location=/card/123/account/456/Transation/790

A new Transaction resource is created which has exactly the opposite amount (-100). This has the effect of balancing the account back to 0, negating the original Transaction.

You might consider creating a “utility” endpoint like

POST /card/{card-id}/account/{account-id}/Transaction/789/Undo <- BAD!

to get the same effect. However, this breaks the semantics of a URI as being an identifier by introducing a verb. You are better off sticking to nouns in identifiers and keeping operations constrained to the HTTP verbs. That way you can easily create a permalink from the identifier and use it for GETs and so on.

11

The main reason for REST existence is resilience against network errors. To which end all operations should be idempotent.

The basic approach seems reasonable, but the way you describe the DepositAction creation does not sound to be idempotent, which should be fixed. By having client provide unique ID that will be used to detect duplicate requests. So the creation would change to

PUT /card/{card-id}/account/{account-id}/Deposit/{action-id}
AmountToDeposit=100, different parameters...

If another PUT to the same URL is made with the same content as previously, the response should still be 201 created if the content is the same and error if the content is different. This allows the client to simply retransmit the request when it fails, since the client can’t tell whether the request or response got lost.

It makes more sense to use PUT, because it just writes the resource and is idempotent, but using POST wouldn’t really cause any problem either.

To look at the transaction details the client will GET the same URL, i.e.

GET /card/{card-id}/account/{account-id}/Deposit/{action-id}

and to undo it, it can DELETE it. But if it actually has anything to do with money as the sample suggests, I would suggest PUTting it with added “cancelled” flags instead though for accountability (that there remains trace of created and cancelled transaction).

Now you need to choose a method of creating the unique id. You have several options:

  1. Issue client-specific prefix earlier in the exchange that must be included.
  2. Add a special POST request to get blank unique ID from the server. This request does not have to be idempotent (and can’t, really), because unused IDs don’t really cause any trouble.
  3. Simply use UUID. Everybody uses them and nobody seems to have any problem with neither the MAC-based ones nor the random ones.

9

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật