简体   繁体   English

REST用于更新资源的不同用例

[英]REST different use cases for updating a resource

I want to know whats right in the RESTful prespective when i want to update a resource but there are multiple operations, each one with a different flow in the business layer 我想知道什么是RESTful,当我想要更新资源但有多个操作,每个操作在业务层中有不同的流程

for example, there is 3 cases to change the task status (all of them will update the task) 例如,有3种情况需要更改任务状态(所有这些都将更新任务)

  • Acquire 获得
  • Release 发布
  • Update 更新

so to do that what i would do is doing a PUT to: 所以我要做的就是做一个PUT:

PUT: Tasks/{taskId}?flag=acquire

The reason i am asking is because if this is valid, What I'll do is: 我问的原因是因为如果这是有效的,我将做的是:

In the back end code I'll make a condition on the flag, based on each value will do something different entirely (I don't like that from the code perspective), but is this the right way to do it, or there is an alternative way to define the resources from the REST perspective ? 后端代码中我将在标志上创建一个条件,基于每个值将完全做一些不同的事情(我不喜欢从代码角度来看),但这是正确的方法,或者有从REST角度定义资源的另一种方法?

Given that I don't really understand what your trigger events are in this case (what does "acquire" mean in the scope of your design?) I'll give you a real-world example. 鉴于我并不真正理解你的触发事件在这种情况下是什么(在你的设计范围内“获取”是什么意思?)我将给你一个真实的例子。

Think of the RESTful approach as being like so: 将RESTful方法视为如此:

/what?property=value

Imagine {taskid} exists with the following data: 想象{taskid}存在以下数据:

{
    "status":"dormant",
    "owner":"user1"
}

A PUT by virtue of definition, is an update. 根据定义, PUT是更新。 Therefore in your case, you could PUT the following JSON payload like: 因此,在您的情况下,您可以输出以下JSON有效负载,如:

PUT: tasks/{taskid}

{
    "status":"initiated",
    "running_tasks": [
        "item1",
        "item2",
        "item6"
    ],
    "notify": [
        "user1",
        "user8"
    ]
}

Will have the following effect on the data: 会对数据产生以下影响:

{
    "status":"initiated",
    "running_tasks": [
        "item1",
        "item2",
        "item6"
    ],
    "notify": [
        "user1",
        "user8"
    ]
    "owner":"user1"
}

If you want me to incorporate Acquire, Release & Update in this example, please elaborate on what your flow looks like and what ?flag=acquire would actually do. 如果您希望我在此示例中包含Acquire,Release和Update,请详细说明您的流程是什么样的以及什么?flag=acquire实际上会做什么。

I'm trying to read between the lines a little bit, but it seems to me you want to execute operations on your task resources, not just assign them state. 我试着稍微阅读一下这些行,但在我看来,你想在你的任务资源上执行操作,而不仅仅是为它们分配状态。 So, that would be implemented in terms of a POST. 因此,这将在POST方面实施。 However, you don't want to overload POST to mean different things on the same resource. 但是,您不希望重载POST以表示同一资源上的不同内容。 For this, you could use "controller" resources. 为此,您可以使用“控制器”资源。

For example, to acquire a task (eg for a worker to "claim" the task and begin executing it), the worker could POST to /tasks/allocator, with a payload specifying the URI of the task the worker wants to claim (maybe with no payload, the allocator could assign one based on position in queue, priority, etc.). 例如,为了获取任务(例如,对于工作人员“声明”任务并开始执行它),工作人员可以POST到/ tasks / allocator,其中有效负载指定工作人员想要声明的任务的URI(可能如果没有有效负载,分配器可以根据队列中的位置,优先级等分配一个。 This would have the side effect of changing the state of the task (eg change status to "in progress", record the ID of the worker, the start time, etc.). 这将具有改变任务状态的副作用(例如,将状态改变为“进行中”,记录工作者的ID,开始时间等)。

Updating a task in progress could be a PUT if you're just changing its state. 如果您只是更改其状态,则更新正在进行的任务可能是一个PUT。 If you're doing something more complicated then maybe POST to a different controller resource. 如果你正在做一些更复杂的事情,那么可能会POST到不同的控制器资源。

Releasing the task could also be a PUT, if you're just changing its state (eg clearing the assigned worker ID, changing its status, recording the completion time). 如果您只是更改其状态(例如,清除分配的工作者ID,更改其状态,记录完成时间),则释放任务也可以是PUT。 Or again, it could be another POST to a different controller resource if there's more to it. 或者,如果有更多的控制器资源,它可能是另一个控制器资源的POST。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM