build an API service in Django
Posted
by Peter
on Stack Overflow
See other posts from Stack Overflow
or by Peter
Published on 2010-04-11T18:12:15Z
Indexed on
2010/04/11
18:23 UTC
Read the original article
Hit count: 271
Hi all,
I want to build an API service using Django. A basic workflow goes like this:
First, an http request goes to http://mycompany.com/create.py?id=001&callback=http://callback.com
. It will create a folder on the server with name 001.
Second, if the folder does not exist, it will be created. You get response immediately in XML format. It will look like:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>
<statusCode>0</statusCode>
<message>Success</message>
</status>
<group id="001"/>
</response>
Finally, the server will do its job (i.e. creating the folder). After it is done, the server does a callback to the URL provided.
Currently, I use
return render_to_response('create.xml', {'statusCode': statusCode,
'statusMessage': statusMessage,
'groupId': groupId,
}, mimetype = 'text/xml')
to send the XML response back. I have an XML template which has statusCode
, statusMessage
, groupId
placeholders.
<?xml version="1.0" encoding="UTF-8"?>
<response>
<status>
<statusCode>{{ statusCode }}</statusCode>
<message>{{ statusMessage }}</message>
</status>
{% if not statusCode %}
<group id="{{ groupId }}"/>
{% endif %}
</response>
But in this way I have to put step 3 before step 2, because otherwise step 3 will not be executed if it is after return
statement.
Can somebody give me some suggestions how to do this? Thanks.
© Stack Overflow or respective owner