json-rpc

  1. json-rpc
    1. Description
    2. Author
    3. Repository
    4. Requirements
    5. Documentation
      1. High-level API
      2. Low-level API
        1. Error handling
    6. Examples
    7. License
    8. Version History

Description

An implementation of the JSON-RPC for Scheme.

The JSON-RPC allows calling procedures on remote servers by exchanging JSON objects. It's specification can be found at https://www.jsonrpc.org/specification.

Author

Ricardo G. Herdt

Repository

https://gitlab.com/rgherdt/scheme-json-rpc.git

Requirements

Documentation

High-level API

[parameter] (json-rpc-handler-table)

An association list mapping method names (string) to functions that get an (unmarshalled) scheme object as input. These handlers must return one of the following: - 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. - #f, if no response is expected. This is the way to handle notifications.

[procedure] (json-rpc-call in-port out-port method params)
[procedure] (json-rpc-call/tcp method params tcp-address tcp-port)

Call the remote procedure method (string) with the JSON-mappable scheme object params. See medea.

[procedure] (json-rpc-start-server/tcp tcp-port)

Listen on tcp-port and loop over input connections, answering requests according to json-rpc-handler-table.

Low-level API

[procedure] (json-rpc-read input-port)

Read from input-port and return a Scheme object based on the provided json-string->scheme parameter.

[procedure] (json-rpc-write scm output-port)

Convert the scheme object scm to a JSON string using the provided paramter scheme->json-string and writes the result to output-port.

[procedure] (json-rpc-loop input-port output-port)

JSON-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.

Error handling

User-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.

[parameter] (custom-error-codes)

JSON-RPC supports custom server-side error codes. This parameter defines an alist mapping custom error names (symbols) to error codes (integer) ranging between -32000 and -32099.

[procedure] (make-json-rpc-custom-error error-symbol [msg])

Create 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.

[procedure] (make-json-rpc-internal-error)

Create a generic error object.

[procedure] (json-rpc-error? condition)
[procedure] (json-rpc-custom-error? condition)
[procedure] (json-rpc-internal-error? condition)

Predicates for several error objects.

Examples

 ;; server.scm
 (import (json-rpc))
 (parameterize
   ((json-rpc-handler-table
      `(("hello" . ,(lambda (params)
                      (let ((name (cdr (assoc 'name params))))
                        (string-append "Hello " name))))
        ("exit" . ,(lambda (params)
                     (json-rpc-exit)
                     #f)))))
  (json-rpc-start-server/tcp 4242))
 ;; client.scm
 (import (json-rpc))
 (json-rpc-call/tcp "hello" '((name . "World!")) "localhost" 4242)

License

MIT License

Copyright (c) 2021 Ricardo G. Herdt

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version History

0.4.4
fix build problems
0.4.3
fix problems with utf8 data
0.4.0 - 0.4.2
Mostly guile-related fixes
0.3.0
Batch requests; rearrange implementation specific code; fix bugs
0.2.10
fix dependency bug
0.2.9
add Gambit support; replace medea by srfi-180
0.2.4 - 0.2.8
Bug fix (mostly for guile)
0.2.3
add json-rpc-log-file parameter to log to files
0.2.2
[ignore]
0.2.1
Refactoring for Guile build
0.2.0
Handlers returning #f now don't generate responses (instead of "null" as before). Fix json-rpc-read
0.1.1
Expose invalid-request error type
0.1
Initial version.