简体   繁体   English

REST API-通用或特定资源

[英]REST API - Generic or specific resources

I am currently working on a project that has a resource for sending things. 我目前正在一个项目中,该项目具有发送事物的资源。

Let's say the URI looks like this http://api.somename.com/thingssender with only the POST verb. 假设URI看起来像http://api.somename.com/thingssender ,仅包含POST动词。

Over time this has become a resource that in the payload has several 'optional' elements that only apply to sending one type of thing. 随着时间的流逝,这已成为一种资源,有效负载中具有多个“可选”元素,这些元素仅适用于发送一种类型的事物。

Currently this resource handles two different types of things (ThingA and ThingB) but also varies on the content in a sub-element. 当前,该资源处理两种不同类型的事物(ThingA和ThingB),但子元素的内容也有所不同。 So for example you can send many things or a single thing to a group of people. 因此,例如,您可以将很多东西或一件东西发送给一群人。

A requirement has emerged that a ThingC is required. 已经出现了需要ThingC的要求。 Given that this endpoint is likely to take a ThingD in the future with optional data in the payload, I'm starting to question this 'catch-all' resource. 鉴于此端点将来可能会在有效负载中带有可选数据的情况下使用ThingD,因此我开始质疑这种“包罗万象”的资源。

I'm worried that the resource will keep growing in complexity not only for developers but for the clients using the REST API. 我担心该资源的复杂性将持续增长,不仅对开发人员而言,对于使用REST API的客户端而言也是如此。 To me this will just cause confusion and not create the best experience (again for both developer and client). 对我来说,这只会造成混乱,而不能创造最佳体验(同样适用于开发人员和客户)。

What are peoples thoughts on creating a generic endpoint versus a specific endpoint? 人们对于创建通用端点还是特定端点有何想法?

So a specific endpoint for sending ThingCs ( http://api.somename.com/ThingsCSender ). 因此,用于发送ThingC的特定端点( http://api.somename.com/ThingsCSender )。 It screams single responsibility to me and with careful consideration to the underlying implementation should not cause any repeteated functionality. 它向我发出了一个单一的责任,并且仔细考虑了基础实现不应导致任何重复的功能。

(The examples given are not real concepts or resources in the project :p !) (给出的示例不是项目中的实际概念或资源:p!)

Cheers 干杯

You are correct in the fact that each resource should have its own URI. 您是正确的,因为每个资源都应具有自己的URI。 For more information, if interested there are some great answers on stackoverflow: 有关更多信息,如果有兴趣,可以在stackoverflow上找到一些很好的答案:

Understanding REST: Verbs, error codes, and authentication 了解REST:动词,错误代码和身份验证

What exactly is RESTful programming? RESTful编程到底是什么?

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

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