-
Notifications
You must be signed in to change notification settings - Fork 2
/
response.go
101 lines (84 loc) · 2.82 KB
/
response.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package tinybird
import (
"encoding/json"
"errors"
"io"
"github.com/olivere/ndjson"
)
// Basic JSON struct for all response by Tinybird.
type Response struct {
Cached bool // Flag control to use to determine the origin this response.
Raw io.ReadCloser // Raw is the original data from response.
Body string // Body in string format, same a Data but in struct format and Raw in original format.
Data []Row `json:"data,omitempty"` // Data is part a tinybird response.
Documentation string `json:"documentation,omitempty"` // Documentation is part a tinybird response.
Error string `json:"error,omitempty"` // Error is part a tinybird response.
Meta []Meta `json:"meta,omitempty"` // Meta is part a tinybird response.
Rows uint `json:"rows,omitempty"` // Rows is part a tinybird response.
RowsBeforeLimitAtLeast uint `json:"rows_before_limit_at_least,omitempty"` // RowsBeforeLimitAtLeast is part a tinybird response.
Statistics Statistics `json:"statistics,omitempty"` // Statistics is part a tinybird response.
Status int // Status is a HTTP status code, ej: 200, 400, 500 etc...
Format string // Save format setting.
}
// Generic row structure to allow any field with any type.
type Row map[string]any
// Specific field with data type.
type Meta struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
}
// Statistics information about the response.
type Statistics struct {
Elapsed float64 `json:"elapsed,omitempty"`
RowsRead uint64 `json:"rows_read,omitempty"`
BytesRead uint64 `json:"bytes_read,omitempty"`
}
// Convert response body to struct.
func (r *Response) Decode() (err error) {
switch r.Format {
case CSV:
err = r.CSV()
case NDJSON:
err = r.NDJSON()
default:
err = r.JSON()
}
return err
}
// Convert body to CSV.
func (r *Response) CSV() error {
if r.Raw == nil {
return errors.New("Raw is empty")
}
body, err := io.ReadAll(r.Raw)
if err != nil {
return err
}
r.Body = string(body)
return nil
}
// Convert body to NDJSON.
func (r *Response) NDJSON() error {
ndjsonReader := ndjson.NewReader(r.Raw)
for ndjsonReader.Next() {
var row Row
if err := ndjsonReader.Decode(&row); err != nil {
return err
}
r.Data = append(r.Data, row)
r.Rows++
}
return nil
}
// Convert body to JSON.
func (r *Response) JSON() error {
if r.Raw == nil {
return errors.New("Raw is empty")
}
body, err := io.ReadAll(r.Raw)
if err != nil {
return err
}
r.Body = string(body)
return json.Unmarshal(body, &r)
}