def test_roundtrip_deferred(self): from twisted.internet import reactor from twisted.internet.task import deferLater v = "yaaay!" p_ctx = [] class SomeService(ServiceBase): @rpc(Unicode, _returns=Unicode) def yay(ctx, u): def _cb(): return u p_ctx.append(ctx) return deferLater(reactor, 0.1, _cb) app = Application([SomeService], 'tns', in_protocol=MessagePackDocument(), out_protocol=MessagePackDocument()) prot = self.gen_prot(app) request = msgpack.packb({'yay': [v]}) def _ccb(_): val = prot.transport.value() print repr(val) val = msgpack.unpackb(val) print repr(val) self.assertEquals(val, [0, msgpack.packb(v)]) prot.dataReceived(msgpack.packb([1, request])) return p_ctx[0].out_object[0].addCallback(_ccb)
def get_object_as_msgpack(o, cls=None, ignore_wrappers=False, complex_as=dict, encoding="utf8", polymorphic=False): if cls is None: cls = o.__class__ prot = MessagePackDocument(ignore_wrappers=ignore_wrappers, complex_as=complex_as, polymorphic=polymorphic) ctx = FakeContext(out_document=[prot._object_to_doc(cls, o)]) prot.create_out_string(ctx, encoding) return "".join(ctx.out_string)
def get_object_as_msgpack_doc(o, cls=None, ignore_wrappers=False, complex_as=dict, polymorphic=False): if cls is None: cls = o.__class__ prot = MessagePackDocument(ignore_wrappers=ignore_wrappers, complex_as=complex_as, polymorphic=polymorphic) return prot._object_to_doc(cls, o)
def get_object_as_msgpack(o, cls=None, ignore_wrappers=False, complex_as=dict, encoding='utf8', polymorphic=False): if cls is None: cls = o.__class__ prot = MessagePackDocument(ignore_wrappers=ignore_wrappers, complex_as=complex_as, polymorphic=polymorphic) ctx = FakeContext(out_document=[prot._object_to_doc(cls,o)]) prot.create_out_string(ctx, encoding) return b''.join(ctx.out_string)
def test_invalid_input(self): class SomeService(Service): @srpc() def yay(): pass app = Application([SomeService], 'tns', in_protocol=MessagePackDocument(), out_protocol=MessagePackDocument()) server = ServerBase(app) initial_ctx = MethodContext(server, MethodContext.SERVER) initial_ctx.in_string = [b'\xdf'] # Invalid input ctx, = server.generate_contexts(initial_ctx) assert ctx.in_error.faultcode == 'Client.MessagePackDecodeError'
def test_roundtrip(self): v = "yaaay!" class SomeService(ServiceBase): @rpc(Unicode, _returns=Unicode) def yay(ctx, u): return u app = Application([SomeService], 'tns', in_protocol=MessagePackDocument(), out_protocol=MessagePackDocument()) prot = self.gen_prot(app) request = msgpack.packb({'yay': [v]}) prot.dataReceived(msgpack.packb([1, request])) val = prot.transport.value() print repr(val) val = msgpack.unpackb(val) print repr(val) self.assertEquals(val, [0, msgpack.packb(v)])
def geometry_service(fcgi=True): if fcgi is False: def _on_method_return_object(ctx): ctx.transport.resp_headers['Access-Control-Allow-Origin'] = "*" ctx.transport.resp_headers['Cache-Control'] = "public,max-age=86400" # tbd GeometryService.event_manager.add_listener('method_return_object', _on_method_return_object) json = Application([GeometryService], tns='swhv.service.geometry.json', in_protocol=HttpRpc(validator='soft'), out_protocol=JsonDocument()) msgpack = Application([GeometryService], tns='swhv.service.geometry.msgpack', in_protocol=HttpRpc(validator='soft'), out_protocol=MessagePackDocument()) return WsgiMounter({'json': json, 'msgpack': msgpack})
def say_hello(ctx, name, times): for i in range(times): yield u'Hello, %s' % name if __name__ == '__main__': from twisted.internet import reactor from twisted.python import log # logging boilerplate logging.basicConfig(level=logging.DEBUG) logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG) observer = log.PythonLoggingObserver('twisted') log.startLoggingWithObserver(observer.emit, setStdout=False) # set up application application = Application([HelloWorldService], 'spyne.examples.hello.msgpack', in_protocol=MessagePackDocument(), out_protocol=MessagePackDocument()) # set up transport factory = TwistedMessagePackProtocolFactory(application) # set up listening endpoint reactor.listenTCP(PORT, factory, interface=HOST) # run the reactor reactor.run()
def main(): global protocols rest = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=HttpRpc()) xml = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=XmlDocument()) soap = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=Soap11()) html = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=HtmlMicroFormat()) png = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=PngClock()) svg = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=SvgClock()) json = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=JsonDocument()) jsoni = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=JsonDocument( ignore_wrappers=True)) jsonl = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=JsonDocument(complex_as=list)) jsonil = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=JsonDocument( ignore_wrappers=True, complex_as=list)) msgpack_doc = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=MessagePackDocument()) msgpack_rpc = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=MessagePackRpc()) yaml = Application([MultiProtService], tns=tns, in_protocol=HttpRpc(), out_protocol=YamlDocument()) dyn = Application([DynProtService], tns=tns, in_protocol=HttpRpc(validator='soft'), out_protocol=HttpRpc()) DynProtService.protocols = { 'json': Tsetprot(JsonDocument(dyn)), 'xml': Tsetprot(XmlDocument(dyn)), 'yaml': Tsetprot(YamlDocument(dyn)), 'soap': Tsetprot(Soap11(dyn)), 'html': Tsetprot(HtmlMicroFormat(dyn)), 'png': Tsetprot(PngClock(dyn)), 'svg': Tsetprot(SvgClock(dyn)), 'msgpack': Tsetprot(MessagePackDocument(dyn)), } root = WsgiMounter({ 'rest': rest, 'xml': xml, 'soap': soap, 'html': html, 'png': png, 'svg': svg, 'json': json, 'jsoni': jsoni, 'jsonl': jsonl, 'jsonil': jsonil, 'mpd': msgpack_doc, 'mprpc': msgpack_rpc, 'yaml': yaml, 'dyn': dyn, }) from wsgiref.simple_server import make_server server = make_server(host, port, root) logging.basicConfig(level=logging.DEBUG) logging.info("listening to http://%s:%d" % (host, port)) logging.info("navigate to e.g. http://%s:%d/json2/get_utc_time" % (host, port)) logging.info(" or: http://%s:%d/xml/get_utc_time" % (host, port)) return server.serve_forever()