简体   繁体   English

编写API的好处是:自动而不是完全包括嵌套的对象,或者提供一个参数来指定要包括的对象?

[英]Writing an API, benefits of: including nested objects automatically, not at all, or provide a parameter to specify which to include?

For example, we have an entity called ServiceConfig that contains a pointer to a Service and a Professional . 例如,我们有一个称为ServiceConfig的实体,其中包含pointer ServiceProfessionalpointer If returned without including the fields would look like this: 如果返回但不包含字段,则将如下所示:

{
    'type': '__Pointer', 
    'className': 'Service', 
    'objectId': 'q92he840'
}

At which point they could query again to retrieve that service. 此时,他们可以再次查询以检索该服务。 However, it is often the case that they need the Service name. 但是,通常情况下,他们需要Service名称。 In which case it is inefficient to have to query again to get the service every time. 在这种情况下,每次必须再次查询才能获得服务的效率很低。

Options: 选项:

  1. Automatically return the Service . 自动返回Service In which case, we should automatically return the Industry for that Service as well in case they need that... same applies to all. 在这种情况下,如果他们需要,我们也应该自动返回该ServiceIndustry 。。。 Seems like we're returning data too often here. 似乎我们在这里经常返回数据。

  2. Allow them to pass an includes parameter that specifies which entities to include. 允许他们传递一个includes参数,该参数指定要包含的实体。 Format is an array of strings where using a . 格式是使用的字符串数组. can allow them to include subclasses. 可以允许他们包括子类。 In this case ['Professional', 'Service.Industry'] would work. 在这种情况下, ['Professional', 'Service.Industry']将起作用。

Can anyone identify why any one solution would be better than the others? 谁能确定为什么任何一种解决方案都会比其他更好? I feel that the last solution is the best, however it does not seem to be common to do to in the APIs I've seen. 我觉得最后一个解决方案是最好的,但是在我见过的API中似乎并不常见。

This is a good API Design decision to spend your time on before you release an initial version. 这是一个不错的API设计决策,需要花时间在发布初始版本上。 Both your approaches are valid and it all depends on what you think are the most common ways that clients would use your API. 这两种方法都是有效的,并且都取决于您认为客户端使用您的API的最常见方式。

Here are some points that you could consider: 您可以考虑以下几点:

  1. You might prefer the first approach where you do not give all the data upfront. 您可能更喜欢第一种方法,即您不预先提供所有数据。 Sometimes it is about efficiency and at times it is also about security and ensuring that any additional important data is only fetched on as as needed basis and on authorization. 有时,它关系到效率,有时也关系到安全性,并确保仅根据需要和授权来获取任何其他重要数据。
  2. Implementing the 2nd approach is going to take more effort on part of your team to design/code and test out the API. 实施第二种方法将需要团队的更多工作来设计/编码和测试API。 So you might want to consider how much of effort you want to put into release 1.0 因此,您可能需要考虑要在1.0版中投入多少精力
  3. Since you have nested data for example, the second approach will serve you well. 例如,由于您已嵌套数据,因此第二种方法将为您提供良好的服务。 Several public APIs do that as a matter of fact. 实际上,有几个公共API可以这样做。 For eg look at the LinkedIn public API and particularly the facets section, where you can specify the fields or additional information that you would like to return. 例如,查看LinkedIn公共API,尤其是Facets部分,您可以在其中指定要返回的字段或其他信息。
  4. Look at some of the client applications that you have written and if you can identify for sure that some data is needed anyways upfront, then it can help in designed the return data. 查看您已编写的某些客户端应用程序,如果可以确定是否确实需要一些数据,那么它可以帮助设计返回数据。
  5. Eventually monitoring API usage and doing some analysis on the number of calls, methods invoked will give you good inputs on what to do next. 最终监视API使用情况并分析调用次数,所调用的方法将为您提供下一步的良好输入。

If I had to make a choice and have a little bit more leeway in terms of effort, I would go with the 2nd option, even if it is a simple version at first. 如果我不得不做出选择并且在努力方面还有更多余地,那么我会选择第二个选项,即使起初它是一个简单的版本。

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

相关问题 从 REST API 获取所有数据,包括嵌套的 API 链接 - Get all the data from a REST API including nested API links Rest API 中哪个更好的做法是返回 NESTED 空对象(无数据)、第一级空对象或 null 或 404? - Which is better practice in Rest API, to return NESTED empty objects (no data), first level empty object or null or 404? 澄清 API 的好处 - Clarification of API benefits 自动传递所有 API 的 api 密钥 - Passing api key for all API automatically API 提供来自弹性搜索的数据,而不是 SQL - API which provide data from Elastic Search and not SQL 我应该在GitHub API中提供哪些标头进行身份验证? - Which headers should I provide for authentication in GitHub API? 将嵌套对象从 Blazor API 返回到 WASM - Returning nested objects from Blazor API to WASM 如何为Clockify API指定日期/时间? 哪些字段是可选的? - How to specify date/time for the clockify API? Which fields are optional? 如何在PHP中解析Google Directions API Json,特别是每个嵌套的“Step”和“Leg”的Distance-Values和Duration-Values? - How to parse Google Directions API Json in PHP, specifically the Distance-Values and Duration-Values for each “Step” and “Leg” which are all nested? RESTful URL中嵌套节点的好处是什么 - What are the benefits of nested nodes in a RESTful URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM