REST API设计以检索摘要信息
我有一个方案,其中我有一个REST API,该API管理一个称为组"的资源. 网上论坛的概念类似于Google网上论坛中的讨论论坛.
I have a scenario in which I have REST API which manages a Resource which we will call Group. A Group is similar in concept to a discussion forum in Google Groups.
现在我有两种GET访问方法,我认为它们需要单独的表示形式.
Now I have two GET access method which I believe needs separate representations.
第一个GET访问方法检索有关组的最少信息. 给定一个 group_id ,它应该返回少量信息,例如
The 1st GET access method retrieves the minimal amount of information about a Group. Given a group_id it should return a minimal amount of information like
{
group_id: "5t7yu8i9io0op",
group_name: "Android Developers",
is_moderated: true,
number_of_users: 34,
new_messages: 5,
icon: "http://boo.com/pic.png"
}
第二种GET访问方法检索本质上更具统计性的摘要信息,例如:
The 2nd GET access method retrives summary information which are more statistical in nature like:
{
group_id: "5t7yu8i9io0op",
top_ranking_users: {
[ { user: "george", posts: 789, rank: 1 },
{ user: "joel", posts: 560, rank: 2 } ...]
},
popular_topics: {
[ ... ]
}
}
我想将这些数据访问方法分开,并且我目前正在计划这种设计:
I want to separate these data access methods and I'm currently planning on this design:
GET /group/:group_id/
GET /group/:group_id/stat
只有后者会返回有关该组的统计信息.您对此有何看法?
Only the latter will return the statistical information about the group. What do you think about this ?
我认为您的方法没有问题.由于统计信息基本上是单独的数据,因此您也可以将统计信息视为单独的资源,并提供
I don't see a problem with your approach. Since the statistics are basically separate data, you could treat the stats as a separate resource, too, providing a URI like
GET /stat/:group_id
此外,您可以交叉引用您的资源(这意味着一个组链接到相应的统计资源,反之亦然):
Additionally you can cross reference your resources (meaning a group links to the corresponding stat resource and vice versa):
GET /group/5t7yu8i9io0op
{
group_id: "5t7yu8i9io0op",
group_name: "Android Developers",
is_moderated: true,
number_of_users: 34,
new_messages: 5,
icon: "http://boo.com/pic.png",
stats: "http://mydomain.com/stat/5t7yu8i9io0op"
}
GET /stat/5t7yu8i9io0op
{
group: "http://mydomain.com/group/5t7yu8i9io0op",
top_ranking_users: {
[ { user: "george", posts: 789, rank: 1 },
{ user: "joel", posts: 560, rank: 2 } ...]
},
popular_topics: {
[ ... ]
}
}