简体   繁体   English

Rest API为同一资源设计多个URL?

[英]Rest API design multiple URLs to same resource?

In my API I have the following resource: 在我的API中,我有以下资源:

example.com/api/v1/users/me/sites
example.com/api/v1/users/123/sites

Which returns all sites for the current or given user. 返回当前用户或给定用户的所有站点。

Now, what if I want to fetch all sites, and ignore users. 现在,如果我想获取所有网站并忽略用户该怎么办? I'd assume that would have the following URL: 我假设有以下网址:

example.com/api/v1/sites

Which then strikes me as somewhat odd to have sites as a root resource and as a subresource for users. 然后,让网站作为根资源和用户的子资源有点奇怪。

Is this an OK approach or is it common to do this some other way? 这是一个好的方法还是以其他方式做到这一点很常见?

Or should it be something like: 或者应该是这样的:

example.com/api/v1/users/sites

Where there is no Id for the users? 哪些用户没有ID?

For me, as a rule of thumb, I look at what I'm returning from the API and this usually informs what I'm representing. 对我来说,作为一个经验法则,我看看我从API返回的内容,这通常会告诉我我代表什么。

For example, you are actually returning sites, not users for the URL: example.com/api/v1/users/123/sites. 例如,您实际上是返回网站,而不是URL的用户:example.com/api/v1/users/123/sites。 In this case, I would prefer to address sites, as that is what is being returned. 在这种情况下,我更愿意解决网站,因为这是返回的内容。

So, I would opt for this URL: example.com/api/v1/sites?user=123 所以,我会选择这个URL: example.com/api/v1/sites?user=123

IMHO, this is utilizing the query string as an actual query for sites. 恕我直言,这是利用查询字符串作为网站的实际查询。

So, for the site representation I would build it up like this: 所以,对于网站表示,我会像这样构建它:

  1. All sites: example.com/api/v1/sites 所有网站: example.com/api/v1/sites

  2. A particular site: example.com/api/v1/sites/1 特定站点: example.com/api/v1/sites/1

  3. A user's site: example.com/api/v1/sites?user=123 用户的站点: example.com/api/v1/sites? user= 123

  4. Current user's site: example.com/api/v1/sites?user=current 当前用户的网站: example.com/api/v1/sites? user=current

There is no problem with having 有没有问题

example.com/api/v1/sites

In addtion to 另外还有

example.com/api/v1/users

especially each seems to be a root aggregate. 特别是每个似乎都是根聚合。

So if association is composition rather than aggregation (between site and user) then it is not only OK, but also correct to have them as the root URL fragment. 因此,如果关联是组合而不是聚合 (在站点和用户之间),那么它不仅可以,而且还可以将它们作为根URL片段。

I wanted to add something to this interesting question. 我想在这个有趣的问题上添加一些内容。

You could for example find out which user "owns" a certain site by doing 例如,您可以通过执行查找哪个用户“拥有”某个站点

example.com/api/v1/sites/[site_id]/user

This would return the user resource that owns site with site_id. 这将返回拥有site_id的站点的用户资源。

Another example could be users and addresses. 另一个例子可以是用户和地址。 If you allow users to share an address resource (addresses can have several users) you could do this: 如果您允许用户共享地址资源(地址可以有多个用户),您可以这样做:

example.com/api/v1/users/[user_id]/address => returns address for user with user_id. example.com/api/v1/users/[user_id]/address =>返回user_id用户的地址。 Doing a DELETE operation on that path will not delete the whole address resource but merely the connection between the user and the address. 在该路径上执行DELETE操作不会删除整个地址资源,而只会删除用户和地址之间的连接。 To delete the address itself you should do DELETE on example.com/api/v1/addresses/[address_id] 要删除地址本身,您应该在example.com/api/v1/addresses/[address_id]上执行DELETE操作

example.com/api/v1/addresses/[address_id]/users => returns users that live at that address and similarly as deleting the address from a user you can also delete a user from an address with DELETE on example.com/api/v1/addresses/[address_id]/users/[user_id] example.com/api/v1/addresses/[address_id]/users =>返回居住在该地址的用户,类似于从用户删除地址,您也可以在example.com/api/v1/addresses/[address_id]/users/[user_id]上使用DELETE从地址中删除用户example.com/api/v1/addresses/[address_id]/users/[user_id]

The relationship between Address and User (what @Aliostad refers to as association as composition) itself is a resource and can be accessed and also deleted without deleting the resources themselves. 地址和用户之间的关系(@Aliostad称为关联作为组合)本身就是一种资源,可以在不删除资源本身的情况下访问和删除。

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

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