Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

support for type instances to eliminate need in explicit element decl…

…aration for method return
  • Loading branch information...
commit 1c991de21e53ada99de77e9229e516c3703bfb4f 1 parent 067c8fa
@baverman authored
View
16 dropthesoap/schema/model.py
@@ -8,6 +8,7 @@ class TagDescriptor(object):
def __get__(_self, _instance, cls):
return getattr(cls, '__tag__', None) or cls.__name__
+
class TypeNameDescriptor(object):
def __get__(_self, instance, cls):
return instance.attributes['name'] if instance else cls.tag
@@ -58,11 +59,15 @@ def __get__(self, _instance, cls):
result = self.cache[cls] = create_instance_class(cls)
return result
+ instance_class = InstanceClassDescriptor()
+
@classmethod
def get_name(cls):
return cls.__name__
- instance_class = InstanceClassDescriptor()
+ @classmethod
+ def instance(cls, *args, **kwargs):
+ return TypeInstance(cls, *args, **kwargs)
class Namespace(object):
@@ -89,6 +94,15 @@ def tag(self):
return self._element.name
+class TypeInstance(object):
+ def __init__(self, type, *args, **kwargs):
+ self.inferior_instance = type.instance_class(None, *args, **kwargs)
+
+ def create(self, element):
+ self.inferior_instance._element = element
+ return self.inferior_instance
+
+
class ElementInstance(Instance):
def __init__(self, tree):
self.value = tree
View
6 dropthesoap/service.py
@@ -1,5 +1,5 @@
from .schema import xs, wsdl, soap
-from .schema.model import Namespace, get_root, etree, Instance
+from .schema.model import Namespace, get_root, etree, Instance, TypeInstance
class customize(object):
def __init__(self, type, minOccurs=None, maxOccurs=None, default=None, nillable=None):
@@ -129,7 +129,9 @@ def dispatch(self, request):
result = func(*args)
- if not isinstance(result, Instance):
+ if isinstance(result, TypeInstance):
+ result = result.create(response)
+ elif not isinstance(result, Instance):
result = response.instance(result)
return result
View
16 tests/test_schema.py
@@ -111,3 +111,19 @@ def test_simple_content():
obj = schema.fromstring(tostring(request))
assert obj.value == 'message'
assert obj.lang == 'en'
+
+def test_type_instances():
+ Request = xs.element('Request')(xs.cts(
+ xs.element('x', xs.string)))
+
+ schema = xs.schema(Namespace('http://boo', 'boo'))(
+ Request,
+ )
+
+ request = Request.type.instance(x='message')
+ real_request = request.create(Request)
+ assert validate(schema, real_request)
+
+ obj = schema.fromstring(tostring(real_request))
+ assert obj.x == 'message'
+
View
27 tests/test_service.py
@@ -12,9 +12,34 @@ def test_simple_service():
def add(x=xs.int, y=xs.int):
return x + y
- open('/tmp/wow.xml', 'w').write(service.get_wsdl('http://localhost/'))
+ #open('/tmp/wow.xml', 'w').write(service.get_wsdl('http://localhost/'))
cl = Client('some address', transport=DirectSudsTransport(service), cache=None)
result = cl.service.add(1, 10)
assert result == 11
+
+def test_complex_return_type():
+ service = Service('Boo', 'http://boo')
+
+ ResponseType = xs.complexType(name='ResponseType')(
+ xs.sequence()(
+ xs.element('foo', xs.string),
+ xs.element('bar', xs.string)))
+
+ service.schema(
+ ResponseType
+ )
+
+ @service.expose(returns=ResponseType)
+ def add(x=xs.int, y=xs.int):
+ return ResponseType.instance(foo=x+y, bar=x-y)
+
+ open('/tmp/wow.xml', 'w').write(service.get_wsdl('http://localhost/'))
+
+ cl = Client('some address', transport=DirectSudsTransport(service), cache=None)
+
+ result = cl.service.add(1, 10)
+ print result
+ assert result.foo == '11'
+ assert result.bar == '-9'
Please sign in to comment.
Something went wrong with that request. Please try again.