def test_anyuri_uri_value(self): _link = "http://arskom.com.tr/" _text = "Arskom" class C(ComplexModel): c = AnyUri class SomeService(ServiceBase): @srpc(_returns=C) def some_call(): return C(c=AnyUri.Value(_link, text=_text)) app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlTable(field_name_attr='class', fields_as='rows')) 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="c"]')[0][0].tag == 'a' assert elt.xpath('//td[@class="c"]')[0][0].text == _text assert elt.xpath('//td[@class="c"]')[0][0].attrib['href'] == _link
def test_complex_array(self): class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [ccm] * 5 app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs( server, ccm_i='456', ccm_s='def', ccm_c_i='123', ccm_c_s='abc', ) elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) resp = elt.find_class('some_callResponse') assert len(resp) == 1 for i in range(len(elt)): row = elt[i] if i == 0: # check for field names in table header cell = row.findall('th[@class="i"]') assert len(cell) == 1 assert cell[0].text == 'i' cell = row.findall('th[@class="c_i"]') assert len(cell) == 1 assert cell[0].text == 'c_i' cell = row.findall('th[@class="c_s"]') assert len(cell) == 1 assert cell[0].text == 'c_s' cell = row.findall('th[@class="s"]') assert len(cell) == 1 assert cell[0].text == 's' else: # check for field values in table body cell = row.findall('td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '456' cell = row.findall('td[@class="c_i"]') assert len(cell) == 1 assert cell[0].text == '123' cell = row.findall('td[@class="c_s"]') assert len(cell) == 1 assert cell[0].text == 'abc' cell = row.findall('td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'def'
def test_complex(self): class SomeService(ServiceBase): @srpc(CCM, _returns=CCM) def some_call(ccm): return ccm app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlTable(field_name_attr='class', fields_as='rows')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs(server, ccm_c_s='abc', ccm_c_i='123', ccm_i='456', ccm_s='def') elt = html.fromstring(out_string) print(html.tostring(elt, pretty_print=True)) # Here's what this is supposed to return """ <table class="some_callResponse"> <tr> <th class="i">i</th> <td class="i">456</td> </tr> <tr> <th class="c_i">c_i</th> <td class="c_i">123</td> </tr> <tr> <th class="c_s">c_s</th> <td class="c_s">abc</td> </tr> <tr> <th class="s">s</th> <td class="s">def</td> </tr> </table> """ resp = elt.find_class('some_callResponse') assert len(resp) == 1 assert elt.xpath('//th[@class="i"]/text()')[0] == 'i' assert elt.xpath('//td[@class="i"]/text()')[0] == '456' assert elt.xpath('//th[@class="c_i"]/text()')[0] == 'c_i' assert elt.xpath('//td[@class="c_i"]/text()')[0] == '123' assert elt.xpath('//th[@class="c_s"]/text()')[0] == 'c_s' assert elt.xpath('//td[@class="c_s"]/text()')[0] == 'abc' assert elt.xpath('//th[@class="s"]/text()')[0] == 's' assert elt.xpath('//td[@class="s"]/text()')[0] == 'def'
def test_complex_array(self): class SomeService(ServiceBase): @srpc(CCM, _returns=Array(CCM)) def some_call(ccm): return [ccm] * 5 app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlTable(field_name_attr='class')) server = WsgiApplication(app) out_string = call_wsgi_app_kwargs( server, ccm_i='456', ccm_s='def', ccm_c_i='123', ccm_c_s='abc', ) from lxml import etree elt = etree.fromstring(out_string) print(etree.tostring(elt, pretty_print=True)) elt = html.fromstring(out_string) resp = elt.find_class('some_callResponse') assert len(resp) == 1 row, = elt[0] # thead cell = row.findall('th[@class="i"]') assert len(cell) == 1 assert cell[0].text == 'i' cell = row.findall('th[@class="s"]') assert len(cell) == 1 assert cell[0].text == 's' for row in elt[1]: # tbody cell = row.xpath('td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '456' cell = row.xpath('td[@class="c"]//td[@class="i"]') assert len(cell) == 1 assert cell[0].text == '123' cell = row.xpath('td[@class="c"]//td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'abc' cell = row.xpath('td[@class="s"]') assert len(cell) == 1 assert cell[0].text == 'def'
def test_string_array(self): class SomeService(ServiceBase): @srpc(String(max_occurs='unbounded'), _returns=Array(String)) def some_call(s): return s app = Application([SomeService], 'tns', in_protocol=HttpRpc(), out_protocol=HtmlTable(fields_as='rows')) server = WsgiApplication(app) out_string = call_wsgi_app(server, body_pairs=(('s', '1'), ('s', '2'))) assert out_string == '<table class="some_callResponse"><tr><td>1</td></tr><tr><td>2</td></tr></table>'
return Iterable.Push(_cb) @rpc(Unicode(default='World'), _returns=Iterable(Unicode)) def say_hello_forever(ctx, name): def _cb(push): push.append(u'Hello, %s' % name) return deferLater(reactor, 0.1, _cb, push) return Iterable.Push(_cb) if __name__ == '__main__': application = Application( [HelloWorldService], 'spyne.examples.twisted.resource_push', in_protocol=HttpRpc(), out_protocol=HtmlTable(), ) resource = TwistedWebResource(application) site = Site(resource) reactor.listenTCP(port, site, interface=host) logging.basicConfig(level=logging.DEBUG) logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG) logging.info("listening on: %s:%d" % (host, port)) logging.info('wsdl is at: http://%s:%d/?wsdl' % (host, port)) sys.exit(reactor.run())
def main(connection_string=DB_CONNECTION_STRING): # configure logging logging.basicConfig(level=logging.DEBUG) logging.getLogger('spyne.protocol.xml').setLevel(logging.DEBUG) #logging.getLogger('sqlalchemy.engine.base.Engine').setLevel(logging.DEBUG) from spynepi.const import FILES_PATH RootService.FILES_ROOT = os.path.abspath(FILES_PATH) index_app = MyApplication([RootService, IndexService], "http://usefulinc.com/ns/doap#", in_protocol=HttpRpc(), out_protocol=HtmlTable()) rdf_app = MyApplication([RdfService], "http://usefulinc.com/ns/doap#", in_protocol=HttpRpc(), out_protocol=XmlDocument()) html_app = MyApplication([HtmlService], "http://usefulinc.com/ns/doap#", in_protocol=HttpRpc(), out_protocol=HttpRpc()) db_handle = init_database(connection_string) class UserDefinedContext(object): def __init__(self): self.session = db_handle.Session() def close(self): self.session.close() # this is called after validation def _on_method_call(ctx): ctx.udc = UserDefinedContext() # this is called once all data is sent to the client. def _on_method_return_object(ctx): ctx.udc.session.commit() def _on_wsgi_close(ctx): if ctx.udc is not None: ctx.udc.close() for app in index_app, rdf_app, html_app: app.event_manager.add_listener('method_call', _on_method_call) app.event_manager.add_listener('method_return_object', _on_method_return_object) wsgi_index = WsgiApplication(index_app) wsgi_rdf = WsgiApplication(rdf_app) wsgi_html = WsgiApplication(html_app) for a in wsgi_index, wsgi_rdf, wsgi_html: a.event_manager.add_listener('wsgi_close', _on_wsgi_close) url_map = Map([ Rule("/", endpoint=wsgi_index), Rule("/<project_name>", endpoint=wsgi_html), Rule("/<project_name>/", endpoint=wsgi_html), Rule("/<project_name>/doap.rdf", endpoint=wsgi_rdf), Rule("/<project_name>/<version>", endpoint=wsgi_html), Rule("/<project_name>/<version>/", endpoint=wsgi_html), Rule("/<project_name>/<version>/doap.rdf", endpoint=wsgi_rdf), Rule("/files/<project_name>/<version>/<download>", endpoint=wsgi_html), ]) resource = WSGIResource(reactor, reactor, TWsgiApplication(url_map)) site = Site(resource) reactor.listenTCP(PORT, site) logging.info('listening on: %s:%d' % (HOST, PORT)) logging.info('wsdl is at: http://%s:%d/?wsdl' % (HOST, PORT)) sys.exit(reactor.run())