什么是RESTful方式来访问附加到不同资源的资源

时间:2013-04-07 14:58:43

标签: python rest flask

我的数据模式类似于

User 很多 TransactionUserTransaction都是数据库中的不同表格 我有REST个端点为

用户端点

GET /users/uuid # to get information about specific user
PUT /users/uuid # update information of a specific user  

交易终点

GET /transactions/uuid # get a specific transaction
GET /transactions/user_uuid # get all transactions of a specific user
GET /transactions/user_uuid/yyyy # get all transactions of a specific user in year yyyy
GET /transactions/user_uuid/yyyy/mm # get all transactions of a specific user in year yyyy, month mm
GET /transactions/user_uuid/yyyy/mm/dd # get all transactions of a specific user in year yyyy, month mm and day dd
  • 我的问题是,transactions始终会附加User资源,为它们设置不同的端点是否有意义?
  • 建议访问用户的交易?上面贴的那张?或类似的东西
GET /users/uuid/transactions # all transactions for a user
GET /users/uuid/transactions/yyyy # all transactions for a user for year yyyy
GET /users/uuid/transactions/yyyy/mm # all transactions for a user for year yyyy, month mm
GET /users/uuid/transactions/yyyy/mm/dd # all transactions for a user for year yyyy, month mm and day dd

此刻我很困惑哪一个更好。

谢谢

1 个答案:

答案 0 :(得分:1)

如果您始终在用户的上下文中访问transactions,那么对您来说第二种方法是有意义的,因为正如您所说,用户有很多transactions,您的第一种方法是网址设计可能相当令人困惑,因为它不清楚日期部分引用transactions而不是用户资源。另一方面,如果您想要为所有用户阅读所有transactions,可以理解您有transactions的两个端点

GET /transactions/ # reads all transactions for all users
GET /transactions/yyyy # reads all trasnsaction for all users with date 
GET /users/uuid/transactions # reads all transaction for a particular user
GET /users/uuid/transactions/yyyy # reads all transactions for a particular user with date