简体   繁体   English

REST API设计汇总

[英]REST API design aggregate

We are creating api on employee manage app. 我们正在员工管理应用程序上创建api。 So there is schedule in interface, where we have to show all shifts in table, for every user per row. 因此,在界面中有时间表,我们必须在表中显示每行每个用户的所有班次。 Farther, there is summary for every user (per row) and day (per column). 此外,每个用户(每行)和日期(每列)都有摘要。 Should we create one big aggregate call like: 我们是否应该创建一个大型集合调用,例如:

GET /api/locations/{id}/shedule

which will return all employees, shifts, summaries etc. Or maybe should we smash that to several collections like: 这将返回所有员工,轮班,总结等。或者也许我们应该将其粉碎到几个集合中,例如:

GET /api/locations/{id}/shifts
GET /api/locations/{id}/events
GET /api/locations/{id}/summary
GET /api/employee/{id}/summary?date_from={date_from}&date_from={date_to}

For me, second option is more flexible and there is no reason to create new abstract resource, which is shedule. 对我来说,第二种选择更灵活,没有理由创建新的抽象资源,这是有问题的。 In my opinion it is clearly part of interface layer and should not affect on API design. 我认为它显然是接口层的一部分,不应影响API设计。

On the other hand the big aggregate is more optimal, becouse there will be less database calls and it's easy to cache. 另一方面,大的聚合是最佳的,因为将减少数据库调用并且易于缓存。

How do you think? 你怎么想? Is there any source, kind of article, which can I rely on? 是否可以依靠任何来源,文章?

There's nothing RESTful or unRESTful about either approach. 两种方法都没有RESTful或UnRESTful的特性。 The semantics of URIs is irrelevant to REST. URI的语义与REST无关。 What really matters is how the clients obtain the URIs. 真正重要的是客户端如何获取URI。 If they are looking up URI patterns in documentation and filling up values instead of using links, the API is not RESTful. 如果他们在文档中查找URI模式并填充值而不是使用链接,则该API不是RESTful的。

With that in mind, I'd say the approach that is more consistent with your business and your ecosystem of applications is the best one. 考虑到这一点,我认为与您的业务和应用程序生态系统更一致的方法是最好的方法。 Don't be afraid to create aggregate resources if you feel the need to. 如果需要,不要害怕创建聚合资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM