txJSONRPC: issue accesson Session information and returning error
G'day everybody, I'm using TxJSON-RPC with twisted.web for a project. In my JSON-RPC method, I want to access the Request instance to call getSession() (I need to identify the user which is calling the method with the Session instance), but the request object is not forwarded to jsonrpc_xxx methods. I don't see any other way than overidding the render method, is there any better way ? My other issue is that I want to be able to return a jsonrpc error (when the user is not identified), and when I send a jsonrpclib.Fail(12,"hello") like in the tests, I have a HTML 500 error. Any suggestion ? Thanks Goffi
On Jan 27, 2011, at 7:01 PM, goffi wrote:
G'day everybody,
I'm using TxJSON-RPC with twisted.web for a project. In my JSON-RPC method, I want to access the Request instance to call getSession() (I need to identify the user which is calling the method with the Session instance), but the request object is not forwarded to jsonrpc_xxx methods. I don't see any other way than overidding the render method, is there any better way ?
Technically, no. The issue is just that your approach wouldn't exactly be the "twisted way". Instead, in one of your getChild/locateChild methods, you should be passing the user info as an argument to the constructor for your JSON-RPC resource instance, and saving it as an instance attribute. This will allow you to test your RPC resource more easily as well. Ideally, you'll only use that user info for data purposes, not to delegate access to some method. If different users are to have different abilities, they should get different kinds of resource instances returned. But for simpler security setups, it's okay to cheat and just throw in a few if-statements.
My other issue is that I want to be able to return a jsonrpc error (when the user is not identified), and when I send a jsonrpclib.Fail(12,"hello") like in the tests, I have a HTML 500 error. Any suggestion ?
I think you might just need to use jsonrpclib.Fault, not Fail. -phil
On 12:01 am, goffi@goffi.org wrote:
G'day everybody,
I'm using TxJSON-RPC with twisted.web for a project. In my JSON-RPC method, I want to access the Request instance to call getSession() (I need to identify the user which is calling the method with the Session instance), but the request object is not forwarded to jsonrpc_xxx methods. I don't see any other way than overidding the render method, is there any better way ?
twisted.web.xmlrpc had the same limitation until http://tm.tl/3073 was resolved recently. For that ticket we added a `withRequest` decorator. The same solution could most likely be applied to txjsonrpc. Jean-Paul
My other issue is that I want to be able to return a jsonrpc error (when the user is not identified), and when I send a jsonrpclib.Fail(12,"hello") like in the tests, I have a HTML 500 error. Any suggestion ?
Thanks Goffi
_______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
Thanks for the answers. For the moment I'm saving the request to self.request, I like the idea of the decorator.
I think you might just need to use jsonrpclib.Fault, not Fail.
Sorry for the typo, I'm actually using jsonrpclib.Fault and not Fail, but I found my (stupid !) mistake: I was raising it directly in the render method (the idea is: you are not identified, you can't use the json API) instead of managing it with JSONRPC._cbRender. I'm now doing that, and it works as expected. Thanks Goffi On 28/01/2011 19:48, exarkun@twistedmatrix.com wrote:
On 12:01 am, goffi@goffi.org wrote:
G'day everybody,
I'm using TxJSON-RPC with twisted.web for a project. In my JSON-RPC method, I want to access the Request instance to call getSession() (I need to identify the user which is calling the method with the Session instance), but the request object is not forwarded to jsonrpc_xxx methods. I don't see any other way than overidding the render method, is there any better way ? twisted.web.xmlrpc had the same limitation until http://tm.tl/3073 was resolved recently. For that ticket we added a `withRequest` decorator. The same solution could most likely be applied to txjsonrpc.
Jean-Paul
My other issue is that I want to be able to return a jsonrpc error (when the user is not identified), and when I send a jsonrpclib.Fail(12,"hello") like in the tests, I have a HTML 500 error. Any suggestion ?
Thanks Goffi
_______________________________________________ Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
Twisted-web mailing list Twisted-web@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
participants (3)
-
exarkun@twistedmatrix.com
-
goffi
-
Phil Christensen