简体   繁体   English

使用RESTful API监听更新

[英]Listening for updates with RESTful APIs

I have a messenger app that makes GET /conversations requests to populate a list of the conversations of the user. 我有一个Messenger应用程序,该应用程序发出GET /conversations请求以填充用户对话列表。

The next step is to make it "listen" for updates so that it marks conversations that have been updated and add conversations that have been created. 下一步是使其“侦听”更新,以便它标记已更新的对话并添加已创建的对话。

Should I use the same /conversations resource to get the updates or should I rather have a separate resource for that? 我应该使用相同的/conversations资源来获取更新,还是应该使用单独的资源? Perhaps, something like /conversationUpdates . 也许像/conversationUpdates类的东西。

It depends on whether you want to follow RESTful conventions. 这取决于您是否要遵循RESTful约定。 Many client side libraries such as backbone and extjs have fairly deep support for declaring a resource with a URI and then using the different HTTP methods (GET, POST, DELETE, etc.) against it. 许多客户端库(例如骨架和extjs)都具有相当深的支持,可以使用URI声明资源,然后对它使用不同的HTTP方法(GET,POST,DELETE等)。 This might sometimes lessen the work clients need to do and folks will be grateful. 有时这可能会减少客户需要做的工作,并且人们会很感激。

Following the convention will also make your api less surprising. 遵循约定也将使您的api不那么令人惊讶。 There are undoubtedly other conventions for API's and not every domain space is well modeled with REST. 毫无疑问,API还有其他约定,并不是每个域空间都可以使用REST很好地建模。

Rereading your post, I see you want to have an api that that just gets new posts. 重新阅读您的文章,我发现您想拥有一个仅获取新文章的api。 What constitutes new? 什么是新的? New since the last time the client called the end point? 自上次客户呼叫终点以来的新消息? In such instances an api might accept a parameter like the last identifier that had been received (if you are using something like a auto increment field, or a mongodb id). 在这种情况下,api可能会接受一个参数,例如接收到的最后一个标识符(如果您使用的是自动递增字段或mongodb id之类的东西)。 In that case you would just use the /conversations endpoint, with an extra parameter. 在这种情况下,您只需要使用/conversations端点以及一个附加参数即可。

Firstly, I'd stick here with the GET method, since this is exactly the point: getting data. 首先,我会坚持使用GET方法,因为这正是重点:获取数据。

As for the resource name, I'd go with the same, specifying it further in the query, something like /conversations?state=new . 至于资源名称,我将使用相同的名称,在查询中进一步指定它,例如/conversations?state=new My point here is that the resource itself is still the same but you only want a subset of it. 我的意思是,资源本身仍然是相同的,但是您只需要它的一个子集。

However, if you plan on updating other things than conversations, you can use /updates/conversations since in this case, an update can be considered as a resource, itself composed of, among other things, conversations. 但是,如果您打算更新对话以外的其他内容,则可以使用/updates/conversations因为在这种情况下,更新可以被视为一种资源,其本身除其他外包括对话。

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

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