Question

I'm trying to use a combination of Spyne and Suds(although I'm not very particular on using Suds) to create a module which functions as a middleman between two SOAP entities.

There is a client, C, which connects to server S, calls a method M, which returns a ComplexType, D. The same data object needs to be sent by S to another server S1. Of course there is a method M1 which takes D type as parameter on server S1. The problem I'm facing is I can't just send D to S1, without making a conversion to a type which is recognized by Suds.

Is there a smart way to do this, without copying field by field the attributes of D from one "type" to the other?

Was it helpful?

Solution

You can indeed convert incoming objects to dicts and pass them to suds. But Spyne already offers both ways object<==>dict conversion facitilies.

To convert to dict you can use spyne.util.dictdoc.

e.g.

from spyne.model.complex import ComplexModel
from spyne.model.primitive import Integer

class B(ComplexModel):
    c = Integer

class A(ComplexModel):
    a = Integer
    b = B 

from spyne.util.dictdoc import get_object_as_dict
print get_object_as_dict(A(a=4, b=B(c=5)), A)
# prints {'a': 4, 'b': {'c': 5}}

get_object_as_dict(A(a=4, b=B(c=5)), A, ignore_wrappers=False)
# prints {'A': {'a': 4, 'b': {'B': {'c': 5}}}}

I hope it helps.

OTHER TIPS

You can convert the object of type D do a dict and pass the dict to the service method, Suds take dicts that respect the objects structure: https://fedorahosted.org/suds/wiki/Documentation#ComplexArgumentsUsingPythondict

To convert to dict you can use code from here: Recursively convert python object graph to dictionary.

Ie:

    D_inst = S.service.M()
    D_dict = to_dict(D_inst)
    S1.service.M1(D_dict)

Suds has big issues parsing complex objects.

E.g.

class C(ComplexModel):
    d = Integer

class B(ComplexModel):
    c = Array(C)

class A(ComplexModel):
    a = Integer
    b = B 

In this case using get_object_to_dict to convert to a dictionary functions properly, but suds is unable to call the SOAP method and fails with Type not found 'c'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top