2020-12-06 03:20:42 +03:00
|
|
|
:: json-rpc: protocol utilities
|
|
|
|
::
|
|
|
|
/- *json-rpc
|
|
|
|
|%
|
|
|
|
++ request-to-hiss
|
|
|
|
|= [url=purl:eyre req=request]
|
|
|
|
^- hiss:eyre
|
|
|
|
:- url
|
|
|
|
:+ %post
|
|
|
|
%- ~(gas in *math:eyre)
|
|
|
|
~['Content-Type'^['application/json']~]
|
|
|
|
%- some
|
|
|
|
%- as-octt:mimes:html
|
|
|
|
(en-json:html (request-to-json req))
|
|
|
|
::
|
|
|
|
++ request-to-json
|
|
|
|
|= request
|
|
|
|
^- json
|
|
|
|
%- pairs:enjs:format
|
2021-07-07 18:29:12 +03:00
|
|
|
:~ jsonrpc+s+'2.0'
|
2020-12-06 03:20:42 +03:00
|
|
|
id+s+id
|
|
|
|
method+s+method
|
|
|
|
::
|
|
|
|
:- %params
|
|
|
|
^- json
|
|
|
|
?- -.params
|
2021-05-03 14:21:16 +03:00
|
|
|
%list [%a +.params]
|
2021-05-21 14:39:06 +03:00
|
|
|
:: FIXME: support either %map or %object (also in /sur/json/rpc)
|
|
|
|
::
|
|
|
|
%map [%o +.params]
|
2021-05-03 14:21:16 +03:00
|
|
|
%object [%o (~(gas by *(map @t json)) +.params)]
|
|
|
|
== ==
|
2021-04-29 11:56:29 +03:00
|
|
|
::
|
|
|
|
++ response-to-json
|
|
|
|
|= =response
|
|
|
|
^- json
|
|
|
|
:: TODO: consider all cases
|
|
|
|
::
|
|
|
|
?+ -.response ~|([%unsupported-rpc-response response] !!)
|
2021-07-07 18:29:12 +03:00
|
|
|
%batch a+(turn bas.response response-to-json)
|
|
|
|
::
|
2021-04-29 11:56:29 +03:00
|
|
|
%result
|
|
|
|
:- %o
|
|
|
|
%- molt
|
|
|
|
^- (list [@t json])
|
2021-05-04 11:08:44 +03:00
|
|
|
:: FIXME: return 'id' as string, number or NULL
|
2021-05-21 14:39:06 +03:00
|
|
|
::
|
|
|
|
:~ ['jsonrpc' s+'2.0']
|
|
|
|
['id' s+id.response]
|
2021-05-11 14:28:55 +03:00
|
|
|
['result' res.response]
|
2021-05-04 11:08:44 +03:00
|
|
|
==
|
2021-04-29 11:56:29 +03:00
|
|
|
::
|
|
|
|
%error
|
|
|
|
:- %o
|
|
|
|
%- molt
|
|
|
|
^- (list [@t json])
|
2021-05-21 14:39:06 +03:00
|
|
|
:~ ['jsonrpc' s+'2.0']
|
|
|
|
['id' ?~(id.response ~ s+id.response)]
|
2021-05-04 11:08:44 +03:00
|
|
|
['code' n+code.response]
|
2021-04-29 11:56:29 +03:00
|
|
|
['message' s+message.response]
|
|
|
|
==
|
|
|
|
==
|
2021-04-29 12:23:50 +03:00
|
|
|
::
|
|
|
|
++ validate-request
|
|
|
|
|= [body=(unit octs) parse-method=$-(@t term)]
|
2021-07-07 18:29:12 +03:00
|
|
|
^- (unit batch-request)
|
2021-04-29 12:23:50 +03:00
|
|
|
?~ body ~
|
|
|
|
?~ jon=(de-json:html q.u.body) ~
|
2021-07-07 00:10:05 +03:00
|
|
|
=, dejs-soft:format
|
2021-07-07 18:29:12 +03:00
|
|
|
=; reparser
|
|
|
|
?: ?=([%a *] u.jon)
|
|
|
|
(bind ((ar reparser) u.jon) (lead %a))
|
|
|
|
(bind (reparser u.jon) (lead %o))
|
2021-04-29 12:23:50 +03:00
|
|
|
%- ot
|
2021-05-03 14:21:16 +03:00
|
|
|
:~ :: FIXME: parse 'id' as string, number or NULL
|
|
|
|
::
|
|
|
|
['id' so]
|
|
|
|
['jsonrpc' (su (jest '2.0'))]
|
2021-04-29 12:23:50 +03:00
|
|
|
['method' (cu parse-method so)]
|
|
|
|
::
|
|
|
|
:- 'params'
|
|
|
|
|= =json
|
2021-07-07 00:10:05 +03:00
|
|
|
^- (unit request-params)
|
|
|
|
?+ -.json ~
|
|
|
|
%a `[%list ((ar:dejs:format same) json)]
|
|
|
|
%o `[%map ((om:dejs:format same) json)]
|
2021-05-03 14:21:16 +03:00
|
|
|
== ==
|
2021-05-21 14:39:06 +03:00
|
|
|
::
|
|
|
|
++ error
|
|
|
|
|_ id=@t
|
|
|
|
:: https://www.jsonrpc.org/specification#error_object
|
|
|
|
::
|
|
|
|
++ parse [%error id '-32700' 'Failed to parsed']
|
|
|
|
++ request [%error id '-32600' 'Invalid Request']
|
|
|
|
++ method [%error id '-32601' 'Method not found']
|
|
|
|
++ params [%error id '-32602' 'Invalid params']
|
|
|
|
++ internal [%error id '-32603' 'Internal error']
|
|
|
|
++ not-found [%error id '-32000' 'Resource not found']
|
|
|
|
--
|
2020-12-06 03:20:42 +03:00
|
|
|
--
|