在这个具体案例中,我应该如何设计我的 RESTful API?
How should I design my RESTful API in this concrete case?
我正在用 Loopback.io 构建一个 API,我一直在思考我应该如何设计一些端点。
要了解上下文:
a Person hasMany Groups,
a Group hasMany People (and one of them is the admin),
a Member is the "Through" model for Person and Group,
a Group hasMany Sessions.
现在我有 (A):
- /人
- /人物/{id}/群组
- /组
- /组/{id}/会话
这是Loopback生成的API。 2 和 3 是 "repeated" 端点,但是 2 创建了一个 Member 实例而 3 没有。
选项 B:
- /人
- /人物/{id}/群组
- /People/{id}/Groups/{groupId}/Sessions
选项 C:
- /人
- /组
- /组/{id}/会话
我想知道哪种方法最好,A 解决方案是否足够好。
非常感谢。
选择最接近选项 C 的内容:
/people
/people/{person_id}
/groups
/groups/{group_id}
/sessions
/sessions/{session_id}
你的资源看起来像这样:
GET /people/1
{
"person_id": 1,
"groups": [
"/groups/5",
"/groups/7"
]
}
GET /groups/5 (Option 1 - if every group has exactly one admin)
{
"group_id": 5,
"admin": "/people/1",
"members": [
"/people/1",
"/people/4",
"/people/5"
],
"sessions": [
"/sessions/2",
"/sessions/3"
]
}
GET /groups/5 (Option 2 - if groups can have any number of admins)
{
"group_id": 5,
"members": [
{
"person": "/people/1",
"is_admin": true,
},
{
"person": "/people/4",
"is_admin": false
},
{
"person": "/people/5",
"is_admin": false
}
],
"sessions": [
"/sessions/2",
"/sessions/3"
]
}
GET /sessions/3
{
"session_id": 3,
"group": "/groups/5"
}
我的建议始终是避免嵌套 URI,除非有很好的理由支持这样做。
我正在用 Loopback.io 构建一个 API,我一直在思考我应该如何设计一些端点。 要了解上下文:
a Person hasMany Groups,
a Group hasMany People (and one of them is the admin),
a Member is the "Through" model for Person and Group,
a Group hasMany Sessions.
现在我有 (A):
- /人
- /人物/{id}/群组
- /组
- /组/{id}/会话
这是Loopback生成的API。 2 和 3 是 "repeated" 端点,但是 2 创建了一个 Member 实例而 3 没有。
选项 B:
- /人
- /人物/{id}/群组
- /People/{id}/Groups/{groupId}/Sessions
选项 C:
- /人
- /组
- /组/{id}/会话
我想知道哪种方法最好,A 解决方案是否足够好。 非常感谢。
选择最接近选项 C 的内容:
/people
/people/{person_id}
/groups
/groups/{group_id}
/sessions
/sessions/{session_id}
你的资源看起来像这样:
GET /people/1
{
"person_id": 1,
"groups": [
"/groups/5",
"/groups/7"
]
}
GET /groups/5 (Option 1 - if every group has exactly one admin)
{
"group_id": 5,
"admin": "/people/1",
"members": [
"/people/1",
"/people/4",
"/people/5"
],
"sessions": [
"/sessions/2",
"/sessions/3"
]
}
GET /groups/5 (Option 2 - if groups can have any number of admins)
{
"group_id": 5,
"members": [
{
"person": "/people/1",
"is_admin": true,
},
{
"person": "/people/4",
"is_admin": false
},
{
"person": "/people/5",
"is_admin": false
}
],
"sessions": [
"/sessions/2",
"/sessions/3"
]
}
GET /sessions/3
{
"session_id": 3,
"group": "/groups/5"
}
我的建议始终是避免嵌套 URI,除非有很好的理由支持这样做。