build an API service in Django
- by Peter
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.