Created
April 22, 2011 01:04
-
-
Save rotaris/935809 to your computer and use it in GitHub Desktop.
Django-soaplib Hello World Web Service View
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# soaplib v2.0.0beta2 (from memory) | |
# Django v1.3 (stable) | |
# NOTE: CSRF middleware has been turned off! | |
# For urls.py, see: https://gist.github.com/935812 | |
import soaplib | |
from soaplib.core.service import rpc, DefinitionBase | |
from soaplib.core.model.primitive import String, Integer | |
from soaplib.core.model.clazz import Array | |
from django.views.decorators.csrf import csrf_exempt | |
class HelloWorldService(DefinitionBase): | |
@rpc(String,Integer,_returns=Array(String)) | |
def say_hello(self, name, times): | |
results = [] | |
for i in range(0, times): | |
results.append('Hellow, %s' %name) | |
return results | |
from soaplib.core.server.wsgi import Application | |
from django.http import HttpResponse | |
import StringIO | |
class DumbStringIO(StringIO.StringIO): | |
def read(self, n): | |
return self.getvalue() | |
class DjangoSoapApp(Application): | |
def __call__(self, request): | |
django_response = HttpResponse() | |
def start_response(status, headers): | |
status, reason = status.split(' ', 1) | |
django_response.status_code = int(status) | |
for header, value in headers: | |
django_response[header] = value | |
environ = request.META.copy() | |
environ['CONTENT_LENGTH'] = len(request.raw_post_data) | |
environ['wsgi.input'] = DumbStringIO(request.raw_post_data) | |
environ['wsgi.multithread'] = False | |
response = super(DjangoSoapApp, self).__call__(environ, start_response) | |
django_response.content = '\n'.join(response) | |
return django_response | |
soap_application = soaplib.core.Application([HelloWorldService], 'tns') | |
hello_world_service = DjangoSoapApp(soap_application) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment