REST API - 媒体类型参数中的属性过滤器 [英] REST API - attribute filter in media type parameters

查看:133
本文介绍了REST API - 媒体类型参数中的属性过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想就设计REST / hipermedia API寻求一些建议,特别是关于使用 django-rest 框架。



相反,通用的实体示例,我将使用一个更平凡的文档实体。



问题1。

  GET / document /?[query] 

获取文档列表。问题是文档具有很少的属性,在许多情况下,客户端只关心几个(特别是在这个搜索中) - 响应的大小可能会有所不同,几个(最多10个)时间,还有服务器查询可能会更快。另外,我不得不提到,我们更喜欢无模式。



我发现像

  GET / document / attr1,attr2 ../?[查询] 



另一篇文章建议使用Content-Types(实际上是接受,因为它是针对请求),但是例子是失踪,仍然有混合的感觉。如下所示:

 接受:application / json; attrs =attr1,attr2

我不知道这是否符合HTTP的语义,也是如果这样使用媒体类型的参数是适当的(毕竟我想要一个不同的资源表示 - 一些属性被过滤掉)。



问题2.



如果以上是或多或少的可接受的解决方案,我想知道在 django-rest 。从我可以看到他的文档,媒体类型参数没有单独解析(或处理)。



编辑



一些额外的信息:应用程序的大部分是OLTP(不可缓存)。架构是具有静态文件的JSON服务器,JS重客户端。



编辑2



实际上,我发现一些意见,搜索本质上是创建新的(易失性)资源(结果),所以POST方法更合适。这消除了讨论中的问题。我对创建的实体(结果)有一些问题,因为我不想坚持下去,但我认为这不是强制性的。问题是在位置标题(伪造的URL,没有位置标题或其他)中放置什么?我唯一一致的行为正是我不想做的 - 搜索POST执行搜索,将结果存储在服务器端,并返回201并链接到它。对于浏览器测试,MIME类型的text / html可能会提供用户友好的表单进行搜索。

解决方案

建筑风格通知那些限制设计然后被实现的架构。 REST是一种建筑风格。您很难设计一个URI,而不是因为实现选项有限,而是因为架构不匹配。您的客户想要通过减少邮件的大小来最大限度地提高效率。但您选择的建筑风格(REST)使用缓存最大限度地提高效率,这自然会导致更大的消息(从而减少资源)。如果您的架构不使用缓存来最大限度地提高效率,那么它就会偏离REST风格(并且可能会产生新风格; Roy应该对这种非常常见的风格进行架构分析)。



解决方案是选择不同的架构风格(RPC通过减少消息大小来最大限度地提高效率),或者由于它带来的质量好处:可扩展性,简单性,效率,可演变性和用户感知性能。


I want to seek some advise regarding designing REST / hipermedia API and in particular regarding implementation with django-rest framework.

Instead the generic 'entity' example, I will use a more mundane 'document' entity.

Question 1.

GET /document/?[query]

gets a list of documents. The issue is that 'document' has few dozens of attributes and in many cases the client would care only for a few (especially in this search) - the size of the response may differ like several (up to 10) times, and also server queries may be faster. Also, I have to mention that we prefer to be schema-less.

I found samples like

GET /document/attr1, attr2../?[query]

which i find quite un-REST-ful.

Another article was suggesting to use Content-Types (in fact Accept, as it is for requests), but example was missing and still have mixed feeling. Something like:

Accept: application/json; attrs="attr1,attr2"

I am not sure if this is respecting the semantics of HTTP, and also if such use of media type parameters is appropriate (after all I want a different representation of resource - with some attributes filtered out).

Question 2.

If the above is more or less aceptable solution, I wonder if there is something ready in django-rest regarding parsing of custom media type attributes. From what i can see in he docs, media type parameters are not separately parsed (or handled).

Edit

Some additional info: large part of the application is OLTP (with will not be cacheable). Architecture is JSON server with static files, JS heavy client.

Edit 2

Actually, I found some opinions that searches in their nature are creation of new (volatile) resource (the result), so POST method is more appropriate. This eliminates the issue under discussion. I have some problems with the created entity (result), as I don't want to have to persist it, but I think this is not compulsory. The question is what to put in 'Location' header (bogus URL, no Location header or else)? The only consistent behavior for me is exactly what I do not want to do - search POST performs the search, stores the result on server side and returns 201 with link to it. This, however, is unjustified persistence load...

Regarding browser testing, MIME type text/html may present a user friendly form for searching.

解决方案

Architectural styles inform architectures which constrain designs which are then implemented. REST is an architectural style. You're finding it hard to design a URI, not because the implementation options are limited, but because of an architectural mismatch. Your client "wants" to maximize efficiency by reducing the size of messages. But the architectural style you've chosen (REST) uses caching to maximize efficiency, which naturally leads to larger messages (and therefore fewer resources). If your architecture doesn't use caching to maximize efficiency, it's deviating from the REST style (and potentially making a new style; Roy should do an architectural analysis of this very common style).

The solution is to either pick a different architectural style (RPC maximizes efficiency by reducing the size of messages), or influence your client to want REST because of the quality benefits it brings: scalability, simplicity, efficiency, evolvability, and user-perceived performance.

这篇关于REST API - 媒体类型参数中的属性过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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