-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.go
More file actions
642 lines (587 loc) · 22.3 KB
/
messages.go
File metadata and controls
642 lines (587 loc) · 22.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
package srljrpc
import (
"encoding/json"
"fmt"
"math/rand"
"strings"
"time"
"github.com/azyablov/srljrpc/actions"
"github.com/azyablov/srljrpc/apierr"
"github.com/azyablov/srljrpc/datastores"
"github.com/azyablov/srljrpc/formats"
"github.com/azyablov/srljrpc/methods"
"github.com/azyablov/srljrpc/yms"
)
// NewGetRequest provides a new Request with the GET method and the given paths, which more advanced version of JRPCClient.Get().
func NewGetRequest(paths []string, recursion bool, defaults bool, of formats.EnumOutputFormats, ds datastores.EnumDatastores) (*Request, error) {
var cmds []*Command
var cmdOpt []CommandOption
// Setting command options
if recursion {
cmdOpt = append(cmdOpt, WithoutRecursion())
}
if defaults {
cmdOpt = append(cmdOpt, WithDefaults())
}
for _, path := range paths {
cmd, err := NewCommand(actions.NONE, path, CommandValue(""), cmdOpt...)
if err != nil {
return nil, apierr.NewMessageError(apierr.CodeMsgCmdCreation, err)
}
cmds = append(cmds, cmd)
}
return NewRequest(methods.GET, cmds, WithOutputFormat(of), WithRequestDatastore(ds))
}
// NewSetRequest provides a new Request with the SET method and the given commands, which more advanced version of JRPCClient.Set().
func NewSetRequest(delete []PV, replace []PV, update []PV, ym yms.EnumYmType, of formats.EnumOutputFormats, ds datastores.EnumDatastores, ct int) (*Request, error) {
// Check if commands are empty for set and TOOLS datastore combination
if (len(delete) != 0 || len(replace) != 0) && ds == datastores.TOOLS {
return nil, apierr.NewMessageError(apierr.CodeMsgSetNotAllowedActForTools, nil)
}
// build the commands
cmds, err := cmdPacker(delete, replace, update)
if err != nil {
return nil, apierr.NewMessageError(apierr.CodeMsgCmdCreation, err)
}
// build the request
if ct == 0 {
return NewRequest(methods.SET, cmds, WithRequestDatastore(ds), WithYmType(ym), WithOutputFormat(of))
} else {
return NewRequest(methods.SET, cmds, WithRequestDatastore(ds), WithYmType(ym), WithOutputFormat(of), WithConfirmTimeout(ct))
}
}
// NewValidateRequest provides a new Request with the VALIDATE method and the given commands, which more advanced version of JRPCClient.Validate().
func NewValidateRequest(delete []PV, replace []PV, update []PV, ym yms.EnumYmType, of formats.EnumOutputFormats, ds datastores.EnumDatastores) (*Request, error) {
// build the commands
cmds, err := cmdPacker(delete, replace, update)
if err != nil {
return nil, apierr.NewMessageError(apierr.CodeMsgCmdCreation, err)
}
// build the request
return NewRequest(methods.VALIDATE, cmds, WithRequestDatastore(ds), WithYmType(ym), WithOutputFormat(of))
}
// NewDiffRequest provides a new Request with the DIFF method and the given commands, which more advanced version of JRPCClient.Diff().
func NewDiffRequest(delete []PV, replace []PV, update []PV, ym yms.EnumYmType, of formats.EnumOutputFormats, ds datastores.EnumDatastores) (*Request, error) {
// Check if commands are empty for diff and TOOLS datastore combination
if (len(delete) != 0 || len(replace) != 0) && ds == datastores.TOOLS {
return nil, apierr.NewMessageError(apierr.CodeMsgSetNotAllowedActForTools, nil)
}
// build the commands
cmds, err := cmdPacker(delete, replace, update)
if err != nil {
return nil, apierr.NewMessageError(apierr.CodeMsgCmdCreation, err)
}
// build the request
return NewRequest(methods.DIFF, cmds, WithRequestDatastore(ds), WithYmType(ym), WithOutputFormat(of))
}
// NewRequest provides a new Request with the given method, commands and options.
// Sequence of functions is applied to the Request in the order of appearance.
func NewRequest(m methods.EnumMethods, cmds []*Command, opts ...RequestOption) (*Request, error) {
r := &Request{}
// set version
r.JSONRpcVersion = "2.0"
// set method
r.Method = &methods.Method{}
err := r.Method.SetMethod(m)
if err != nil {
return nil, apierr.NewMessageError(apierr.CodeMsgSettingMethod, err)
}
// set random ID
rand.Seed(time.Now().UnixNano())
id := rand.Int()
r.setID(id)
// set params and output format
r.Params = &Params{}
r.Params.OutputFormat = &formats.OutputFormat{}
r.Params.Datastore = &datastores.Datastore{}
r.Params.YmType = &yms.YmType{}
// set commands
err = apply_cmds(r, cmds)
if err != nil {
return nil, apierr.NewMessageError(apierr.CodeMsgReqAddingCmds, err)
}
// apply options to request
err = apply_opts(r, opts)
if err != nil {
// Options creating MessageError.
return nil, err
}
return r, nil
}
// JSON RPC Request for get / set / validate methods.
//
// JSONRpcVersion is mandatory. Version, which must be ‟2.0”. No other JSON RPC versions are currently supported.
// ID is mandatory. Client-provided integer. The JSON RPC responds with the same ID, which allows the client to match requests to responses when there are concurrent requests.
// Implementation uses random numbers and verifies Response ID is the same as Request ID.
// Embeds Method and Params.
type Request struct {
JSONRpcVersion string `json:"jsonrpc"`
ID int `json:"id"`
*methods.Method
Params *Params `json:"params"`
}
// Marshaling of the Request into JSON.
func (r *Request) Marshal() ([]byte, error) {
b, err := json.Marshal(r)
if err != nil {
return nil, apierr.NewMessageError(apierr.CodeMsgReqMarshalling, err)
}
return b, nil
}
// Get Request ID.
func (r *Request) GetID() int {
return r.ID
}
// Set Request ID.
func (r *Request) setID(id int) {
r.ID = id
}
// Set output format for the request via embedded Params.
func (r *Request) SetOutputFormat(of formats.EnumOutputFormats) error {
err := r.Params.OutputFormat.SetFormat(of)
if err != nil {
return apierr.NewMessageError(apierr.CodeMsgReqSettingOutFormat, err)
}
return nil
}
// Setting confirm timeout for the Request.
func (r *Request) SetConfirmTimeout(t int) error {
err := r.Params.withConfirmTimeout(t)
if err != nil {
return err
}
return nil
}
// Requester is an interface used by the JSON RPC client to send a request to the server.
type Requester interface {
Marshal() ([]byte, error)
GetMethod() (methods.EnumMethods, error)
GetID() int
SetOutputFormat(of formats.EnumOutputFormats) error
}
// RequestOption is a function type that applies options to a Request.
// Each RequestOption has validation logic implemented to check correctness of the option application and return non nil apierr.MessageError if the option is not correct.
type RequestOption func(*Request) error
// Defines output format RequestOption.
func WithOutputFormat(of formats.EnumOutputFormats) RequestOption {
return func(r *Request) error {
return r.SetOutputFormat(of)
}
}
// Defines confirm timeout RequestOption.
func WithConfirmTimeout(t int) RequestOption {
return func(r *Request) error {
m, err := r.GetMethod()
if err != nil {
return apierr.NewMessageError(apierr.CodeMsgGettingMethod, err)
}
// confirm timeout is only allowed for SET method
if m != methods.SET {
return apierr.NewMessageError(apierr.CodeMsgReqSettingConfirmTimeout, nil)
}
return r.SetConfirmTimeout(t)
}
}
// Defines yang models RequestOption.
func WithYmType(ym yms.EnumYmType) RequestOption {
return func(r *Request) error {
m, err := r.GetMethod()
if err != nil {
return apierr.NewMessageError(apierr.CodeMsgGettingMethod, err)
}
// yang models specification on Request.Params level is not supported for method CLI and GET
if m == methods.CLI || m == methods.GET {
return apierr.NewMessageError(apierr.CodeMsgYANGSpecNotAllowed, nil)
}
err = r.Params.withYmType(ym)
if err != nil {
return apierr.NewMessageError(apierr.CodeMsgReqSettingYMParams, err)
}
return nil
}
}
// RequestOption that sets the datastore for the request in Params level. Overrides the datastore in Command level!
// Implemented logic in this option is to check if datastore is valid for the selected method and perform necessary checks on the commands.
func WithRequestDatastore(ds datastores.EnumDatastores) RequestOption {
return func(r *Request) error {
m, _ := r.GetMethod()
switch m {
case methods.GET:
if ds == datastores.TOOLS {
return apierr.NewMessageError(apierr.CodeMsgReqGetDSNotAllowed, nil)
}
err := r.Params.withDatastore(ds)
if err != nil {
return apierr.NewMessageError(apierr.CodeMsgReqSettingDSParams, err)
}
return nil
case methods.SET:
for _, c := range r.Params.Commands {
c.CleanDatastore() // clean datastore in commands, to be later if such protective measures are needed, since c.IsDefaultDatastore() added as verification check for SET/VALIDATE
a, err := c.Action.GetAction()
if err != nil {
//return err
return apierr.NewMessageError(apierr.CodeMsgReqSetSettingAction, err)
}
// now we can check if action UPDATE has value for CANDIDATE datastore
if ds == datastores.CANDIDATE && a == actions.UPDATE {
if c.Value == "" && !strings.Contains(c.Path, ":") {
return apierr.NewMessageError(apierr.CodeMsgDSCandidateUpdateNoValue, nil)
}
}
// The set method can be used with tools datastores only with the update action.
if ds == datastores.TOOLS && a != actions.UPDATE {
return apierr.NewMessageError(apierr.CodeMsgDSToolsSetUpdateOnly, nil)
}
}
if ds != datastores.CANDIDATE && ds != datastores.TOOLS {
return apierr.NewMessageError(apierr.CodeMsgDSToolsCandidateSetOnly, nil)
}
err := r.Params.withDatastore(ds)
if err != nil {
return apierr.NewMessageError(apierr.CodeMsgReqSettingDSParams, err)
}
return nil
case methods.VALIDATE:
// clean datastore in commands
for _, c := range r.Params.Commands {
c.CleanDatastore() // clean datastore in commands, to be decided later if such protective measures are needed, since c.IsDefaultDatastore() added as verification check for SET/VALIDATE
}
if ds != datastores.CANDIDATE {
return apierr.NewMessageError(apierr.CodeMsgDSCandidateValidateOnly, nil)
}
err := r.Params.withDatastore(ds)
if err != nil {
return apierr.NewMessageError(apierr.CodeMsgReqSettingDSParams, err)
}
return nil
case methods.DIFF:
// clean datastore in commands
for _, c := range r.Params.Commands {
c.CleanDatastore() // clean datastore in commands, to be decided later if such protective measures are needed, since c.IsDefaultDatastore() added as verification check for SET/VALIDATE
}
if ds != datastores.CANDIDATE {
return apierr.NewMessageError(apierr.CodeMsgDSCandidateDiffOnly, nil)
}
err := r.Params.withDatastore(ds)
if err != nil {
return apierr.NewMessageError(apierr.CodeMsgReqSettingDSParams, err)
}
return nil
default:
return apierr.NewMessageError(apierr.CodeMsgDSSpecNotAllowedForUnknownMethod, nil)
}
}
}
// Helper function to add commands to the request and verify if they are valid for the selected method i.e. implements JSON RPC API specification rules:
// correctly set path, action and value. Check for datastore set correctly on the command level, if allowed for the particular method.
func apply_cmds(r *Request, cmds []*Command) error {
// check if commands are empty
if len(cmds) == 0 {
return fmt.Errorf("no commands given")
}
// check if commands aren't empty
for _, c := range cmds {
if c == nil {
return fmt.Errorf("nil commands aren't allowed")
}
}
// check if commands are valid for the selected method
m, err := r.Method.GetMethod()
if err != nil {
return err
}
switch m {
case methods.GET:
for _, c := range cmds {
// path command - Mandatory with the get, set and validate methods.
if c.Path == "" {
return fmt.Errorf("path not found, but should be specified for method %s", m)
}
// The get method can be used with candidate, running, and state datastores, but cannot be used with the tools datastore.
d, err := c.GetDatastore()
if err != nil {
return err
}
if d == datastores.TOOLS {
return fmt.Errorf("datastore TOOLS is not allowed for method %s", m)
}
// Action and value are not allowed for the get method.
if c.Action != nil {
return fmt.Errorf("action not allowed for method %s", m)
}
if c.Value != "" {
return fmt.Errorf("value not allowed for method %s", m)
}
}
case methods.SET:
for _, c := range cmds {
// path command - Mandatory with the get, set and validate methods.
if c.Path == "" {
return fmt.Errorf("path not found, but should be specified for method %s", m)
}
// Check if datastore has been set to default datastore.
if !c.IsDefaultDatastore() {
return fmt.Errorf("command level datastore must not be set for method %s", m)
}
// Action command is mandatory with the set method.
if c.Action == nil {
return fmt.Errorf("action not found, but should be specified for method %s", m)
}
a, err := c.Action.GetAction()
if err != nil {
return err
}
// check if action is valid for the set method
if a == actions.NONE {
return fmt.Errorf("action not found, but should be specified for method %s", m)
}
// Check if value is specified for the set method.
if c.Value == "" && !strings.Contains(c.Path, ":") && a != actions.DELETE && a != actions.UPDATE {
return fmt.Errorf("value isn't specified or not found in the path for method %s", m)
}
if c.Value != "" && a == actions.DELETE {
return fmt.Errorf("value specified for action DELETE for method %s", m)
}
// Check if value is specified in the path and as a separate value for the set method.
if strings.Contains(c.Path, ":") {
sl := strings.Split(c.Path, ":")
if len(sl) != 2 {
return fmt.Errorf("invalid k:v path specification for method %s", m)
}
if c.Value != "" {
return fmt.Errorf("value specified in the path and as a separate value for method %s", m)
}
}
}
case methods.VALIDATE:
for _, c := range cmds {
// path command - Mandatory with the GET, SET and VALIDATE methods.
if c.Path == "" {
return fmt.Errorf("path not found, but should be specified for method %s", m)
}
// Check if datastore has been set to default datastore.
if !c.IsDefaultDatastore() {
return fmt.Errorf("command level datastore must not be set for method %s", m)
}
// Action command is mandatory with the VALIDATE method.
if c.Action == nil {
return fmt.Errorf("action not found, but should be specified for method %s", m)
}
a, err := c.Action.GetAction()
if err != nil {
return err
}
// check if action is valid for the VALIDATE method
if a == actions.NONE {
return fmt.Errorf("action not found, but should be specified for method %s", m)
}
// Check if value is specified for the VALIDATE method.
if c.Value == "" && !strings.Contains(c.Path, ":") && a != actions.DELETE {
return fmt.Errorf("value isn't specified or not found in the path for method %s", m)
}
if c.Value != "" && a == actions.DELETE {
return fmt.Errorf("value specified for action DELETE for method %s", m)
}
// Check if value is specified in the path and as a separate value for the DIFF method.
if strings.Contains(c.Path, ":") {
sl := strings.Split(c.Path, ":")
if len(sl) != 2 {
return fmt.Errorf("invalid k:v path specification for method %s", m)
}
if c.Value != "" {
return fmt.Errorf("value specified in the path and as a separate value for method %s", m)
}
}
}
case methods.DIFF:
for _, c := range cmds {
// Path command - Mandatory for DIFF method.
if c.Path == "" {
return fmt.Errorf("path not found, but should be specified for method %s", m)
}
// Check if datastore has been set to default datastore.
if !c.IsDefaultDatastore() {
return fmt.Errorf("command level datastore must not be set for method %s", m)
}
// Action command is mandatory with the DIFF method.
if c.Action == nil {
return fmt.Errorf("action not found, but should be specified for method %s", m)
}
a, err := c.Action.GetAction()
if err != nil {
return err
}
// check if action is valid for the DIFF method
if !(a == actions.UPDATE || a == actions.DELETE || a == actions.REPLACE) {
return fmt.Errorf("action not found, but should be specified for method %s", m)
}
// Check if value is specified for the DIFF method.
if c.Value == "" && !strings.Contains(c.Path, ":") && a != actions.DELETE {
return fmt.Errorf("value isn't specified or not found in the path for method %s", m)
}
if c.Value != "" && a == actions.DELETE {
return fmt.Errorf("value specified for action DELETE for method %s", m)
}
// Check if value is specified in the path and as a separate value for the DIFF method.
if strings.Contains(c.Path, ":") {
sl := strings.Split(c.Path, ":")
if len(sl) != 2 {
return fmt.Errorf("invalid k:v path specification for method %s", m)
}
if c.Value != "" {
return fmt.Errorf("value specified in the path and as a separate value for method %s", m)
}
}
}
case methods.CLI:
return fmt.Errorf("method %s not supported by Request, please use CLIRequest object", m)
default:
return fmt.Errorf("method %s not supported by Request", m)
}
// checks passed, append commands to request
err = r.Params.appendCommands(cmds)
if err != nil {
return err
}
return nil
}
// CRUD helper packing commands for CRUD operations
func cmdPacker(delete []PV, replace []PV, update []PV) ([]*Command, error) {
var cmds []*Command
for _, pv := range delete {
cmd, err := NewCommand(actions.DELETE, pv.Path, CommandValue(""))
if err != nil {
return nil, err
}
cmds = append(cmds, cmd)
}
for _, pv := range replace {
cmd, err := NewCommand(actions.REPLACE, pv.Path, pv.Value)
if err != nil {
return nil, err
}
cmds = append(cmds, cmd)
}
for _, pv := range update {
cmd, err := NewCommand(actions.UPDATE, pv.Path, pv.Value)
if err != nil {
return nil, err
}
cmds = append(cmds, cmd)
}
return cmds, nil
}
// Helper function applies options to the request.
func apply_opts(r *Request, opts []RequestOption) error {
for _, o := range opts {
if o != nil { // check that's not nil
if err := o(r); err != nil {
return err
}
}
}
return nil
}
// Creates a new CLIRequest object using the provided list of commands executed one by one and output format.
// Each command should have a response under JSON RPC response message - Response under "result" field with respective command index.
func NewCLIRequest(cmds []string, of formats.EnumOutputFormats) (*CLIRequest, error) {
r := &CLIRequest{}
// set version
r.JSONRpcVersion = "2.0"
// set method
r.Method = &methods.Method{}
err := r.Method.SetMethod(methods.CLI)
if err != nil {
return nil, apierr.NewMessageError(apierr.CodeMsgCLISettingMethod, err)
}
// set random ID
rand.Seed(time.Now().UnixNano())
id := rand.Int()
r.setID(id)
// set params
r.Params = &CLIParams{}
r.Params.OutputFormat = &formats.OutputFormat{}
// set commands
err = r.Params.appendCommands(cmds)
if err != nil {
return nil, apierr.NewMessageError(apierr.CodeMsgCLIAddingCmdsInReq, err)
}
// apply options to request
err = r.SetOutputFormat(of)
if err != nil {
return nil, err
}
return r, nil
}
// JSONRpcVersion is mandatory. Version, which must be ‟2.0”. No other JSON RPC versions are currently supported.
//
// ID is mandatory. Client-provided integer. The JSON RPC responds with the same ID, which allows the client to match requests to responses when there are concurrent requests.
// Implementation uses random numbers and verifies Response ID is the same as Request ID.
// Embeds Method (set to CLI by NewCLIRequest) and CLIParams.
type CLIRequest struct {
JSONRpcVersion string `json:"jsonrpc"`
ID int `json:"id"`
*methods.Method
Params *CLIParams `json:"params"`
}
// Marshalling CLIRequest into JSON.
func (r *CLIRequest) Marshal() ([]byte, error) {
b, err := json.Marshal(r)
if err != nil {
return nil, apierr.NewMessageError(apierr.CodeMsgCLIMarshalling, err)
}
return b, nil
}
// Returns the ID of the request.
func (r *CLIRequest) GetID() int {
return r.ID
}
// Sets the ID of the request. Internal method.
func (r *CLIRequest) setID(id int) {
r.ID = id
}
// Sets the output format of the request.
func (r *CLIRequest) SetOutputFormat(of formats.EnumOutputFormats) error {
err := r.Params.OutputFormat.SetFormat(of)
if err != nil {
return apierr.NewMessageError(apierr.CodeMsgCLISettingOutFormat, err)
}
return nil
}
// RpcError is generic JSON RPC error object.
// When a rpc call is made, the Server MUST reply with a Response, except for in the case of Notifications. The Response is expressed as a single JSON Object.
//
// ID should be set to client provided ID.
// Message is a string providing a short description of the error. The message SHOULD be limited to a concise single sentence."
// Data is a primitive or structured value that contains additional information about the error. This may be omitted. The value of this member is defined by the Server (e.g. detailed error information, nested errors etc.).
type RpcError struct {
ID int `json:"id"`
Message string `json:"message"`
Data string `json:"data,omitempty"`
}
// JSON RPC response message. When a rpc call is made, the Server MUST reply with a Response, except for in the case of Notifications. The Response is expressed as a single JSON Object.
// Result and error are mutually exclusive, so only one of them can be expected. Error is represented as a pointer to RpcError, so it can be nil.
//
// JSONRpcVersion is mandatory. Version, which must be ‟2.0”. No other JSON RPC versions are currently supported.
// ID is mandatory. Client-provided integer. The JSON RPC responds with the same ID, which allows the client to match requests to responses when there are concurrent requests.
// Result is REQUIRED on success (jsonRawMessage). This member MUST NOT exist if there was an error invoking the method. The value of this member is determined by the method invoked on the Server.
// Error is REQUIRED on error. This member MUST NOT exist if there was no error triggered during invocation. The value for this member MUST be an RpcError object.
type Response struct {
JSONRpcVersion string `json:"jsonrpc"`
ID int `json:"id"`
Result json.RawMessage `json:"result,omitempty"`
Error *RpcError `json:"error,omitempty"`
}
// Marshalling Response into JSON.
func (r *Response) Marshal() ([]byte, error) {
b, err := json.Marshal(r)
if err != nil {
return nil, apierr.NewMessageError(apierr.CodeMsgRespMarshalling, err)
}
return b, nil
}
// Returns ID of the response in order to compare with request ID.
func (r *Response) GetID() int {
return r.ID
}