简体   繁体   English

最佳的API最佳实践:是否包含状态

[英]Best rest API practice: Whether to include a status or not

for instance let me say I have an API that returns several object in an array that have stati... Just like 例如,让我说我有一个API,该API返回数组中具有固定状态的多个对象。

[{status:good},...,{status:good}]

The application logic says, this array is 'good' iff each objact in it has the status 'good'. 应用程序逻辑说,如果其中的每个对象的状态为“良好”,则此数组为“良好”。 Of course. 当然。

So, now I return that array through my API JSON encoded. 因此,现在我通过API JSON编码返回该数组。 Should I insert the status directly into the API output, like so 我是否应该将状态直接插入API输出中,就像这样

{status:good, array:[{status:good},...,{status:good}]}

or should I do it on the client side. 还是应该在客户端执行

How to decide such cases while API design? API设计时如何确定这种情况? Is there something like a guideline? 有没有类似的指导方针? I mean the first approach is more intuitive, while the second is less expensive. 我的意思是第一种方法更直观,而第二种方法更便宜。

The most important question is what is your API being used for? 最重要的问题是您的API的用途是什么? By adding an extra "summary" status you are de-normalising the data, which is a huge topic in itself. 通过添加额外的“摘要”状态,您可以对数据进行非规范化,这本身就是一个巨大的话题。

If it would help users and a priority is to make your API quick & easy to use then include it, make the API easy-to-use, especially if it can reduce the need for repeat calls. 如果可以帮助用户并且优先考虑的是使您的API快速且易于使用,则将其包括在内,使该API易于使用,尤其是在可以减少重复调用的情况下。

If a priority is data integrity and heavy data usage over ease-of-use, then don't include a summary. 如果优先考虑的是数据完整性和大量数据使用,而不是易用性,则不要包含摘要。

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

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