2008年6月15日星期日

RESTful Web Services 中文版 读书摘要 第四章

P81
任何事物,只要具有被引用的必要,它就是一个资源。
----------------------------
A resource is anything that’s important enough to be referenced as a thing in itself.

P81
是什么令资源称得上是一个资源?它必须至少有一个 URI。URI 既是资源的名称,也是资源的地址。如果一则信息没有 URI ,它就不能算是一个资源,也不能算真正在 Web 上,而只能算作描述另一个资源的一些数据。
----------------------------
What makes a resource a resource? It has to have at least one URI. The URI is the name and address of a resource. If a piece of information doesn’t have a URI, it’s not a resource and it’s not really on the Web, except as a bit of data describing some other resource.

P86
在最终用户看来,Gmail 的 URI 始终是 https://mail.google.com/。无论作什么操作,无论从 Gmail 获取什么信息或向 Gmail 上传什么信息,你不会看到别的 URI。
Gmail有一个可寻址的版本,位于 URI https://mail.google.com/mail/?ui=html。
Gmail Web 服务是可寻址的,不过调用该服务的 Gmail Web 应用不是可寻址的。
----------------------------
From the end-user perspective, there is only one Gmail URI: https://mail.google.com/. Whatever you do, whatever pieces of information you retrieve from or upload to Gmail, you’ll never see a different URI.
Compare the Ajax interface against the more addressable version of Gmail you get by starting off at the URI https://mail.google.com/mail/?ui=html.
The Gmail web service is addressable, but the Gmail web application that uses it is not.

P90
无状态性意味着:有一种状态,是服务器不应该保存的。实际上,状态分为两种。从现在开始,我将区分应用状态与资源状态:前者应该保存在客户端,后者应该保存在服务端。
----------------------------
Statelessness implies there’s only one kind of state and that the server should go without it. Actually, there are two kinds of state. From this point on in the book I’m going to distinguish between application state, which ought to live on the client, and resource state, which ought to live on the server.

P90
每当客户端向服务器发请求时,请求里必须包含服务器处理该请求所需的所有应用状态。
各个客户端应当管理自己的应用状态。
----------------------------
This means that whenever a client makes a request, it must include all the application states the server will need to process it.
The client should be in charge of managing its own path through the application.

P95
将超媒体作为应用状态的引擎。
客户端应用状态在服务器提供的超媒体(即超文本表示里的链接和表单)的指引下发生变迁。
----------------------------
Hypermedia as the engine of application state.
The server guides the client’s path by serving “hypermedia”: links and forms inside hypertext representations.

P97
HTTP四种基本方法:
获取资源的一个表示:HTTP GET
创建一个新资源:向一个新 URI 发送 HTTP PUT,或者向一个已有 URI 发送 HTTP POST
修改已有资源:向已有 URI 发送 HTTP PUT
删除已有资源:HTTP DELETE
----------------------------
Retrieve a representation of a resource: HTTP GET
Create a new resource: HTTP PUT to a new URI, or HTTP POST to an existing URI
Modify an existing resource: HTTP PUT to an existing URI
Delete an existing resource: HTTP DELETE

P98
HEAD 请求就是比 GET 请求少返回实体主体而已。
----------------------------
HEAD gives you exactly what a GET request would give you, but without the entity-body.

P99
在一个 REST 式设计中,POST 通常被用于创建从属资源——即从属于“父”资源(另一个资源)而存在的资源。
----------------------------
In a RESTful design, POST is commonly used to create subordinate resources: resources that exist in relation to some other “parent” resource.

P99
PUT 与 POST 的区别就在于:假如是客户端负责决定新资源采用什么 URI,那就用 PUT;假如是服务器负责新资源采用什么 URI,那就用 POST。
----------------------------
The difference between PUT and POST is this: the client uses PUT when it’s in charge of deciding which URI the new resource should have. The client uses POST when the server is in charge of deciding which URI the new resource should have.

P100
给一个资源发 POST 请求,未必总是创建一个全新的从属资源。有时,当向一个资源 POST 数据时,它把你发来的信息附加到它自身的状态上,而不是用该信息新建一个资源。
----------------------------
The information conveyed in a POST to a resource doesn’t have to result in a whole new subordinate resource. Sometimes when you POST data to a resource, it appends the information you POSTed to its own state, instead of putting it in a new resource.

P102
假如对一个资源做某操作是幂等的,那么无论对该资源做多少次这种操作,结果总是一样的;对该资源的第二次及其后各次操作,不会令该资源的状态产生比第一次操作更多的影响。
----------------------------
an operation on a resource is idempotent if making one request is the same as making a series of identical requests. The second and subsequent requests leave the resource state in exactly the same state as the first request did.

P103
幂等性对实践提出的要求是:不要允许客户端对资源状态做相对的改动。比如一个资源把某个数值作为其部分资源状态来保存,客户端可以通过 PUT 操作把该数值设为 4、0 或 -50,但是它不应通过 PUT 操作对该值作“加一”操作。
----------------------------
The practical upshot of this is that you shouldn’t allow your clients to PUT representations that change a resource’s state in relative terms. If a resource keeps a numeric value as part of its resource state, a client might use PUT to set that value to 4, or 0, or -50, but not to increment that value by 1.

P103
POST 既不是安全的,也不是幂等的。
----------------------------
POST is neither safe nor idempotent.

没有评论: