Exemple #1
0
    def test_simple(self):
        class SomeService(ServiceBase):
            @srpc(String, _returns=String)
            def some_call(s):
                return s

        app = Application([SomeService], 'tns',
                                            in_protocol=HttpRpc(hier_delim='_'),
                                            out_protocol=HtmlMicroFormat())
        server = WsgiApplication(app)

        initial_ctx = WsgiMethodContext(server, {
            'QUERY_STRING': 's=s',
            'PATH_INFO': '/some_call',
            'REQUEST_METHOD': 'GET',
            'SERVER_NAME': 'localhost',
        }, 'some-content-type')

        ctx, = server.generate_contexts(initial_ctx)
        assert ctx.in_error is None

        server.get_in_object(ctx)
        server.get_out_object(ctx)
        server.get_out_string(ctx)

        assert ''.join(ctx.out_string) == '<div class="some_callResponse"><div class="some_callResult">s</div></div>'
Exemple #2
0
    def test_multiple_return(self):
        class SomeNotSoComplexModel(ComplexModel):
            s = String

        class SomeService(ServiceBase):
            @srpc(_returns=[Integer, String])
            def some_call():
                return 1, 's'

        app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlMicroFormat())
        server = WsgiApplication(app)

        initial_ctx = WsgiMethodContext(server, {
            'QUERY_STRING': '',
            'PATH_INFO': '/some_call',
            'REQUEST_METHOD': 'GET',
            'SERVER_NAME': 'localhost',
        }, 'some-content-type')

        ctx, = server.generate_contexts(initial_ctx)
        server.get_in_object(ctx)
        server.get_out_object(ctx)
        server.get_out_string(ctx)

        assert ''.join(ctx.out_string) == '<div class="some_callResponse"><div class="some_callResult0">1</div><div class="some_callResult1">s</div></div>'
    def test_multiple(self):
        class SomeService(ServiceBase):
            @srpc(String(max_occurs='unbounded'), _returns=String)
            def some_call(s):
                print(s)
                return '\n'.join(s)

        app = Application([SomeService], 'tns',
                                            in_protocol=HttpRpc(hier_delim='_'),
                                            out_protocol=HtmlMicroFormat())
        server = WsgiApplication(app)

        initial_ctx = WsgiMethodContext(server, {
            'QUERY_STRING': 's=1&s=2',
            'PATH_INFO': '/some_call',
            'REQUEST_METHOD': 'GET',
            'SERVER_NAME': 'localhost',
        }, 'some-content-type')

        ctx, = server.generate_contexts(initial_ctx)
        server.get_in_object(ctx)
        server.get_out_object(ctx)
        server.get_out_string(ctx)

        assert b''.join(ctx.out_string) == (b'<div class="some_callResponse">'
                               b'<div class="some_callResult">1\n2</div></div>')

        ctx, = server.generate_contexts(initial_ctx)
        server.get_in_object(ctx)
        server.get_out_object(ctx)
        server.get_out_string(ctx)

        assert b''.join(ctx.out_string) == b'<div class="some_callResponse">' \
                               b'<div class="some_callResult">1\n2</div></div>'
Exemple #4
0
    def test_complex_array(self):
        class CM(ComplexModel):
            i = Integer
            s = String

        class CCM(ComplexModel):
            c = CM
            i = Integer
            s = String

        class SomeService(ServiceBase):
            @srpc(CCM, _returns=Array(CCM))
            def some_call(ccm):
                return [CCM(c=ccm.c, i=ccm.i, s=ccm.s)] * 2

        app = Application([SomeService],
                          'tns',
                          in_protocol=HttpRpc(hier_delim='_'),
                          out_protocol=HtmlMicroFormat())
        server = WsgiApplication(app)

        out_string = call_wsgi_app_kwargs(server,
                                          ccm_c_s='abc',
                                          ccm_c_i=123,
                                          ccm_i=456,
                                          ccm_s='def')

        #
        # Here's what this is supposed to return:
        #
        # <div class="some_callResponse"><div class="some_callResult">
        #     <div class="CCM">
        #         <div class="i">456</div>
        #         <div class="c">
        #             <div class="i">123</div>
        #             <div class="s">abc</div>
        #         </div>
        #         <div class="s">def</div>
        #     </div>
        #     <div class="CCM">
        #         <div class="i">456</div>
        #         <div class="c">
        #             <div class="i">123</div>
        #             <div class="s">abc</div>
        #         </div>
        #         <div class="s">def</div>
        #     </div>
        # </div></div>
        #

        print(out_string)
        elt = html.fromstring(''.join(out_string))
        show(elt, "TestHtmlMicroFormat.test_complex_array")

        resp = elt.find_class('some_callResponse')
        assert len(resp) == 1
        res = resp[0].find_class('some_callResult')
        assert len(res) == 1

        assert len(res[0].find_class("CCM")) == 2
Exemple #5
0
    def test_before_first_root(self):
        class CM(ComplexModel):
            i = Integer
            s = String

        class CCM(ComplexModel):
            c = CM
            i = Integer
            s = String

        class SomeService(Service):
            @srpc(CCM, _returns=Array(CCM))
            def some_call(ccm):
                return [CCM(c=ccm.c,i=ccm.i, s=ccm.s)] * 2

        cb_called = [False]
        def _cb(ctx, cls, inst, parent, name, **kwargs):
            assert not cb_called[0]
            cb_called[0] = True

        app = Application([SomeService], 'tns',
                                     in_protocol=HttpRpc(hier_delim='_'),
                                     out_protocol=HtmlMicroFormat(
                                           doctype=None, before_first_root=_cb))
        server = WsgiApplication(app)

        call_wsgi_app_kwargs(server,
                             ccm_c_s='abc', ccm_c_i=123, ccm_i=456, ccm_s='def')

        assert cb_called[0]
def start_a2bs(config):
    subconfig = config.services.getwrite(
        'web',
        HttpListener(
            host='0.0.0.0',
            port=9271,
            disabled=False,
            _subapps=[
                StaticFileServer(url='assets',
                                 path=abspath('assets'),
                                 list_contents=False)
            ],
        ))

    services = [
        TestServices,
        CardReaderServices,
        CardWriterServices,
        SipBuddyReaderServices,
        SipBuddyWriterServices,
        ExtReaderServices,
        ExtWriterServices,
    ]

    subconfig.subapps['json'] = \
        Application(services,
            tns='a2bs.web', name='A2BillingJson',
            in_protocol=HttpRpc(validator='soft'),
            out_protocol=JsonDocument(),
            config=config,
        )

    subconfig.subapps['xml'] = \
        Application(services,
            tns='a2bs.web', name='A2BillingXml',
            in_protocol=HttpRpc(validator='soft'),
            out_protocol=XmlDocument(),
            config=config,
        )

    subconfig.subapps[''] = \
        Application(services,
            tns='a2bs.web', name='A2BillingHtml',
            in_protocol=HttpRpc(validator='soft'),
            out_protocol=HtmlMicroFormat(),
            config=config,
        )

    site = subconfig.gen_site()

    logger.info("listening for a2billing http endpoint %s:%d", subconfig.host,
                subconfig.port)
    return reactor.listenTCP(subconfig.port, site,
                             interface=subconfig.host), None
Exemple #7
0
    def test_complex(self):
        class CM(ComplexModel):
            i = Integer
            s = String

        class CCM(ComplexModel):
            c = CM
            i = Integer
            s = String

        class SomeService(ServiceBase):
            @srpc(CCM, _returns=CCM)
            def some_call(ccm):
                return CCM(c=ccm.c,i=ccm.i, s=ccm.s)

        app = Application([SomeService], 'tns',
                                            in_protocol=HttpRpc(hier_delim='_'),
                                            out_protocol=HtmlMicroFormat())
        server = WsgiApplication(app)

        initial_ctx = WsgiMethodContext(server, {
            'QUERY_STRING': 'ccm_c_s=abc&ccm_c_i=123&ccm_i=456&ccm_s=def',
            'PATH_INFO': '/some_call',
            'REQUEST_METHOD': 'GET',
            'SERVER_NAME': 'localhost',
        }, 'some-content-type')

        ctx, = server.generate_contexts(initial_ctx)
        server.get_in_object(ctx)
        server.get_out_object(ctx)
        server.get_out_string(ctx)

        #
        # Here's what this is supposed to return:
        #
        # <div class="some_callResponse">
        #   <div class="some_callResult">
        #     <div class="i">456</div>
        #     <div class="c">
        #       <div class="i">123</div>
        #       <div class="s">abc</div>
        #     </div>
        #     <div class="s">def</div>
        #   </div>
        # </div>
        #

        elt = html.fromstring(''.join(ctx.out_string))
        print(html.tostring(elt, pretty_print=True))

        resp = elt.find_class('some_callResponse')
        assert len(resp) == 1
        res = resp[0].find_class('some_callResult')
        assert len(res) == 1

        i = res[0].findall('div[@class="i"]')
        assert len(i) == 1
        assert i[0].text == '456'

        c = res[0].findall('div[@class="c"]')
        assert len(c) == 1

        c_i = c[0].findall('div[@class="i"]')
        assert len(c_i) == 1
        assert c_i[0].text == '123'

        c_s = c[0].findall('div[@class="s"]')
        assert len(c_s) == 1
        assert c_s[0].text == 'abc'

        s = res[0].findall('div[@class="s"]')
        assert len(s) == 1
        assert s[0].text == 'def'
Exemple #8
0
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()
Exemple #9
0
                       out_protocol=HttpRpc())

    xml = Application([HelloWorldService],
                      tns=tns,
                      in_protocol=HttpRpc(),
                      out_protocol=XmlDocument())

    soap = Application([HelloWorldService],
                       tns=tns,
                       in_protocol=HttpRpc(),
                       out_protocol=Soap11())

    html = Application([HelloWorldService],
                       tns=tns,
                       in_protocol=HttpRpc(),
                       out_protocol=HtmlMicroFormat())

    png = Application([HelloWorldService],
                      tns=tns,
                      in_protocol=HttpRpc(),
                      out_protocol=PngClock())

    svg = Application([HelloWorldService],
                      tns=tns,
                      in_protocol=HttpRpc(),
                      out_protocol=SvgClock())

    json = Application([HelloWorldService],
                       tns=tns,
                       in_protocol=HttpRpc(),
                       out_protocol=JsonDocument())
Exemple #10
0
    def test_row_subprot(self):
        from lxml.html.builder import E
        from spyne.protocol.html import HtmlBase
        from spyne.util.six.moves.urllib.parse import urlencode
        from spyne.protocol.html import HtmlMicroFormat

        class SearchProtocol(HtmlBase):
            def to_parent(self, ctx, cls, inst, parent, name, **kwargs):
                s = self.to_unicode(cls._type_info['query'], inst.query)
                q = urlencode({"q": s})

                parent.write(
                    E.a("Search %s" % inst.query,
                        href="{}?{}".format(inst.uri, q)))

            def column_table_gen_header(self, ctx, cls, parent, name):
                parent.write(
                    E.thead(E.th("Search", **{'class': 'search-link'})))

            def column_table_before_row(self, ctx, cls, inst, parent, name,
                                        **_):
                ctxstack = getattr(ctx.protocol[self],
                                   'array_subprot_ctxstack', [])

                tr_ctx = parent.element('tr')
                tr_ctx.__enter__()
                ctxstack.append(tr_ctx)

                td_ctx = parent.element('td', **{'class': "search-link"})
                td_ctx.__enter__()
                ctxstack.append(td_ctx)

                ctx.protocol[self].array_subprot_ctxstack = ctxstack

            def column_table_after_row(self, ctx, cls, inst, parent, name,
                                       **kwargs):
                ctxstack = ctx.protocol[self].array_subprot_ctxstack

                for elt_ctx in reversed(ctxstack):
                    elt_ctx.__exit__(None, None, None)

                del ctxstack[:]

        class Search(ComplexModel):
            query = Unicode
            uri = Unicode

        SearchTable = Array(
            Search.customize(prot=SearchProtocol()),
            prot=HtmlColumnTable(field_type_name_attr=None),
        )

        class SomeService(ServiceBase):
            @srpc(_returns=SearchTable)
            def some_call():
                return [
                    Search(query='Arskom',
                           uri='https://www.google.com/search'),
                    Search(query='Spyne', uri='https://www.bing.com/search'),
                ]

        app = Application([SomeService],
                          'tns',
                          in_protocol=HttpRpc(),
                          out_protocol=HtmlMicroFormat())
        server = WsgiApplication(app)

        out_string = call_wsgi_app_kwargs(server)

        elt = html.fromstring(out_string)
        print(html.tostring(elt, pretty_print=True))

        assert elt.xpath('//td[@class="search-link"]/a/text()') == \
                                               ['Search Arskom', 'Search Spyne']

        assert elt.xpath('//td[@class="search-link"]/a/@href') == [
            'https://www.google.com/search?q=Arskom',
            'https://www.bing.com/search?q=Spyne',
        ]

        assert elt.xpath('//th[@class="search-link"]/text()') == ["Search"]