Going beyond CRUD in RESTful services

I am working on a WCF RESTful service and noticed that in some places I can present operations other than CRUD (not resources) in more than one way.

Say we bought a new TV and connected it to our private network. Now we want to create a Web service for viewing and managing the TV.

  • To read and update the properties of the TV, we will use the following URI:

    http: // domain / tv / GET | PUT - receiving and updating TV properties. ( company , owner , inches )

  • To use the video, we will use the following URI:

    ws: // domain / tv / video - (suppose WebSocket is the best option for transferring video content)

  • And operations:

    • updateVersion
    • startBIT (built-in self test)
    • changeChannel
    • turnVolumeUp , turnVolumeDown

The first project is to use properties to represent operations. The server will see the property changes, and then perform the required operations. Finally, POST operations that cannot be represented by properties:

http: // domain / tv / GET | PUT - getting or setting volume or channel using json objects.

and for updateVersion or startBIT :

http: // domain / tv / POST with {function: 'updateVersion'} or {function: 'startBIT'}

The second project is to represent all operations using the Command resource:

http: // domain / tv / commands POST with {command: 'BIT', sender: 'Dan' ...} - create a new command to execute startBIT , changeChannel or turnVolume

The third design is to represent each operation, which can be expressed as a noun as a resource, and the rest as properties:

http: // domain / tv / versionUpdates GET | Put | REMOVE | Post

http: // domain / tv / BITs GET | Put | REMOVE | Post

http: // domain / tv / PUT ({volume: 10})

http: // domain / tv / PUT ({channel: 29})

What is the best RESTful design?

+5
source share
1 answer

The third option (with the changes mentioned below) seems to be best designed - because REST is resources and nouns, not verbs and operations.

Changes I would apply:

  • http://domain/tv/firmware GET | DELETE | POST

    I would change versionUpdates to firmware - it seems much more self-describing. What else seems to be that the PUT operation does not make any sense here - you probably do not know the new firmware version a priori. Thus, GET returns the current firmware version, DELETE deletes the latest and restores the previous ones, POST looks and sets for the latest version.

  • http://domain/tv/BITs GET | POST

    Here it seems that GET and POST will be enough. You just need to get the results of all the tests or a specific one or just the POST new test.

  • http://domain/tv/ PATCH ({volume: 10}) and http://domain/tv/ PATCH ({channel: 29})

    Since both are very similar - I would change PUT to PATCH - I remember that when using PUT you need to include the entire object - that is, each property. PUT also idempotent. When using PATCH you can only change one property.

+3
source

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


All Articles