« Return to Thread: Our REST implementation

Re: Our REST implementation

by Victor Farazdagi :: Rate this Message:

Reply to Author | View in Thread

Well,
Although REST is certainly not a specification and does not go and
define a protocol, there's a conventional REST model that is based on
some well known protocols and specs. These include (you are always free
to redefine set, just follow the REST principles):

URI - as REST design style disjoints an application into semantic
resources, everything is identified and located via URL, which although
should not be analized by client application still MUST correlate to a
resource it represents in a meaningful way. So, huge URIs with a long
query strings are not welcome. Need to define some query, define it as
accessible resource. I think that Davey's assumption that REST is still
RPC, is mis-leading at least. Granted, REST service is to accept some
sort of manipulation and really does invoke some server-side code, but
there no procedure mapping like it's done in conventional RPC system,
say XML-RPC. REST is less about sending requests and getting responses,
from service dev. point of view, but is about constructing resources -
others layers are handled via well-known protocols. And as such, there
must be a resource abstraction in any valuable REST service builder
library, say abstract class on top of which other resources would be
defined. As it stands now, service is more procedure oriented than
resource-oriented and that's not the very expected by any REST service
developer (and it really messes things up, as we are forced to think in
terms of procedure and their mappings - instead of considering the
REsourceST). So, if the package should not be redesigned, then it should
really be extended - at least with Resource abstraction (some but not
all funcs of which is carried out by Rest/Server.php now).

HTTP (both for available verbs and response status codes). Although
again not dictated by Fielding's text, considering that author has
contributed to the specs, and constantly deploys the slang referencing
to how HTTP handles things, I suppose it's quite a safe bet to use it.
As such it is present in current Zend/Server.php but is an integral part
of it - for the REST does not force one to use HTTP methods for
acceptable state transfer protocol, this usage should be abstracted, so
that it could be easily substituted if service dev. would go with some
other protocol. BUT, this could be left intact as well - I really
haven't seen anyone going past the HTTP.

Content Types - well the last point of so called REST Triangle
(Resources - represented as URIs and unconstrained, Verbs - could be
represented by intentionally limited set of HTTP methods, Content Types
- generally MIME types are deployed to describe the content media of
transfered resource state). Although XML is a safe bet for API -lovers,
as you can represent almost anything with it, REST is not only about
sending XML back and forth it's also about caching - so if you represent
some image in your XML as base64 string, and the rest of values found in
that document are highly dynamic - you are killing one of the purposes
REST is claimed to be a successful theme - caching. It's better to
include the link to your image file, and make actual image available as
yet another resource with well-known content type (png, jpeg whatever),
so that intermediaries in REST talking would be able to handle saving
and reusing the cache. Shahar makes extremely good point by saying that
even HTTP's status codes (201, 204, 205 etc) would be enough for a REST
response. Should user want some fancy "die hacker!" message be sent, it
should be done via extension of resource base class.

To sum up: REST is about practices, and violating those is not the best
way of implementing the service you happen to call RESTyful. Just by
looking at current class, it's at least an insufficient presentation of
building blocks for the REST service. Btw, imo, client side's interface
(Rest/Client.php) is quite good and could safely left intact - although
I prefer to use LiveHeaders (http://livehttpheaders.mozdev.org/) for
Firefox - they just make your browser a little bit more REST-enabled.

Thanks,
Victor

P.S.Btw, Shahar, R. Fielding loves to define the REST not as
architecture but as architectural style, with architecture being more
specific (as in specifications), while style just a set of genius :)
insights on how distributed systems become popular :)

> One note about my example, which might be confusing: I mention SOAP as
> one example of an RPC-style protocol. AFAIK REST does not define
> specific protocols - REST is just the approach or service architecture,
> while specific protocols are defined "ad hoc".
>
> A REST response might be based on HTTP alone (just a 201 or 500 response
> with no body), or send some data, which can be encoded in any way, and
> the "Content-type" header is used to define how. I know JSON and XML are
> popular data encapsulation methods, but since REST is not really a
> protocol like SOAP is, it does not specify which data encapsulation
> method should be used in REST services.
>
> Shahar.
>
> On Tue, 2007-05-29 at 18:26 +0300, Shahar Evorn wrote:
>  
>> I am not an expert on the subject, but I think the key difference
>> between REST and RPC based web services is more philosophical:
>>
>> RPC (Remote Procedure Call), as the name suggests, is procedure focused,
>> as SOAP and XMLRPC are. In practice, an RPC web serivce will usually
>> present numerous procedures (or methods) than can be executed on a
>> single resource (URL).
>>
>> REST (Representational State Transfer) on the other hand focuses on
>> resources ("nouns") and allows several methods (usually CRUD like
>> methods) to be executed on each resource. In the web services world,
>> that would usually mean you have numerous resources (URLs, "nouns"), and
>> you can execute a limited number of "methods" ("verbs") on each one
>> (HTTP GET, PUT, POST, DELETE == CRUD).
>>
>> A service which, for example, allows a user to retrieve, add, update and
>> delete articles in a blog, would be designed this way:
>>
>> RPC:
>> POST http://example.com/blog/soap/articleManager.php
>> (Call the same URL, while the SOAP request body defines the method and
>> parameters)
>>
>> REST:
>> GET    http://example.com/blog/rest/articles - get list of all articles
>> PUT    http://example.com/blog/rest/articles - create a new article
>> GET    http://example.com/blog/rest/articles/1 - retrieve article #1
>> POST   http://example.com/blog/rest/articles/1 - update article #1
>> DELETE http://example.com/blog/rest/articles/1 - delete article #1
>>
>> and so on. One might simply say that REST takes a different approach to
>> mapping URLs to objects / methods / parameters, but I guess that under
>> some circumstances there is more than that.
>>
>> There is something very appealing for HTTP freaks in REST I think
>> because of the proper use of request methods (GET is never state
>> changing while POST is, etc.).
>>
>> As I mentioned briefly earlier, I think it would be quite easy to
>> implement a Rest_Server component (even if we name it differently) as
>> some kind of subclass of some Zend_Controller modules - it looks like
>> the REST URL to object / method / parameter mapping can be done quite
>> easily with some smart Zend_Controller routing / dispatching rules or
>> plug-ins.
>>
>> Shahar.
>>
>>
>> On Tue, 2007-05-29 at 15:55 +0200, till wrote:
>>    
>>> Shahar,
>>>
>>> On 5/28/07, Shahar Evorn <shahar.e@...> wrote:
>>>      
>>>> I did some more reading on this and it really does seem that our
>>>> Rest_Server is more RPC oriented (eg. method focused and not resource
>>>> focused) web service implementation.
>>>>        
>>> I had this discussion with a friend a while ago. I am not sure where
>>> we left of - so can you provide an example where REST is less RPC and
>>> more like ... whatever it is supposed to be? ;-))
>>>
>>>
>>> Cheers,
>>> Till
>>>      

 « Return to Thread: Our REST implementation