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
|
|
|
|
:~ jsonrpc+s+'0.2'
|
|
|
|
id+s+id
|
|
|
|
method+s+method
|
|
|
|
::
|
|
|
|
:- %params
|
|
|
|
^- json
|
|
|
|
?- -.params
|
2021-05-03 14:21:16 +03:00
|
|
|
%list [%a +.params]
|
|
|
|
%map [%o +.params]
|
|
|
|
%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] !!)
|
|
|
|
%result
|
|
|
|
:- %o
|
|
|
|
%- molt
|
|
|
|
^- (list [@t json])
|
2021-05-04 11:08:44 +03:00
|
|
|
:: FIXME: return 'id' as string, number or NULL
|
|
|
|
::
|
|
|
|
:~ ['jsonrpc' s+'2.0']
|
|
|
|
['id' s+id.response]
|
|
|
|
['res' res.response]
|
|
|
|
==
|
2021-04-29 11:56:29 +03:00
|
|
|
::
|
|
|
|
%error
|
|
|
|
:- %o
|
|
|
|
%- molt
|
|
|
|
^- (list [@t json])
|
2021-05-04 11:08:44 +03:00
|
|
|
:~ ['jsonrpc' s+'2.0']
|
|
|
|
['id' ?~(id.response ~ s+id.response)]
|
|
|
|
['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)]
|
|
|
|
^- (unit request)
|
|
|
|
?~ body ~
|
|
|
|
?~ jon=(de-json:html q.u.body) ~
|
|
|
|
:: ignores non-object responses
|
|
|
|
::
|
|
|
|
:: ?. ?=([%o *] json) ~|([%format-not-valid json] !!)
|
|
|
|
?. ?=([%o *] u.jon) ~
|
|
|
|
%- some
|
|
|
|
%. u.jon
|
|
|
|
=, dejs:format
|
2021-05-03 14:21:16 +03:00
|
|
|
:: TODO: If parsing fails, return a proper error (not 500)
|
|
|
|
::
|
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
|
|
|
|
^- request-params
|
2021-05-03 14:21:16 +03:00
|
|
|
?+ -.json !!
|
|
|
|
%a [%list ((ar same) json)]
|
|
|
|
%o [%map ((om same) json)]
|
|
|
|
== ==
|
2020-12-06 03:20:42 +03:00
|
|
|
--
|