关于为简单票务系统设计 RESTful API 的建议
Advice on designing a RESTful API for a simple ticket system
最近开始学习围棋了。为了练习我新学到的技能,我决定开始一个小的副项目。在这个项目中,我想为票务系统设计和构建一个 RESTful API。由于我对这个主题还很陌生,所以我正在寻找有关如何正确完成它的建议。系统应该能够创建可供注册和未注册用户预订的新事件。因此,我为 API 想到了以下结构:
login:
/api/login/
method POST
logout:
/api/logout/
method POST
users:
/api/users/create (create user)
method POST
/api/users/{id} (get, update and delete user)
method GET, PUT, DELETE
/api/users (get all user)
method GET
events:
/api/events/create (create new event)
method POST
/api/events/{id} (get, update and delete event)
method GET, PUT, DELETE
/api/events (get all events)
method GET
bookings:
/api/bookings/create (create new booking for a certain event)
method POST
/api/bookings/{id} (get, update and delete booking)
method GET, PUT, DELETE
/api/bookings (get all bookings for a certain event)
method GET
如何改进这个结构?你会怎么做?
您将 /create
集成到您的创作网址中是否有特定原因?
资源通常是通过 POST 到根资源创建的。例如:
users:
/api/users (create user) <-- change here
method POST
/api/users/{id} (get, update and delete user)
method GET, PUT, DELETE
/api/users (get all user)
method GET
same for others ...
不要忘记在成功创建资源后使用 HTTP 201 和 Location-Header 进行响应。
预订和活动如何相互关联?
如果预订始终属于特定事件,您可以将预订建模为事件的子资源。
例如:
POST /api/events/<id>/bookings <-- Create a booking for a specific event
GET /api/events/<id>/bookings <-- Get all bookings for an event
GET /api/events/<id>/bookings/<id> <-- Get specific booking
PUT /api/events/<id>/bookings/<id> <-- Update booking
最近开始学习围棋了。为了练习我新学到的技能,我决定开始一个小的副项目。在这个项目中,我想为票务系统设计和构建一个 RESTful API。由于我对这个主题还很陌生,所以我正在寻找有关如何正确完成它的建议。系统应该能够创建可供注册和未注册用户预订的新事件。因此,我为 API 想到了以下结构:
login:
/api/login/
method POST
logout:
/api/logout/
method POST
users:
/api/users/create (create user)
method POST
/api/users/{id} (get, update and delete user)
method GET, PUT, DELETE
/api/users (get all user)
method GET
events:
/api/events/create (create new event)
method POST
/api/events/{id} (get, update and delete event)
method GET, PUT, DELETE
/api/events (get all events)
method GET
bookings:
/api/bookings/create (create new booking for a certain event)
method POST
/api/bookings/{id} (get, update and delete booking)
method GET, PUT, DELETE
/api/bookings (get all bookings for a certain event)
method GET
如何改进这个结构?你会怎么做?
您将 /create
集成到您的创作网址中是否有特定原因?
资源通常是通过 POST 到根资源创建的。例如:
users:
/api/users (create user) <-- change here
method POST
/api/users/{id} (get, update and delete user)
method GET, PUT, DELETE
/api/users (get all user)
method GET
same for others ...
不要忘记在成功创建资源后使用 HTTP 201 和 Location-Header 进行响应。
预订和活动如何相互关联? 如果预订始终属于特定事件,您可以将预订建模为事件的子资源。
例如:
POST /api/events/<id>/bookings <-- Create a booking for a specific event
GET /api/events/<id>/bookings <-- Get all bookings for an event
GET /api/events/<id>/bookings/<id> <-- Get specific booking
PUT /api/events/<id>/bookings/<id> <-- Update booking