通过REST(RESTful)API进行批量收集操作 [英] Bulk Collection Manipulation through a REST (RESTful) API

查看:3772
本文介绍了通过REST(RESTful)API进行批量收集操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一些关于设计REST API的建议,它允许客户有效地向集合中添加/删除大量对象。



通过API,客户需要能够向集合中添加项目并从中删除项目,以及操作现有项目。在许多情况下,客户端希望对集合进行批量更新,例如添加1000个项目并删除500个不同的项目。感觉客户端应该能够在与服务器的单个事务中执行此操作,而不是需要1000个单独的POST请求和500个DELETE。



有没有人有关于达到此目的的最佳做法或惯例的任何信息?



我目前的想法是一个应该能够将表示变化的对象PUT到集合URI,但这似乎与 HTTP 1.1 RFC ,这似乎表明在PUT请求中发送的数据应该独立于已经存在于URI的数据进行解释。这意味着客户端必须一次性发送集合的新状态的完整描述,这可能远远大于更改,或者甚至超过客户端在发出请求时所知道的。 / p>

显然,如果有必要,我很乐意偏离RFC,但如果存在这样的惯例,我宁愿以传统方式这样做。

解决方案

您可能希望将更改任务视为资源本身。所以你真的要投入一个对象,这是一个批量数据更新对象。也许它有一个名称,所有者以及需要解析和执行的大量CSV,XML等。对于CSV,您可能还需要确定CSV数据中表示的对象类型。



列出作业,添加作业,查看作业状态,更新作业(可能是为了启动/停止它),删除作业(如果它正在运行则停止它)等等。这些操作很容易映射到REST API设计。



完成此操作后,您可以轻松添加批量数据更新程序可以处理的不同数据类型,甚至可以在同一任务中混合使用。对于您要导入的每种类型的东西,没有必要在您的应用程序中复制相同的API,换句话说。



这也非常容易让后台任务实施。在这种情况下,您可能希望向各个任务对象添加字段,以允许API客户端指定他们希望如何通知他们(他们希望您在完成时获取的URL,或向他们发送电子邮件等) 。


I'd like some advice on designing a REST API which will allow clients to add/remove large numbers of objects to a collection efficiently.

Via the API, clients need to be able to add items to the collection and remove items from it, as well as manipulating existing items. In many cases the client will want to make bulk updates to the collection, e.g. adding 1000 items and deleting 500 different items. It feels like the client should be able to do this in a single transaction with the server, rather than requiring 1000 separate POST requests and 500 DELETEs.

Does anyone have any info on the best practices or conventions for achieving this?

My current thinking is that one should be able to PUT an object representing the change to the collection URI, but this seems at odds with the HTTP 1.1 RFC, which seems to suggest that the data sent in a PUT request should be interpreted independently from the data already present at the URI. This implies that the client would have to send a complete description of the new state of the collection in one go, which may well be very much larger than the change, or even be more than the client would know when they make the request.

Obviously, I'd be happy to deviate from the RFC if necessary but would prefer to do this in a conventional way if such a convention exists.

解决方案

You might want to think of the change task as a resource in itself. So you're really PUT-ing a single object, which is a Bulk Data Update object. Maybe it's got a name, owner, and big blob of CSV, XML, etc. that needs to be parsed and executed. In the case of CSV you might want to also identify what type of objects are represented in the CSV data.

List jobs, add a job, view the status of a job, update a job (probably in order to start/stop it), delete a job (stopping it if it's running) etc. Those operations map easily onto a REST API design.

Once you have this in place, you can easily add different data types that your bulk data updater can handle, maybe even mixed together in the same task. There's no need to have this same API duplicated all over your app for each type of thing you want to import, in other words.

This also lends itself very easily to a background-task implementation. In that case you probably want to add fields to the individual task objects that allow the API client to specify how they want to be notified (a URL they want you to GET when it's done, or send them an e-mail, etc.).

这篇关于通过REST(RESTful)API进行批量收集操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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