分层 RESTful URL 设计

发布于 2024-12-11 03:11:29 字数 1201 浏览 0 评论 0原文

我仔细阅读了有关此问题的问题,但仍然没有明确的答案。

我有一个应用程序,想要构建一个 RESTful API 来公开一部分信息。我有三个资源:

  1. 用户
  2. 报告
  3. 照片

用户有报告,报告有照片。照片不能存在于报表之外,报表也不能存在于用户之外。

我根据我的要求设计了以下 URL

用户登录,服务器使用在所有 API 调用的标头中发送的令牌进行响应

GET example.com/api/

获取用户信息

GET example.com/api/users/{username}

获取所有用户报告

GET example.com/api/users/{username}/reports

获取报告的所有照片

GET example.com/api/users/{username}/reports/{report_id}/photos

添加照片

POST example.com/api/users/{username}/reports/{report_id}/photos

删除照片

DELETE example.com/api/users/{username}/reports/{report_id}/photos/{photo_id}

修改照片描述

PUT example.com/api/users/{username}/reports/{report_id}/photos/{photo_id}

问题

  1. 在 URL 中添加资源 id 是否是一种好的做法,即资源/id,或者应该将其添加为查询参数?
  2. 这种链接资源(即资源/id/子资源/id/等)的方法是否可以接受且良好,或者我应该将所有资源放在顶层并使用查询参数指定其位置?

I have perused the questions asked about this, but I still don't have a definitive answer.

I have an application and would like to build a RESTful API to expose a subset of information. I have three resources:

  1. users
  2. reports
  3. photos

Users have reports and reports have photos. Photos cannot exist outside of reports and reports cannot exist outside of users.

I have designed the following URLs for my requirements

User login, server responds with token which is sent in the header of all API calls

GET example.com/api/

Get user info

GET example.com/api/users/{username}

Get all user reports

GET example.com/api/users/{username}/reports

Get all photos of a report

GET example.com/api/users/{username}/reports/{report_id}/photos

Add a photo

POST example.com/api/users/{username}/reports/{report_id}/photos

Delete a photo

DELETE example.com/api/users/{username}/reports/{report_id}/photos/{photo_id}

Modify photo description

PUT example.com/api/users/{username}/reports/{report_id}/photos/{photo_id}

Questions

  1. Is it good practice to add a resource id in the URL, i.e. resource/id, or should this rather be added as a query parameter?
  2. Is this method of chaining resources, i.e. resource/id/sub-resource/id/etc., acceptable and good or should I put all my resources at the top level and specify its position with query parameters?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时间你老了 2024-12-18 03:11:29

这个设计没有错。但是这会创建很长的 URL,有时很难理解,并且 API 的用户需要了解层次结构。此外,API 的使用者需要以一点点非标准的方式编写更多代码(即使它可以完成,但会有点混乱)。从不同的角度思考这个问题
您拥有三个资源,每个资源都有自己的身份。因此,如果我们重构上述 URI,它将如下所示(我仅演示 GET)

用户资源:

获取用户列表

  GET example.com/api/users

获取特定用户

  GET example.com/api/users/{username}

报告资源:

获取所有报告

 GET example.com/api/reports

获取特定报告

 GET example.com/api/reports/{report_id}

照片资源

照片

GET example.com/api/photos

特定照片

GET example.com/api/photos/{photo_id}

用户 所有报告

GET example.com/api/reports?username={userName}

用户的特定报告

GET example.com/api/report?username={userName}&report_id={reportId}

用户 所有照片

GET example.com/api/photos?username={userName}

用户 报告 ID 的所有照片(如果 report_id 与用户无关,则您可能不需要用户名,这将进一步简化 URI)

GET example.com/api/photos?username={userName}&report_id={reportId}

所有 报告

GET example.com/api/photos?report_id={reportId}

这个简化了理解,并且可以使用这种方法在消费者端编写更标准的代码。

Nothing wrong in this design.But this creates long URL which sometime are difficult to understand and the user of the API needs to know the hierarchy.Moreover the consumer of the API need to write more code in little bit non-standard way(Even though it can be done, but will be little messy). Think this from a different perspective
You have three resources and each has its own identity.So if we refactor the above URI's it will looks like below (I am demonstrating only GET)

User Resource:

Get users list

  GET example.com/api/users

Get specific user

  GET example.com/api/users/{username}

Report Resource:

Get all reports

 GET example.com/api/reports

Get a specific report

 GET example.com/api/reports/{report_id}

Photo Resources

All Photos

GET example.com/api/photos

Specific Photo

GET example.com/api/photos/{photo_id}

User All Reports

GET example.com/api/reports?username={userName}

Specific report of a user

GET example.com/api/report?username={userName}&report_id={reportId}

User All Photos

GET example.com/api/photos?username={userName}

User All Photos for a report id (You may not need user Name if report_id is unique irrespective of the user, which will further simplify the URI)

GET example.com/api/photos?username={userName}&report_id={reportId}

All photos of a report

GET example.com/api/photos?report_id={reportId}

This simplifies the understanding and more standard code can be written on the consumer side using this approach.

谁把谁当真 2024-12-18 03:11:29

恕我直言,你建模得很好。

关于 1 我宁愿使用 resource/id 而不是查询参数。但建模时必须记住的一件事是代理的缓存机制等等。所以不要忘记标题。

我寻找用于过滤和排序的查询参数。

关于登录,凭据应位于标头中,不需要特定资源。只需根据资源安全性应用即可。

IMHO you are modelling it well.

Regarding 1 I'd rather go with resource/id rather than query param. But one thing you must have in mind when modelling is the cache mechanism by proxy and so on. So do not forget the headers.

I go for query params for filtering and those sorts.

About the login, the credentials should be in the headers, and no specific resource is needed. Just apply per resource security.

随梦而飞# 2024-12-18 03:11:29

我不认为你的计划有什么问题。

现在大多数框架都使用类似的标准来指定 url(例如 Django)。

在我个人看来,它使 URL 更具可读性,对用户来说也更友好。

I don't see anything wrong with your scheme.

Most frameworks nowadays use a similar standard for specifying url's (like Django).

In my personal opinion, it makes the URL more readable and a bit nicer for the user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文