python - How do you @rpc _returns polymorphic types in spyne? -


edit

example,

class a(complexmodel):     id = unicode  class b(complexmodel):     __extends__ =     name = unicode  @rpc(string, _returns=a) def hello(self, name):     ret = b()     b.id = '123'     b.name = name     return ret 

how handle behavior doesn't return object of a?


how write spyne decorators correctly return more 1 type? if, example, _returns set zobj returning xaccount (like in code) doesn't anything.

can write xaccount object extends zobj , valid return type?

@rpc(string, _returns=(zobj, xaccount)) def hello(self, name):     acc = xaccount(         user_name = name.upper(),         first_name = name,         last_name = 'last ' + name     )     return acc 

class examples....

class zobj(complexmodel):     id = unicode(pattern='[a-za-z0-9]{32}|\d+')  class account(declarativebase):     __tablename__ = 'account'      id = column(integer, primary_key=true)     user_name = column(string(256))     first_name = column(string(256))     last_name = column(string(256))   class xaccount(tablemodel):     __table__ = account.__table__ 

deleting previous answer apparently need polymorphism, not multiple return types.

so, there 2 ways of doing polymorphism in spyne: python way , spyne way.

let:

class a(complexmodel):     = integer  class b(a):     s = unicode  class c(a):     d = datetime 

the python way uses duck typing return values.

let's define generic class:

class generica(complexmodel):     = integer     s = unicode     d = datetime 

and use return value of our sample service:

class someservice(servicebase):     @rpc(unicode(values=['a', 'b', 'c']), _returns=generica)     def get_some_a(self, type_name):         # (...) 

this way, data, it's tagged generica object. if don't care this, can create class has types objects (assuming attributes same names have same type) , done it. easy, stable , works today.

if that's not enough needs, have polymorphism spyne way. that, first set return type base class:

class someservice(servicebase):     @rpc(unicode(values=['a', 'b', 'c']), _returns=a)     def get_some_a(self, type_name):         # (...) 

and tag output protocol polymorphic:

application = application([someservice], 'tns',     in_protocol=soap11(validator='lxml'),     out_protocol=soap11(polymorphic=true) ) 

this requires @ least spyne-2.12.

working example: https://github.com/arskom/spyne/blob/a1b3593f3754a9c8a6787c29ff50f591db89fd49/examples/xml/polymorphism.py


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -