从REST API获取相关/嵌套数据的最佳实践是什么?

时间:2022-11-28 08:45:25

For example: We have a User model, which in turn has several Company entities. I see 2 solutions:

例如:我们有一个用户模型,后者又有几个公司实体。我看到2个解决方案:

1) Classical. Make an API like:

1)经典。制作如下API:

/users/ 
/users/3/ 
/users/3/companies/ 

and issue /users or companies request separately. However, if we need to have both user and his/her companies information in one view (on Angular2) - we need to send 2 requests to the server.

和问题/用户或公司单独要求。但是,如果我们需要在一个视图中(在Angular2上)同时拥有用户和他/她的公司信息 - 我们需要向服务器发送2个请求。

2) Put the related/nested data inside the level-1 object model. In request:

2)将相关/嵌套数据放在level-1对象模型中。在请求中:

/users/3/ 

the server will provide information about the User, together with his Companies. In this case we get all information for 1 request. But again, the company has an unlimited number of Storage entities. What if they are required in one separate view?

服务器将提供有关用户及其公司的信息。在这种情况下,我们获得1个请求的所有信息。但同样,该公司拥有无限数量的存储实体。如果在一个单独的视图中需要它们怎么办?

I'm more inclined to the first option, but I'm confused by the description of the REST style: "The view must fully represent the resource." Satisfaction of this requirement can lead to a partition of the resource into child resources and, accordingly, to smaller representations. "

我更倾向于第一种选择,但我对REST风格的描述感到困惑:“视图必须完全代表资源。”满足此要求可能导致将资源划分为子资源,并因此导致较小的表示。 “

Please, help with advice, I doubt in connection with the lack of experience what decision will be correct in this case. Oh yes, I forgot, Backend on Django (Python) using Django-Rest-Framework - All this is a SaaS for ~ 1000 users.

请帮助提供建议,我怀疑缺乏经验,在这种情况下哪些决定是正确的。哦,是的,我忘记了,使用Django-Rest-Framework对Django(Python)进行后端 - 所有这些都是〜1000个用户的SaaS。

1 个解决方案

#1


0  

Approach 1 is an an ideal approach for REST. But when it comes to designing APIs for the displaying information on the UI it involves much more than just partitioning the APIs as per resources.

方法1是REST的理想方法。但是,当设计用于在UI上显示信息的API时,它涉及的不仅仅是根据资源对API进行分区。

So I would suggest including the Company information in the User API. But as you suggested Company object can have very large list of Storage objects, in this case I would recommend including only the necessary and sufficient fields of Company model into User API. So that you will be able to render one view. And then later when user expands the Company section then you can pull the left-over fields from /company/<id> API.

所以我建议在User API中包含公司信息。但是正如您所建议的,Company对象可以包含非常大的Storage对象列表,在这种情况下,我建议仅将公司模型的必要和足够的字段包含到User API中。这样您就可以渲染一个视图。然后,当用户展开“公司”部分时,您可以从/ company / API中提取剩余字段。

This way you will have lesser API calls for the hits where user doesn't look for Company details and your API will be light weight as well.

这样,对于用户不查找公司详细信息的命中,您将获得较少的API调用,并且您的API也将是轻量级的。

#1


0  

Approach 1 is an an ideal approach for REST. But when it comes to designing APIs for the displaying information on the UI it involves much more than just partitioning the APIs as per resources.

方法1是REST的理想方法。但是,当设计用于在UI上显示信息的API时,它涉及的不仅仅是根据资源对API进行分区。

So I would suggest including the Company information in the User API. But as you suggested Company object can have very large list of Storage objects, in this case I would recommend including only the necessary and sufficient fields of Company model into User API. So that you will be able to render one view. And then later when user expands the Company section then you can pull the left-over fields from /company/<id> API.

所以我建议在User API中包含公司信息。但是正如您所建议的,Company对象可以包含非常大的Storage对象列表,在这种情况下,我建议仅将公司模型的必要和足够的字段包含到User API中。这样您就可以渲染一个视图。然后,当用户展开“公司”部分时,您可以从/ company / API中提取剩余字段。

This way you will have lesser API calls for the hits where user doesn't look for Company details and your API will be light weight as well.

这样,对于用户不查找公司详细信息的命中,您将获得较少的API调用,并且您的API也将是轻量级的。