rpc: allow batch JSON-RPC requests

Close #1509
This commit is contained in:
Anna Shaleva 2020-10-26 20:22:20 +03:00
parent 679846c1a1
commit 6ee919747f
5 changed files with 272 additions and 58 deletions

View file

@ -24,6 +24,12 @@ type Raw struct {
Result json.RawMessage `json:"result,omitempty"`
}
// AbstractResult is an interface which represents either single JSON-RPC 2.0 response
// or batch JSON-RPC 2.0 response.
type AbstractResult interface {
RunForErrors(f func(jsonErr *Error))
}
// Abstract represents abstract JSON-RPC 2.0 response, it differs from Raw in
// that Result field is an interface here.
type Abstract struct {
@ -31,6 +37,25 @@ type Abstract struct {
Result interface{} `json:"result,omitempty"`
}
// RunForErrors implements AbstractResult interface.
func (a Abstract) RunForErrors(f func(jsonErr *Error)) {
if a.Error != nil {
f(a.Error)
}
}
// AbstractBatch represents abstract JSON-RPC 2.0 batch-response.
type AbstractBatch []Abstract
// RunForErrors implements AbstractResult interface.
func (ab AbstractBatch) RunForErrors(f func(jsonErr *Error)) {
for _, a := range ab {
if a.Error != nil {
f(a.Error)
}
}
}
// Notification is a type used to represent wire format of events, they're
// special in that they look like requests but they don't have IDs and their
// "method" is actually an event name.