def test_rpc(self): data = {"a":"b", "c": "d"} class KeyValuePair(ComplexModel): key = Unicode value = Unicode class SomeService(Service): @rpc(String(max_occurs='unbounded'), _returns=Array(KeyValuePair), _in_variable_names={ 'keys': 'key' } ) def get_values(ctx, keys): for k in keys: yield KeyValuePair(key=k, value=data[k]) application = Application([SomeService], in_protocol=MessagePackRpc(), out_protocol=MessagePackRpc(ignore_wrappers=False), name='Service', tns='tns') server = WsgiApplication(application) input_string = msgpack.packb([0, 0, "get_values", [["a", "c"]]]) input_stream = BytesIO(input_string) ret = server({ 'CONTENT_LENGTH': str(len(input_string)), 'CONTENT_TYPE': 'application/x-msgpack', 'HTTP_CONNECTION': 'close', 'HTTP_CONTENT_LENGTH': str(len(input_string)), 'HTTP_CONTENT_TYPE': 'application/x-msgpack', 'PATH_INFO': '/', 'QUERY_STRING': '', 'SERVER_NAME': 'localhost', 'SERVER_PORT': '7000', 'REQUEST_METHOD': 'POST', 'wsgi.url_scheme': 'http', 'wsgi.input': input_stream, }, start_response) ret = b''.join(ret) print(repr(ret)) ret = msgpack.unpackb(ret) print(repr(ret)) s = [1, 0, None, {b'get_valuesResponse': { b'get_valuesResult': [ {b"KeyValuePair": {b'key': b'a', b'value': b'b'}}, {b"KeyValuePair": {b'key': b'c', b'value': b'd'}}, ] }} ] print(s) assert ret == s
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # from spyne.application import Application from spyne.protocol.msgpack import MessagePackRpc from spyne.service import ServiceBase from spyne.decorator import rpc from spyne.model.primitive import Unicode class RadianteRPC(ServiceBase): @rpc(_returns=Unicode) def whoami(ctx): return "Hello I am Seldon!" app = Application([RadianteRPC], tns="radiante.rpc", in_protocol=MessagePackRpc(validator="soft"), out_protocol=MessagePackRpc()) import logging logging.basicConfig(level=logging.DEBUG)
# import logging logging.basicConfig(level=logging.DEBUG) logging.getLogger('spyne.protocol.msgpack').setLevel(logging.DEBUG) logger = logging.getLogger('spyne.test.interop.server.msgpackrpc_http_basic') from spyne.server.wsgi import WsgiApplication from spyne.test.interop.server._service import services from spyne.application import Application from spyne.protocol.msgpack import MessagePackRpc msgpackrpc_application = Application( services, 'spyne.test.interop.server', in_protocol=MessagePackRpc(validator='soft'), out_protocol=MessagePackRpc()) host = '127.0.0.1' port = 9754 def main(): try: from wsgiref.simple_server import make_server from wsgiref.validate import validator wsgi_application = WsgiApplication(msgpackrpc_application) server = make_server(host, port, validator(wsgi_application)) logger.info('Starting interop server at %s:%s.' % (host, port))
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()
jsonil = Application([HelloWorldService], tns=tns, in_protocol=HttpRpc(), out_protocol=JsonDocument(ignore_wrappers=True, complex_as=list)) msgpack_doc = Application([HelloWorldService], tns=tns, in_protocol=HttpRpc(), out_protocol=MessagePackDocument()) msgpack_rpc = Application([HelloWorldService], tns=tns, in_protocol=HttpRpc(), out_protocol=MessagePackRpc()) yaml = Application([HelloWorldService], tns=tns, in_protocol=HttpRpc(), out_protocol=YamlDocument()) root = WsgiMounter({ 'rest': rest, 'xml': xml, 'soap': soap, 'html': html, 'png': png, 'svg': svg, 'json': json, 'jsoni': jsoni,