source: project/wiki/eggref/5/json-rpc @ 40063

Last change on this file since 40063 was 40063, checked in by rgherdt, 7 weeks ago

Update description of handling functions; add 0.2.0

File size: 4.8 KB
Line 
1[[tags: egg]]
2
3== json-rpc
4
5[[toc:]]
6
7=== Description
8
9An implementation of the JSON-RPC for Scheme.
10
11The JSON-RPC allows calling procedures on remote servers by exchanging JSON objects. It's specification can be found at [https://www.jsonrpc.org/specification|https://www.jsonrpc.org/specification].
12
13=== Author
14
15Ricardo G. Herdt
16
17=== Repository
18
19[[https://gitlab.com/rgherdt/scheme-json-rpc.git|https://gitlab.com/rgherdt/scheme-json-rpc.git]]
20
21=== Requirements
22
23* [[r7rs]]
24* [[srfi-1]]
25* [[srfi-18]]
26* [[srfi-69]]
27
28=== Documentation
29
30==== High-level API
31
32<parameter>(json-rpc-handler-table)</parameter>
33
34An association list mapping method names ({{string}s}) to functions that get an (unmarshalled) scheme object as input. These handlers must return one of the following:
35- a scheme object that is 'mappable' to a JSON string, which becomes the {result} content of the response. See [[medea]] for the mapping between JSON and Scheme objects.
36- #f, if no response is expected. This is the way to handle notifications.
37
38<procedure>(json-rpc-call in-port out-port method params)</procedure>
39<procedure>(json-rpc-call/tcp tcp-address tcp-port method params)</procedure>
40
41Call the remote procedure {{method}} (string) with the JSON-mappable scheme object {{params}}. See [[medea]].
42
43<procedure>(json-rpc-start-server/tcp tcp-port)</procedure>
44
45Listen on {{tcp-port}} and loop over input connections, answering requests according to {{json-rpc-handler-table}}.
46
47==== Low-level API
48<procedure>(json-rpc-read input-port)</procedure>
49
50Read from {{input-port}} and return a Scheme object based on the provided {{json-string->scheme}} parameter.
51
52<procedure>(json-rpc-write scm output-port)</procedure>
53
54Convert the scheme object {{scm}} to a JSON string using the provided paramter {{scheme->json-string}} and writes the result to {{output-port}}.
55
56<procedure>(json-rpc-loop input-port output-port)</procedure>
57
58JSON-RPC 2.0 demands that all requests are answered. This functions loops over {{input-port}} and answers to the requests through {{output-port}} using the handlers defined in {{json-rpc-handler-table}}.
59
60===== Error handling
61
62User-created errors are meant to be raised by handlers defined in {json-rpc-handler-table}, and are properly delivered to the calling process according to the JSON-RPC protocol.
63
64<parameter>(custom-error-codes)</parameter>
65
66JSON-RPC supports custom server-side error codes. This parameter defines an alist mapping custom error names ({symbol}s) to error codes ({integer}) ranging between -32000 and -32099.
67
68<procedure>(make-json-rpc-custom-error error-symbol [msg])</procedure>
69
70Create an error instance of {error-symbol} ({symbol}) with an optional {msg} ({string}). It's an error if {error-type} isn't defined in {custom-error-codes}.
71
72<procedure>(make-json-rpc-internal-error)</procedure>
73
74Create a generic error object.
75
76<procedure>(json-rpc-error? condition)</procedure>
77<procedure>(json-rpc-custom-error? condition)</procedure>
78<procedure>(json-rpc-internal-error? condition)</procedure>
79
80Predicates for several error objects.
81
82=== Examples
83
84<enscript highlight=scheme>
85 ;; server.scm
86 (import (json-rpc))
87 (parameterize
88   ((json-rpc-handler-table
89      `(("hello" . ,(lambda (params)
90                      (let ((name (cdr (assoc 'name params))))
91                        (string-append "Hello " name))))
92        ("exit" . ,(lambda (params)
93                     (json-rpc-exit)
94                     #f)))))
95  (json-rpc-start-server/tcp 4242))
96</enscript>
97
98<enscript highlight=scheme>
99 ;; client.scm
100 (import (json-rpc))
101 (json-rpc-call/tcp "localhost" 4242 "hello" '((name . "World!")))
102</enscript>
103
104=== License
105
106MIT License
107
108Copyright (c) 2021 Ricardo G. Herdt
109
110Permission is hereby granted, free of charge, to any person obtaining a
111copy of this software and associated documentation files (the "Software"),
112to deal in the Software without restriction, including without limitation
113the rights to use, copy, modify, merge, publish, distribute, sublicense,
114and/or sell copies of the Software, and to permit persons to whom the
115Software is furnished to do so, subject to the following conditions:
116
117The above copyright notice and this permission notice shall be included in
118all copies or substantial portions of the Software.
119
120THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
121IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
122FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
123THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
124LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
125FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
126DEALINGS IN THE SOFTWARE.
127
128=== Version History
129
130* 0.2.0  Handlers returning #f now don't generate responses (instead of "null" as before). Fix json-rpc-read
131* 0.1.1 Expose invalid-request error type
132* 0.1   Initial version.
133
Note: See TracBrowser for help on using the repository browser.