Created
July 26, 2011 07:38
-
-
Save Apkawa/1106206 to your computer and use it in GitHub Desktop.
Using soaplib as django views
This file contains hidden or 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
''' | |
Simple usage as cherrypy daemon as example | |
''' | |
import soaplib | |
from soaplib.core.server import wsgi | |
from cherrypy.wsgiserver import CherryPyWSGIServer | |
HOST = "0.0.0.0" | |
PORT = 45124 | |
if __name__ == '__main__': | |
try: | |
soap_application = soaplib.core.Application([MySoapService], 'tns') | |
wsgi_application = wsgi.Application(soap_application) | |
server = CherryPyWSGIServer((HOST, PORT), | |
wsgi_application, | |
numthreads=10, | |
server_name=None, | |
max=-1, | |
request_queue_size=10, | |
timeout=100) | |
server.start() | |
except KeyboardInterrupt: | |
server.stop() | |
print "Shutdown." |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
''' | |
documentation in http://soaplib.github.com/soaplib/2_0/ | |
''' | |
import base64 | |
import soaplib | |
from soaplib.core import Application | |
from soaplib.core.model import primitive as soap_types | |
from soaplib.core.service import DefinitionBase | |
from soaplib.core.service import rpc as soapmethod | |
from soaplib.core.server import wsgi | |
from soaplib.core.model.clazz import ClassModel | |
from soaplib.core.model.clazz import Array | |
from django.http import HttpResponse | |
# the class with actual web methods | |
# the class which acts as a wrapper between soaplib WSGI functionality and Django | |
class DjangoSoapApp(wsgi.Application): | |
def __call__(self, request): | |
# wrap the soaplib response into a Django response object | |
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 | |
response = super(DjangoSoapApp, self).__call__(request.META, start_response) | |
django_response.content = '\n'.join(response) | |
return django_response | |
class SoapView(DefinitionBase): | |
@classmethod | |
def as_view(cls): | |
soap_application = Application([cls], __name__) | |
return DjangoSoapApp(soap_application) | |
# the view to use in urls.py | |
#my_soap_service = DjangoSoapApp([MySOAPService], __name__) |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
from django.conf.urls.defaults import patterns, include, url | |
import views | |
urlpatterns = patterns('', | |
url(r'^soap/', views.my_soap_service), | |
) |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
from soap import SoapView | |
from soap import soapmethod | |
from soap import soap_types, Array | |
from soap import ClassModel | |
class Item(ClassModel): | |
__namespace__ = 'warehouse' | |
pk = soap_types.Integer | |
name = soap_types.String | |
class MySoapService(SoapView): | |
__tns__ = '[url]http://localhost/my-soap-service/[/url]' | |
@soapmethod(soap_types.String, _returns=soap_types.String) | |
def say_hello(self, name): | |
results = 'Hello, %s' %name | |
return results | |
@soapmethod(_returns=Array(Warehouse)) | |
def get_items(self): | |
return [Item(pk=1, name='Item 1'), Item(pk=2, name='Item 2')] | |
my_soap_service = MySoapService.as_view() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks for upload!
I hope, that code works great, I'll check in my project in the evening. greetings :)