xref: /spdk/go/rpc/README.md (revision 1078198e78653b2f39414c1566740018d76ee73d)
1# JSON-RPC 2.0 client in Go
2
3This directory contains JSON-RPC client written in Go. The main goal is to ease communication with
4SPDK over Unix and TCP socket in Go. In addition, this repository provides integration of a client
5with `rpc.py` - Go client replaces Python client.
6
7## Client integration with rpc.py
8
9### Build
10
11Requirements:
12
13* `go` (v1.21 or above)
14
15There are two ways to build files required for client replacement:
16
171. Manual - go to [go/rpc](./) and invoke
18
19```shell
20make
21```
22
232. During configuration part of SPDK add `--with-golang` flag.
24
25### Usage
26
27After successful installation you will be able to use Go client from within `rpc.py`
28
29```shell
30rpc.py --go-client RPC_COMMAND
31```
32
33If you want to use Go client for every RPC command of `rpc.py` without adding `--go-client` flag
34you can define a new environment
35variable
36
37```bash
38SPDK_JSONRPC_GO_CLIENT=1
39```
40
41## Examples
42
43Examples how to integrate this client into your Go code can be
44found [here](../../examples/go/hello_gorpc)
45
46## API
47
48### Client
49
50Client is a main component of RPC client. It is responsible for sending and receiving JSON-RPC 2.0
51calls.
52
53#### Call
54
55Sends RPC call with specified method and parameters.
56
57Input:
58
59- `method`: Name of the method to be invoked.
60- `params`: Set of parameters to be used during invocation of the method.
61
62Output:
63
64- `response`: Response for rpc call. Contains version of JSON-RPC protocol, id, error or result.
65- `error`: Contains error if something goes wrong during creation of a request or
66 sending/receiving rpc call. Otherwise `nil`.
67
68#### Close
69
70Close method closes connection with underlying stream.
71
72Output:
73
74- `error`: Contains error when closing a stream fails. Otherwise `nil`.
75
76### Request
77
78Struct represents JSON-RPC 2.0 Request object. For more information please visit
79[jsonrpc.org/specification#request_object](https://www.jsonrpc.org/specification#request_object)
80
81### Response
82
83Struct represents JSON-RPC 2.0 Response object. For more information please visit
84[jsonrpc.org/specification#response_object](https://www.jsonrpc.org/specification#response_object)
85
86### Error
87
88Struct represents JSON-RPC 2.0 Error object. For more information please visit
89[jsonrpc.org/specification#error_object](https://www.jsonrpc.org/specification#error_object)
90
91### Other
92
93#### CreateClientWithJsonCodec
94
95This method creates a new JSON-RPC 2.0 client. Both Unix and TCP sockets are supported.
96
97Input:
98
99- `network`: Type of network. Both `unix` and `tcp` are supported.
100- `address`: Address to given network.
101
102Output:
103
104- `client`: New [Client](#client) struct.
105- `error`: Contains error if something goes wrong during creation of a client. Otherwise `nil`.
106