Open whacked opened 8 years ago
You're absolutely correct about #1. It hadn't even occurred to me that you might want to select a different endpoint. The options I see are:
As for #2, this was intentional as it's required by the JSON-RPC 1.0 spec. The params must be an array of objects.
http://json-rpc.org/wiki/specification
However, I see that JSON-RPC 2.0 also allows it to be an object, to supply named rather than positional parameters. Unfortunately there's no way to add this functionality to the json-rpc function without breaking it. Cons detection won't work because the original caller may intend to pass an object as the first and only argument, not as the params itself.
So to shoot two birds with one stone, I think we need a whole new function adding support for both. It would look like this:
(defun json-rpc-2.0 (connection endpoint method params)
...)
Where params must be a vector or non-nil list (and the details of encoding this value are left to json-encode). It would also add a jsonrpc 2.0 member to the request. For consistency I'd also create a json-rpc-1.0 that is just like old json-rpc, but has an endpoint argument.
Sound reasonable?
The (defun json-rpc-2.0 (connection endpoint method params) ...)
is reasonable but it entails including the endpoint in every call to json-rpc-2.0
, correct? That seems a bit cumbersome especially since you've encapsulated the connection object in the first place. I was hoping there was an analogue of subclassing the cl-defstruct
of json-rpc
.
The workaroundhack I posted is just a path of least resistance that preserves a similar call signature to the current version, and also some other client libraries such as jquery-jsonrpc. Incidentally, jquery-jsonrpc has a namespace
setting, which would be App
in the toy example above. I didn't think that warranted an additional abstraction, but conceptually that seems to also fit in the cl-defstruct
. From what it seems, cl-defstruct
is not "subclass-able" though?
So it would seem like the hard-coded root path is the main issue. To retain flexibility, what about splitting the json-rpc
logic at the request building point like at with-current-buffer
?
From my understanding of
json-rpc
in json-rpc.el L77-L93, there are 2 limitations that I encountered in my use case./
(line 88)vconcat
; I only played a little bit with therequest
library but it seems like this precludes using named parameters like`((foo . "bar"))
.Below is a modified version of
json-rpc
I am using to work around these 2 issues. The endpoint feature is probably better suited as another attribute into thecl-defstruct
. Thecons
detection section seems inelegant but is the best way for me now.Here's an illustration of the use case
server
emacs (contd from above)