On 22.02.2010, at 09:30, [email protected] wrote:

Hi all

If I wanted to create a REST API using Agavi, is it simply that I need
to create a single action like ApiAction and within it define
executeRead, executeWrite, executeCreate and executeRemove methods?

REST represents resources, not operations. ApiAction is exactly *opposite* to what REST is all about. In SOAP, you do RPC calls, like getProduct(). In REST, you perform an operation (indicated by an HTTP verb) on a resource (represented by a URL).

You'd have a ProductsAction from which you GET to read, and to which you POST to create a new element, which is then read by a ProductAction, to which you can also PUT to replace a file:

GET /products - list
GET /products?num=100&page=2 - this is how you use query strings properly, /products/100/2 or so would *not* be RESTful as it doesn't represent a resource POST /products - create new product (returns 201 Created, and a Location header with the URL of the created entity)
GET /products/1234 - fetch
PUT /products/1234 - update
DELETE /products/1234 - delete

So you'd have ProductsAction::executeRead(), ProductsAction::executeCreate(), ProductAction::executeRead(), ProductAction::executeWrite(), ProductAction::executeRemove().

Note that Agavi maps POST to "write" by default, not to "create" as it would be useful for REST APIs. This is because HTTP forms overload the "POST" verb (only HTML5 can submit with action "PUT" or "DELETE"), and "write" is simply a more generic term than "create".

Also read:
http://stackoverflow.com/questions/1314862/
http://tomayko.com/writings/rest-to-my-wife

In real-world apps, where the actions etc alredy exist, you'd usually keep your Products.CreateAction, Products.Product.DeleteAction and so forth and simply use the routing to map URLs to these nicely (you can use the "constraint" attribute to limit an operation to a request method, and "method" to change the request method, so you can use that to swap "write" and "create").

Also, if I were to POST/PUT/DELETE XML documents to the REST API, is
there any way for me to use getParameter() within the corresponding
executeXXX method to retrieve the XML node values?

No.

Or should I just be using something like SimpleXML for this purpose?

I recommend you use JSON rather than XML, it's easier to deal with. You could easily subclass AgaviWebRequest so it decodes the incoming JSON into request parameters; then you'd probably not even need any changes to your Actions if they're *really* well-done already.

- David

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________
users mailing list
[email protected]
http://lists.agavi.org/mailman/listinfo/users

Reply via email to