-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.go
81 lines (73 loc) · 1.56 KB
/
table.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
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
// Copyright (c) 2018 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package blockwatch
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
)
type Table struct {
Dataframe
Limit int `json:"limit"`
Count int `json:"count"`
Cursor string `json:"cursor"`
Error *Error `json:"error"`
}
type TableParams struct {
Columns []string
Cursor string
Limit int
Filter []*Filter
Format string
}
func (p TableParams) Query() url.Values {
q := url.Values{}
if len(p.Columns) > 0 && p.Columns[0] != "" {
q.Add("columns", strings.Join(p.Columns, ","))
}
for _, v := range p.Filter {
if v == nil {
continue
}
v.AppendQuery(q)
}
if p.Cursor != "" {
q.Add("cursor", p.Cursor)
}
if p.Limit > 0 {
q.Add("limit", strconv.Itoa(p.Limit))
}
return q
}
func (p TableParams) Url(db, set string) string {
if p.Format == "" {
p.Format = "json"
}
return fmt.Sprintf("tables/%s/%s.%s?%s",
db,
set,
p.Format,
p.Query().Encode(),
)
}
func (c *Client) GetTable(ctx context.Context, dbcode, setcode string, params TableParams) (*Table, error) {
v := &Table{}
err := c.Get(ctx, params.Url(dbcode, setcode), nil, v)
if err != nil {
return nil, err
}
// process streaming error
if v.Error != nil {
return v, v.Error
}
return v, nil
}
// TODO
// func (c *Client) StreamTable(ctx context.Context, params TableParams, fn func(r Row) error) error {
// // slower, but from stdlib
// // https://stackoverflow.com/questions/31794355/stream-large-json
// // fetch chunks, stream-decode them and loop until limit is reached
// return nil
// }