简体   繁体   English

具有批处理请求的RESTful API

[英]RESTful API with Batch Requests

We have a not so RESTful API used by our single page app and mobile apps. 我们的单页应用和移动应用使用了一个不那么RESTful的API。

It is not so RESTfu since in the past, URI's have returned whatever was useful for a particular page. 它不是那么RESTfu,因为在过去,URI已经返回了对特定页面有用的任何东西。 This has led to a large number of endpoints, with many having very similar responses. 这导致了大量的端点,其中许多端点具有非常相似的响应。

Datawise we have resources with tens of related resources. Datawise我们拥有数十个相关资源的资源。 In some cases we want those related resources returned, or some of them, and other cases we do not. 在某些情况下,我们希望返回相关的资源,或者其中一些,以及其他我们不需要的情况。 Some are slow to return, and therefore we only want them in specific cases. 有些回复很慢,因此我们在特定情况下只需要它们。

The problem we've had has been with splitting the data up into meaningful URI's, without needing another request to get each related resource. 我们遇到的问题是将数据拆分为有意义的URI,而无需另外请求获取每个相关资源。

we therefore considered a /batch endpoint where a POST request containing a number of requests in the body could execute those requests in parallel on the server. 因此,我们考虑了一个/ batch端点,其中包含正文中的多个请求的POST请求可以在服务器上并行执行这些请求。 Like this https://developers.facebook.com/docs/graph-api/making-multiple-requests 像这样https://developers.facebook.com/docs/graph-api/making-multiple-requests

That way we could split the data into meaningful URI's and not have to make 20 API requests for each page. 这样我们就可以将数据拆分为有意义的URI,而不必为每个页面生成20个API请求。

Is this an acceptable way of handling related resources? 这是处理相关资源的可接受方式吗? Or would it be better to have a URI for each response that we may want? 或者为我们可能需要的每个响应设置一个URI会更好吗?

HTTP/2 will make this problem go away by allowing you to multiplex requests on a single connection. HTTP / 2将允许您在单个连接上复用请求,从而消除此问题。

In the meanwhile, I would suggest that you are not violating any REST constraints with your current solution. 同时,我建议你不要违反当前解决方案的任何REST约束。 However, creating a batch of requests breaks the resource identification constraint which will have a major impact on the cacheability of representations. 但是,创建一批请求会破坏资源标识约束,这将对表示的可缓存性产生重大影响。

Batching is fine -- don't let RESTful design patterns steer you down a path to bad performance. 批处理很好 - 不要让RESTful设计模式引导你走上糟糕的性能之路。

It is likely that your batching solution can be designed such that each piece that can be batched can be called separately. 您的批处理解决方案可能设计为可以分批调用每个可以批处理的部分。 If so, it might be straightforward to create RESTful endpoints for each as well, for those who would want that at the expense of multiple round-trips. 如果是这样,那么为每个人创建RESTful端点也可能是直截了当的,对于那些希望以多次往返为代价的人来说。

You can also use query parameters to select different, packaged returns of resources. 您还可以使用查询参数来选择不同的打包资源返回。 For example, for a user, you could use something like: 例如,对于用户,您可以使用以下内容:

GET /v1/users/1?related={none,all,basic}

You could also use a field selector: 您还可以使用字段选择器:

GET /v1/users/1?data=addresses,history

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

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