forked from golangdaddy/multichain-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_liststreamqueryitems.go
54 lines (49 loc) · 1.6 KB
/
api_liststreamqueryitems.go
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
package multichain
import (
"errors"
)
// This works like liststreamitems, but listing items in stream which match all of the specified keys and/or
// publishers in query.
// The query is an object with a key or keys field, and/or a publisher or publishers field. If present, key and
// publisher should specify a single key or publisher respectively, whereas keys and publishers should specify
// arrays thereof.
// Note that, unlike other stream retrieval APIs, liststreamqueryitems cannot rely completely on prior indexing,
// so the maxqueryscanitems runtime parameter limits how many items will be scanned after using the best index.
// If more than this is needed, an error will be returned.
func (client *Client) ListStreamQueryItems(stream string, keys, publishers []string, verbose bool) (Response, error) {
q, err := getQuery(keys, publishers)
if err != nil {
return nil, err
}
return client.Post(
map[string]interface{}{
"jsonrpc": "1.0",
"id": CONST_ID,
"method": "liststreamqueryitems",
"params": []interface{}{
stream,
q,
verbose,
},
},
)
}
func getQuery(keys, publishers []string) (map[string]interface{}, error) {
lenKeys := len(keys)
lenPublishers := len(publishers)
if lenKeys == 0 && lenPublishers == 0 {
return nil, errors.New("either keys or publishers slices must contain at least one entry")
}
q := make(map[string]interface{})
if lenKeys == 1 {
q["key"] = keys[0]
} else if lenKeys > 1 {
q["keys"] = keys
}
if lenPublishers == 1 {
q["publisher"] = publishers[0]
} else if lenPublishers > 1 {
q["publishers"] = publishers
}
return q, nil
}