Closed antonymott closed 1 year ago
yes, indeed, this example stems from a time when autobahn and crossbar still were one package. this no longer works, as both are separated now.
what would be needed is a crossbar web service for klein .. probably. we already have a lot of options in crossbar rgd webservices, and also mapping rest and wamp ... so I would first try to understand what you actually want
eg there is a powerful feature
https://github.com/crossbario/crossbar-examples/tree/master/webservices/wap
this allows you to provide REST endpoints, which will forward to WAMP RPC calls, with the result rendered through Jinja2 templates into HTML returned in the REST endpoint, eg
Thanks, I need simply to have Klein (or something else but Klein seems very lightweight and made with Twisted) listen for a POST (twilio app) at a url endpoint, and have python call an rRPC that's already registered and return it to the browser.
My modified server_web.py above works mostly, including having Klein listen and the @webapp.route() part fires. And within that def, this line doesn't cause an error:
res = yield self.call('com.test.square', x)
but trying to print or use the res results in:
builtins.TypeError: object of type 'generator' has no len()
Given that you already have your backend WAMP procedure you can achieve this using WAP in Crossbar.io with node configuration only
{
"path": "/square/<int:x>",
"method": "POST",
"call": "com.test.square",
"render": "square_result.html"
}
since the method is POST, the com.test.square
will - in addition to x
argument automatically parsed from URL - receive two more arguments:
data
: the binary POST datadata_type
: content type of POST'ed dataI'll check that out, thanks!
Putting the routes in config.json worked great, thanks! And way faster than running a separate listening server (the Klein way).
I built the webservices/wap example you linked to...very useful!
If I need to respond to HTTP requests with just a string rather than rendered html, is there another dict key I should use (instead of "render")?
"sandbox": true,
"routes": [
{
"path": "/greeting/<name>",
"method": "GET",
"call": "com.test.greeting",
"render": "greeting.html" <== is there a different key for responding with a string rather than html?
}
]
If someone can help me get it working, I'm happy to do a pull request and update the klein example, which is very old and doesn't work with any current versions of crossbar or autobahn/python.
To handle Klein request/response asynchronously, the example code (2014) needs updating for crossbar 21.3.1 and newer, as the example code uses the keyword "standalone" which errors out as it was for way back when WAMP router shipped with autobahn/python (now separated).
The problem is the last line of https://github.com/crossbario/autobahn-python/blob/master/examples/twisted/wamp/app/klein/example1/server_web.py
wampapp.run("ws://127.0.0.1:9000", "realm1", standalone=False)
What I tried so far:
I included the squaring rRPC function in one of my existing components. That registered the squaring function fine, so no need for server_wamp.py as used in the example.
It also meant I could remove the problem line:
wampapp.run("ws://127.0.0.1:9000", "realm1", standalone=False)
from server_web.py which now is:
I used test_web.html:
The above tests the squaring function, so I know that is registered successfully, and server_web.py runs fine until I try to click the submit button in test_web.html, when the following traceback: Unhandled Error writing response [...] builtins.TypeError: object of type 'generator' has no len()