pyramid_rpc chaining decorators

44 views
Skip to first unread message

Vlad K.

unread,
Aug 7, 2012, 11:42:40 AM8/7/12
to pylons-...@googlegroups.com


Hi all,


I have a JSON RPC API as a set of methods which all have some common
processing involved, esp. regarding exceptions. To catch those and in
turn raise proper JsonRpcError, with proper code and message, I wanted
to wrap the methods in a common decorator.

@common_rpc
@jsonrpc_method(endpoint="jsonrpc", method="blahblah")
def blahblah(request, ...params...):
...


But then I get "method not found", and if I swap @common_rpc with
@jsonrpc_method, of course then params are not identified by pyramid_rpc
(MapplyViewMapper).

Am I doing something wrong, perhaps missing to do something with
Venusian? Or is it impossible to chain decorators with @jsonrpc_method?


Thanks.

--


.oO V Oo.


Work Hard,
Increase Production,
Prevent Accidents,
and
Be Happy! ;)

Michael Merickel

unread,
Aug 7, 2012, 12:56:18 PM8/7/12
to pylons-...@googlegroups.com
You haven't shown me the signature of your decorator or any errors.
Ignoring that, jsonrpc_method, similar to view_config in pyramid,
supports a `decorator` argument that guarantees you a common signature
for decorators:

def common_rpc(view):
def _wrapped(context, request):
# do stuff with request.rpc_args or whatever else
return view(context, request)
return _wrapped

@jsonrpc_method(endpoint="jsonrpc", method="blahblah", decorator=common_rpc)
def blahblah_view(request, .. params):
return {}
> --
> You received this message because you are subscribed to the Google Groups
> "pylons-discuss" group.
> To post to this group, send email to pylons-...@googlegroups.com.
> To unsubscribe from this group, send email to
> pylons-discus...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/pylons-discuss?hl=en.
>

Vlad K.

unread,
Aug 7, 2012, 1:51:40 PM8/7/12
to pylons-...@googlegroups.com
On 08/07/2012 06:56 PM, Michael Merickel wrote:
You haven't shown me the signature of your decorator or any errors.
Ignoring that, jsonrpc_method, similar to view_config in pyramid,
supports a `decorator` argument that guarantees you a common signature
for decorators:


Ah, I missed to check the docs on config.add_view(). Yeah, the decorator argument now does what I expect of the code.

The error was simple, default pyramid_jsonrpc "method not found" which you get if you call an unregistered method, and the common_rpc() decorator is the following:

def common_rpc(f):
    def wrapped_rpc(*args, **kwargs):
        try:
            return f(*args, **kwargs)
        except c.Invalid as e:
            fields = e.asdict()
            #log.debug(u"Invalid params: " + unicode(fields))
            raise JsonRpcParamsInvalid(data=fields)
        except PermissionError as e:
            raise JsonRpcError(code=e.code, message=e.message)
        except NotFoundError as e:
            raise JsonRpcError(code=E_NOT_FOUND)
       
    return wrapped_rpc

Many thanks.
Reply all
Reply to author
Forward
0 new messages