I'm trying to figure out the best way to add a web-service API to  
Django views. Let's say I have an application that offers a  
calculator. My normal URL scheme might be:

..../calc/add
.../calc/subtract
.../calc/multiply
.../calc/divide
.../calc/clear

The ordinary web-interface would return plain HTML through templates.  
But if I wanted to allow my calculator to be 'mashable,' I might want  
to also offer a JSON and an XML interface to the same functions. The  
JSON version would take the same params but return JSON results, so  
an Ajax-friendly or Flash UI could take advantage of it. The XML  
version would come handy so third-parties could tie their back-ends  
to the open calculator API.

Ideally, we'll want to avoid burdening the developer with yet another  
thing to remember and avoid per-method custom code. The way I see it,  
there are three parts to this problem:

1. Distinguish between regular vs. web-service requests: one way to  
do this is to install a middleware that looks for a flag on the URL,  
say ".../xml/calc/add" or ".../json/calc/add." Or perhaps "?reply=  
{ json | xml }" The middleware would strip the extras out of the URL  
and set a flag that can be checked inside the request.

2. Return results in the format requested: One way is to have a  
simple function call (or decorator?) check for the request format  
flag and turn a QuerySet or dictionary into the appropriate result  
form. Another way could be to have some sort of 'proxy' result  
renderer take over. (Any other way?)

3. Marking a method as 'callable:' you may not want every method in a  
view exposed as a web-service. Decorators make the most sense here.  
Or would a configuration file with an IDL description make more  
sense? (Blech, I hope not).

These are just ideas. I'd love to hear what others are doing and if  
there's some super-creative way to tackle the problem without  
burdening the developer too much. The main criterion, I think, should  
be to keep it as simple and DRY as possible. Any thoughts?

Ramin

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [EMAIL PROTECTED]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to