How to implement a RESTful resource for a state machine or finite automata

前端 未结 4 544
误落风尘
误落风尘 2021-01-01 10:33

I\'m a Rails and REST newbie and I\'m trying to figure how best to expose a resource that is backed by a domain object that has a state machine (in other words is a finite a

4条回答
  •  清歌不尽
    2021-01-01 10:50

    If your resource has some kind of status attribute, you can use a technique called micro-PUT to update it's status.

    PUT /Customer/1/Status
    Content-Type: text/plain
    
    Closed
    
    => 200 OK
    Content-Location: /Customer/1
    

    You can model resource states as collections and move resources between those collections.

    GET /Customer/1
    =>
    Content-Type: application/vnd.acme.customer+xml
    200 OK
    
    
    POST /ClosedCustomers
    Content-Type: application/vnd.acme.customer+xml
    =>
    200 OK
    
    POST /OpenCustomers
    Content-Type: application/vnd.acme.customer+xml
    =>
    200 OK
    

    You could always use the new PATCH method

    PATCH /Customer/1
    Content-Type: application/x-www-form-urlencoded
    Status=Closed
    =>
    200 OK
    

提交回复
热议问题