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

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

Add example.

File size: 3.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=== Examples
59
60<enscript highlight=scheme>
61 ;; server.scm
62 (import (json-rpc))
63 (parameterize
64   ((json-rpc-handler-table
65      `(("hello" . ,(lambda (params)
66                      (let ((name (cdr (assoc 'name params))))
67                        (string-append "Hello " name))))
68        ("exit" . ,(lambda (params)
69                     (json-rpc-exit)
70                     #f)))))
71  (json-rpc-start-server/tcp 4242))
72</enscript>
73
74<enscript highlight=scheme>
75 ;; client.scm
76 (import (json-rpc))
77 (json-rpc-call/tcp "localhost" 4242 "hello" '((name . "World!")))
78</enscript>
79
80=== License
81
82MIT License
83
84Copyright (c) 2021 Ricardo G. Herdt
85
86Permission is hereby granted, free of charge, to any person obtaining a
87copy of this software and associated documentation files (the "Software"),
88to deal in the Software without restriction, including without limitation
89the rights to use, copy, modify, merge, publish, distribute, sublicense,
90and/or sell copies of the Software, and to permit persons to whom the
91Software is furnished to do so, subject to the following conditions:
92
93The above copyright notice and this permission notice shall be included in
94all copies or substantial portions of the Software.
95
96THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
97IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
98FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
99THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
100LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
101FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
102DEALINGS IN THE SOFTWARE.
103
104=== Version History
105
106* 0.0.1 Initial version.
107
Note: See TracBrowser for help on using the repository browser.