Twitter API *真的*是 RESTful 吗? [英] Is the Twitter API *really* RESTful?

查看:45
本文介绍了Twitter API *真的*是 RESTful 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

和一半的 Web 开发人员社区一样,我一直在努力真正地理解 REST 风格.更具体地说,我一直试图就纯 RESTful 架构在 Web 浏览器和应用程序服务器之间的实用性提出一些意见.

Along with half of the web developer community, I've been struggling to really and truly grok the REST style. More specifically, I've been trying to form some opinions on how practical a pure RESTful architecture really is between a web browser and an application server.

作为我学习努力的一部分,我一直在查看 REST 的一些在线示例,特别是在这种情况下的 Twitter.在他们的 API 文档中,他们讨论了各种REST API 方法".

As part of my learning endeavor, I've been taking a look at some online examples of REST, specifically Twitter in this case. In their API documentation, they discuss their various "REST API Methods".

除了具有 RESTful URL 结构之外,我正在努力合理化其中大多数实际上是 RESTful 的方式.例如,考虑一个对 http://twitter.com/favorites 的简单 GET 请求.

I'm struggling with rationalizing how exactly most of these are actually RESTful, beyond having a RESTful URL structure. Consider, for example, a simple GET request to http://twitter.com/favorites.

在纯粹的 REST 实现中,我希望对该 URL 的相同请求,无论发起客户端如何,都会返回相同的响应.但是,在这种特殊情况下,我们显然会根据当前已通过身份验证的用户看到不同的响应,这意味着在生成响应之前,我们的请求已连接到服务器上的某种形式的客户端状态.

In a pure implementation of REST, I would expect identical requests to that URL, regardless of the initiating client, to return identical responses. In this particular case, though, we would obviously all see different responses depending on our currently authenticated users, which implies that our requests are being connected to some form of client state on the server before a response can be generated.

希望这为我的问题提供了足够的上下文 - 这真的可以称为REST"吗?我的印象是,Web 浏览器和应用服务器之间 90% 的所谓 RESTful 实现都表现出同样的不一致,其中对存储在服务器上的客户端状态的限制被忽略了.

Hopefully that provides enough context for my question then - can that really be called "REST"? I get the impression that 90% of the so-called RESTful implementations between web browsers and application servers demonstrate this same inconsistency, where the restrictions on client state stored on the server are ignored.

推荐答案

Twitter 打破了几乎所有 REST 约束.您的 http://twitter.com/favorites 根据经过身份验证的用户返回不同结果的示例是 Twitter 违反资源标识"约束的示例.每个有趣的资源都应该有一个唯一的标识符.我的 Twitter 收藏夹和你的 Twitter 收藏夹是两种不同的资源,因此应该有两个不同的 URI.

Twitter breaks pretty much every REST constraint. Your example of http://twitter.com/favorites returning different results based on the authenticated user is an example of Twitter violating the "Resource Identification" constraint. Each interesting resource should have a unique identifier. My Twitter favorites and your Twitter favorites are two different resources and therefore should have two different URIs.

这实际上与幂等性完全无关.幂等性是关于能够多次发出相同的请求并且具有相同的效果.甚至 Twitter 也尊重幂等性.如果我多次获得我的收藏夹,我仍然可以找回我的收藏夹.GET 多少次不会影响结果.

This actually is not related to idempotency at all. Idempotency is about being able to make the same request multiple times and it have the same effect. Even Twitter respects idempotency. If I GET my favorites multiple times, I still get my favorites back. How many times I do GET does not affect the result.

Twitter 打破 REST 限制的方式还有很多.其中许多问题之前已在 SO 上讨论过.

There are many other ways in which Twitter break the REST constraints. Many of these issues have been covered here on SO before.

更新在仔细阅读 Twitter api 文档之后,实际上有一种替代 URI 格式可以正确识别收藏夹资源.这里 他们展示了如何创建一个 URL,如:

Update After perusing the Twitter api docs a bit more there is actually an alternative URI format that does properly identify the favourites resource. Here they show how to create an URL like:

http://api.twitter.com/1/favorites/bob.json

距离 RESTful 还有很长的路要走,但至少这是朝着正确方向迈出的一步.

It still is a long way from being RESTful, but at least that's a step in the right direction.

这篇关于Twitter API *真的*是 RESTful 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆