Skip to content

Instantly share code, notes, and snippets.

@plq
Created October 16, 2013 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save plq/7014099 to your computer and use it in GitHub Desktop.
Save plq/7014099 to your computer and use it in GitHub Desktop.
Spyne NullServer example.
'''
This is a simple HelloWorld example showing how the NullServer works. The
NullServer is meant to be used mainly for testing.
'''
import logging
logging.basicConfig(level=logging.INFO)
from pprint import pprint
from spyne.application import Application
from spyne.protocol.soap import Soap11
from spyne.server.null import NullServer
from spyne.decorator import rpc
from spyne.service import ServiceBase
from spyne.model.complex import Iterable
from spyne.model.primitive import Integer
from spyne.model.primitive import Unicode
class HelloWorldService(ServiceBase):
@rpc(Unicode, Integer, _returns=Iterable(Unicode))
def say_hello(ctx, name, times):
for i in range(times):
yield u'Hello, %s' % name
if __name__=='__main__':
application = Application([HelloWorldService], 'spyne.examples.hello.soap',
in_protocol=Soap11(validator='lxml'),
out_protocol=Soap11(pretty_print=True),
)
# disables context markers. set logging level to logging.INFO to enable
# them.
logging.getLogger('spyne.server.null').setLevel(logging.CRITICAL)
print "With serialization"
print "=================="
print
null = NullServer(application, ostr=True)
ret_stream = null.service.say_hello('Dave', 5)
ret_string = ''.join(ret_stream)
print ret_string
print
print "Without serialization"
print "====================="
print
null = NullServer(application, ostr=False)
ret = null.service.say_hello('Dave', 5)
# because the return value is a generator, we need to iterate over it to
# see the actual return values.
pprint(list(ret))
@franklingu
Copy link

How do I test a fairly complex xml message like this (I do not have custom type for this because this xml message may contain attributes):

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Body><ns0:EnterIdea xmlns:ns0="dtl.til-server.http"><ns0:ideaEntry>
<ns0:IdeaEntryCollection>
    <ns0:Idea>
        <ns0:AuthorRdc2Name>test</ns0:AuthorRdc2Name>
        <ns0:AuthorEmail>test</ns0:AuthorEmail>
        <ns0:AuthorParticipantRdc2Name>test</ns0:AuthorParticipantRdc2Name>
        <ns0:IdeaRef>test</ns0:IdeaRef>
        <ns0:Rdc2IdeaRef>test</ns0:Rdc2IdeaRef>
        <ns0:Rdc2BasketRef>test</ns0:Rdc2BasketRef>
        <ns0:Rdc2IdeaId>1234</ns0:Rdc2IdeaId>
        <ns0:IdeaType>Trade</ns0:IdeaType>
        <ns0:AuthorName>test</ns0:AuthorName>
        <ns0:AuthorParticipantName>test</ns0:AuthorParticipantName>
        <ns0:Isin>test</ns0:Isin>
        <ns0:ExternalInstrumentId>test</ns0:ExternalInstrumentId>
        <ns0:Symbol>test</ns0:Symbol>
        <ns0:Ric>test</ns0:Ric>
        <ns0:Mic>test</ns0:Mic>
        <ns0:BloombergTicker>test</ns0:BloombergTicker>
        <ns0:BloombergId>test</ns0:BloombergId>
        <ns0:Direction>Long</ns0:Direction>
        <ns0:Conviction>High</ns0:Conviction>
        <ns0:StopLossPercent>1.0</ns0:StopLossPercent>
        <ns0:Investment>1.0</ns0:Investment>
        <ns0:InvestmentCurrency>USD</ns0:InvestmentCurrency>
        <ns0:TargetPrice>1.0</ns0:TargetPrice>
        <ns0:TargetPriceCurrency>USD</ns0:TargetPriceCurrency>
        <ns0:StartDate>2002-05-30T09:00:00</ns0:StartDate>
        <ns0:IdeaState>Open</ns0:IdeaState>
        <ns0:LastUpdate>2002-05-30T09:00:00</ns0:LastUpdate>
    </ns0:Idea>
</ns0:IdeaEntryCollection>
</ns0:ideaEntry></ns0:EnterIdea></soap-env:Body></soap-env:Envelope>

@anthonyto
Copy link

This is very helpful, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment