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

Last change on this file since 40027 was 40027, checked in by rgherdt, 2 months ago

Expand API.

File size: 4.5 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 (*strings*) to functions that get an (unmarshalled) scheme object as input. These handlers must return a scheme object that is *mappable* to a JSON string. See [[medea]].
35
36<procedure>(json-rpc-call in-port out-port method params)</procedure>
37<procedure>(json-rpc-call/tcp tcp-address tcp-port method params)</procedure>
38
39Call the remote procedure {{method}} (string) with the JSON-mappable scheme object {{params}}. See [[medea]].
40
41<procedure>(json-rpc-start-server/tcp tcp-port)</procedure>
42
43Listen on {{tcp-port}} and loop over input connections, answering requests according to {{json-rpc-handler-table}}.
44
45==== Low-level API
46<procedure>(json-rpc-read input-port)</procedure>
47
48Read from {{input-port}} and return a Scheme object based on the provided {{json-string->scheme}} parameter.
49
50<procedure>(json-rpc-write scm output-port)</procedure>
51
52Convert the scheme object {{scm}} to a JSON string using the provided paramter {{scheme->json-string}} and writes the result to {{output-port}}.
53
54<procedure>(json-rpc-loop input-port output-port)</procedure>
55
56JSON-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}}.
57
58===== Error handling
59
60User-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.
61
62<parameter>(custom-error-codes)</parameter>
63
64JSON-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.
65
66<procedure>(make-json-rpc-custom-error error-symbol [msg])</procedure>
67
68Create 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}.
69
70<procedure>(make-json-rpc-internal-error)</procedure>
71
72Create a generic error object.
73
74<procedure>(json-rpc-error? condition)</procedure>
75<procedure>(json-rpc-custom-error? condition)</procedure>
76<procedure>(json-rpc-internal-error? condition)</procedure>
77
78Predicates for several error objects.
79
80=== Examples
81
82<enscript highlight=scheme>
83 ;; server.scm
84 (import (json-rpc))
85 (parameterize
86   ((json-rpc-handler-table
87      `(("hello" . ,(lambda (params)
88                      (let ((name (cdr (assoc 'name params))))
89                        (string-append "Hello " name))))
90        ("exit" . ,(lambda (params)
91                     (json-rpc-exit)
92                     #f)))))
93  (json-rpc-start-server/tcp 4242))
94</enscript>
95
96<enscript highlight=scheme>
97 ;; client.scm
98 (import (json-rpc))
99 (json-rpc-call/tcp "localhost" 4242 "hello" '((name . "World!")))
100</enscript>
101
102=== License
103
104MIT License
105
106Copyright (c) 2021 Ricardo G. Herdt
107
108Permission is hereby granted, free of charge, to any person obtaining a
109copy of this software and associated documentation files (the "Software"),
110to deal in the Software without restriction, including without limitation
111the rights to use, copy, modify, merge, publish, distribute, sublicense,
112and/or sell copies of the Software, and to permit persons to whom the
113Software is furnished to do so, subject to the following conditions:
114
115The above copyright notice and this permission notice shall be included in
116all copies or substantial portions of the Software.
117
118THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
119IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
120FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
121THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
122LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
123FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
124DEALINGS IN THE SOFTWARE.
125
126=== Version History
127
128* 0.1 Initial version.
129
Note: See TracBrowser for help on using the repository browser.