将处理控制映射到 REST [英] Mapping processing control to REST

查看:42
本文介绍了将处理控制映射到 REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一项管理长期运行(自动)流程的服务.进程可以开始、暂停、恢复或取消.例如,考虑视频转换或 3d 打印.所有以前运行和当前运行的进程的列表始终可用.

Let's say you have a service that manages long-running (automatic) processes. Process can be started, paused, resumed or cancelled. As an example, consider video conversion or 3d printing. A list of all previously run and currently running processes is always available.

我正在尝试将这个概念映射到 REST,但遇到了一些问题.

I am trying to map this concept to REST and having some problems.

Start 可能会映射到 POST/processes,但感觉很奇怪.开始不是创建,并且具有 POST 意味着为某些集合创建项目,而为其他集合启动过程听起来令人困惑.但那部分不那么重要.

Start can potentially be mapped to POST /processes, but it feels weird. Start is not create, and having POST mean create item for some collections, and start process for others sounds confusing. But that part is less important.

暂停、恢复和取消是我绊倒的地方.我可能会将其视为 PATCH — 但是 RESTful 和 RPC 方法之间有什么区别呢?

Pause, resume and cancel is where I stumble. I could potentially see it as PATCH — but what is the difference between RESTful and RPC approaches then?

然而,对于 PATCH,封闭的实体包含一组描述资源当前如何驻留在应修改原始服务器以生成新版本.

With PATCH, however, the enclosed entity contains a set of instructions describing how a resource currently residing on the origin server should be modified to produce a new version.

如果指令指定状态应该设置为暂停,这似乎打破了封装并且感觉很随意——暂停可能影响的不仅仅是状态(任何内部任务等).

If the instruction specifies that status should be set to paused, this seems to break encapsulation and feels arbitrary — pause can potentially affect much more than just status (any inner tasks etc).

如果指令字面上是 pause — 这更好,但如果所有客户端都必须知道这些特定消息,那么与 RPC 相比有什么好处?

If the instruction is literally pause — this is better, but what's the benefit over RPC if all clients have to know these specific messages anyway?

推荐答案

POST 到集合资源 /processes 在默认状态下创建一个新进程.响应包含一个标头

POST to a collection resource /processes creates a new process in a default state. The response contains a header

Location: /processes/42

这个进程的状态可以通过

The state of this process can be retrieved by

GET /processes/42

可能是

{
  "id": 42,
  "state": "created"
}

如果客户想改变这个过程的状态,他可以

If the client wants to change the state of this process, he could

POST /process/42

{
  "state": "started"
}

请注意,JSON 是不完整的.此模式通常用于对现有资源的 POST 请求,可以解释为使用提供的参数更新资源".当然服务器可以随意忽略这个请求,因为服务器状态会被破坏或因为其他原因.

Note that the JSON is incomplete. This pattern is often used in a POST request to an existing resource and can be interpreted as 'update the resource with the provided parameters'. Of course the server is free to ignore this request because server state would be corrupted or because of other reasons.

REST 是关于资源及其表示的.REST 客户端的 client 状态可能与服务器状态不匹配.并非客户端请求的服务器状态的每个转换都是可行的.

REST is about resources and their representation. The REST client has client state which may not match the server state. And not every transition in server state requested by the client may be doable.

这篇关于将处理控制映射到 REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆