Add feature that allows adding Dispatcher context to function calls
This fixes #79, and JSONRPCResponseManager now automatically adds the JSON-RPC id to the Dispatcher context.
Description of the Change
As #79 pointed out, it would be nice to be able to add context data that can be passed to a JSON-RPC method handler, but since JSONRPCResponseManager loops over the JSON-RPC messages and calls the handler for us, the only way to get the id of the JSON-RPC message is for JSONRPCResponseManager to add the id to the context, since the user does not have access to it. This uses a similar approach to what https://pjrpc.readthedocs.io/en/latest/pjrpc/server.html uses, i.e. the context argument name is passed to the decorator.
Alternate Designs
https://jsonrpcserver.readthedocs.io/en/stable/api.html#context passes the context data as part of the dispatch() call, but doing it that way breaks the sugar that JSONRPCResponseManager provides to loop over all the JSON-RPC messages for us.
Benefits
Users can now pass their own context, and users will now have access to the JSON-RPC message id.
Possible Drawbacks
None that I can think of.
Applicable Issues
#79
@ppena-LiveData thank you for the contribution!
Could you please share more information about your use case? Why do you need accessing message id from the client? What could be examples of context other than framework request object?
see my comment here: https://github.com/pavlov99/json-rpc/issues/79#issuecomment-885146740
@pavlov99 given the opt-in nature of this PR, would you please consider accepting it?
Can we hardcode the callback parameter name? I always feel uneasy when handling parameter names as strings, prevents static analysis and are not RPython compatible.
If that's what @pavlov99 prefers, I can do that. Maybe instead of @d.add_method(context_arg="context"), we could just have true/false, e.g. @d.add_method(context_arg=True). Although, I don't understand how that helps with static analysis and RPython compatibility... @kalvdans, do you have examples of the problems you are foreseeing?
@ppena-LiveData @jrobbins-LiveData I know it's been a while. I just got access to the computer, accepted the PR, and released 1.14.0: https://pypi.org/project/json-rpc/1.14.0/