API 命名限制结果?
在我们的办公室,我们正在就公共 API 中一些参数的命名进行激烈的讨论。
在我们为某种列表提供 getter 的地方,我们希望能够限制结果(用于分页)。
即: getPosts/getNews/getUsers/.../
应该能够返回 0 到 25、50 到 100 等项目。 所以,我们需要两个参数。 第一个参数是我们开始的地方,第二个参数是我们结束的地方。
那么,第二个参数应该是绝对的还是相对的? (50,75 应该返回项目 50-75 还是 50-125?)
我们已经决定相对,所以第二个参数告诉我们要返回多少个项目。
棘手的部分是以清晰一致的方式命名。 到目前为止我们提出的建议:
第一:
start/index/offset/page/pageindex
第二:
amount/count/maxcount/limit/perpage/pagesize
每个建议都有优点和缺点。 使用“page”会让人不清楚它是从0还是1开始。使用“index”或“offset”在这方面会更清楚,但更具技术性(毕竟,99%的用户都使用它来分页)时间)。
我想这是非常主观的,但我正在寻找明确的优点和缺点,以及可能的良好 API 的示例以及它们正在使用的内容。
At our office, we are having heated discussions regarding naming some arguments in our public API.
Everywhere we are providing getters to lists of some kind, we want to be able to limit results (for pagination).
Ie: getPosts/getNews/getUsers/.../
should be able to return items 0 to 25, 50 to 100 etc.
So, we need two parameters. First parameter is where we start, and second parameter is where we end.
So, should the second parameter be absolute or relative?
(Should 50,75 return items 50-75 or 50-125?)
We have decided on relative, so the second parameter tells us how many items to return.
The tricky part is naming this in a clear and consistent way. The ones we have come up with so far:
First:
start/index/offset/page/pageindex
Second:
amount/count/maxcount/limit/perpage/pagesize
There are pros and cons to every suggestion. Using "page" would make it unclear on whether it starts on 0 or 1. Using "index" or "offset" would be clearer in that regard, but more technical (after all, users are using this for pagination in 99% of the time).
I guess this is highly subjective, but Im looking for clear pros and cons, and possibly examples of good APIs and what they are using.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会去找亲戚,这样更容易掌握。
关于命名:
我喜欢“开始”表示第一个结果,“计数”表示结果数量。
(但是你不是以某种方式混合了页面和结果计数吗?)
I would go for relative, it's easier to grasp.
regarding the naming:
I like "start" for the first result and "count" for the number of results.
(but aren't you somehow mixing page and results counts?)
肯定是走亲戚了。 我将它们称为“索引”和“计数”。 通常我会使用“长度”,但它似乎不适合你所描述的内容。 如果你想让它类似于 linq,你可以调用参数“skip”和“take”。 对于要查看生成的 URL 的普通人来说,这些术语也更容易理解。
Definitely go relative. I'd call them "index" and "count". Normally I'd use "length", but it doesn't seem to fit for what you're describing. If you want to make it similar to linq, you could call the parameters "skip" and "take". These terms are also more approachable for the Joe Average guy who's going to be looking at the resulting URL.
我会和
I would go with